// BuildingView.jsx — High-level building overview: 11 floors × 2 services

var BUILDING_FLOORS = [
  { n:11, a:{ name:"Soins intensifs", fill:78 },      b:{ name:"Réanimation", fill:64 } },
  { n:10, a:{ name:"Chirurgie cardiaque", fill:71 },   b:{ name:"Cardiologie", fill:33 } },
  { n:9,  a:{ name:"Neurochirurgie", fill:58 },        b:{ name:"Neurologie", fill:82 } },
  { n:8,  a:{ name:"Orthopédie", fill:47 },            b:{ name:"Traumatologie", fill:12 } },
  { n:7,  a:{ name:"Oncologie", fill:69 },             b:{ name:"Hématologie", fill:55 } },
  { n:6,  a:{ name:"Médecine interne", fill:88 },      b:{ name:"Pneumologie", fill:37 } },
  { n:5,  a:{ name:"Néphrologie", fill:61 },           b:{ name:"Gastro-entérologie", fill:74 } },
  { n:4,  a:{ name:"Maternité", fill:90 },             b:{ name:"Néonatologie", fill:52 } },
  { n:3,  a:{ name:"Pédiatrie", fill:44 },             b:{ name:"Chirurgie pédiatrique", fill:28 } },
  { n:2,  a:{ name:"Médecine privé", fill:55 },        b:{ name:"Gériatrie", fill:9 } },
  { n:1,  a:{ name:"Urgences", fill:68 },              b:{ name:"Bloc opératoire", fill:41 } },
];

(function() {
  var uid = 0;
  function statusOf(fill) { return fill < 15 ? "out" : fill < 40 ? "low" : "ok"; }
  BUILDING_FLOORS.forEach(function(f) {
    ["a", "b"].forEach(function(side) {
      f[side].id = "svc" + (uid++);
      f[side].niveau = f.n;
      f[side].wing = side.toUpperCase();
      f[side].status = statusOf(f[side].fill);
    });
  });
})();

var ALL_SERVICES = BUILDING_FLOORS.reduce(function(acc, f) { return acc.concat([f.a, f.b]); }, []);
var ALERT_SERVICES = ALL_SERVICES.filter(function(s) { return s.status !== "ok"; })
  .sort(function(a, b) {
    var aP = a.status === "out" ? 0 : 1;
    var bP = b.status === "out" ? 0 : 1;
    return aP - bP || a.fill - b.fill;
  });

var STATUS_LABEL_BLD = { ok: "Bon", low: "À risque", out: "Rupture" };

function BuildingKpis() {
  var ruptures = ALL_SERVICES.filter(function(s) { return s.status === "out"; }).length;
  var risques = ALL_SERVICES.filter(function(s) { return s.status === "low"; }).length;
  return (
    <div className="kpis">
      <div className="kpi">
        <div className="lab"><Icon name="triangle-alert" size={16} /> Services en alerte</div>
        <div className="val">{ruptures + risques}<small> / {ALL_SERVICES.length}</small></div>
        <div className="sub">{ruptures} en rupture · {risques} à risque</div>
      </div>
      <div className="kpi">
        <div className="lab"><Icon name="package" size={16} /> À charger</div>
        <div className="val">312<small> pièces</small></div>
        <div className="sub">réparties sur {ALL_SERVICES.length} services</div>
      </div>
      <div className="kpi">
        <div className="lab"><Icon name="clock" size={16} /> Réserve de linge</div>
        <div className="val">28<small>h</small></div>
        <div className="sub">bâtiment principal</div>
      </div>
    </div>
  );
}

function ServiceCell({ s, side, onClick }) {
  return (
    <button className={"svc " + side + " " + s.status} onClick={function() { onClick(s); }}>
      <div className="r1">
        <span className="nm">{s.name}</span>
        <span className="pct">{s.fill}%</span>
      </div>
      <div className="mini"><i style={{ width: s.fill + "%" }} /></div>
    </button>
  );
}

function BuildingBlock({ onSelectService }) {
  return (
    <div className="bldg">
      <div className="bcols">
        <div className="roofname">
          <Icon name="hospital" size={16} />
          CHUV — Bâtiment hospitalier
        </div>
      </div>
      {BUILDING_FLOORS.map(function(f) {
        return (
          <div className="floor" key={f.n}>
            <div className="flab">
              <b>{String(f.n).padStart(2, "0")}</b>
              <span>Niv</span>
            </div>
            <ServiceCell s={f.a} side="a" onClick={onSelectService} />
            <ServiceCell s={f.b} side="b" onClick={onSelectService} />
          </div>
        );
      })}
    </div>
  );
}

function BuildingView({ onSelectService }) {
  return (
    <React.Fragment>
      <div className="bwrap">
        <BuildingBlock onSelectService={onSelectService} />
      </div>
    </React.Fragment>
  );
}

window.BuildingView = BuildingView;
window.ALERT_SERVICES = ALERT_SERVICES;
window.STATUS_LABEL_BLD = STATUS_LABEL_BLD;
