/* Results panel — sticky dark card with priority stats + full metrics */

function ResultsPanel({ result }) {
  const f = window.fmt;
  const fuel = window.SicosaEngine.FUELS[result.inputs.fuelType];
  const profitBatchDisplay = result.inputs.includeFixedCosts ? result.profitPerBatchWithFixedAmd : result.profitPerBatchAmd;
  const profitBatchUsd     = result.inputs.includeFixedCosts ? result.usd.profitPerBatchWithFixed : result.usd.profitPerBatch;
  const profitPositive     = profitBatchDisplay > 0;

  // Priority outputs (top of panel)
  // 1. Cost per liter   2. Break-even selling price   3. Profit per batch
  return (
    <aside className="result">
      <div className="result__head">
        <div>
          <div className="result__eyebrow">Live result</div>
          <div className="result__title">
            {fuel.label} · {f.amd(result.inputs.batchTons, 2)} t batch
          </div>
        </div>
        <div className="result__live">
          <span className="result__live-dot"></span>
          Live
        </div>
      </div>

      <div className="priority">
        <div className="prio-stat prio-stat--main">
          <div className="prio-stat__label">Cost per liter (incl. VAT)</div>
          <div className="prio-stat__line">
            <span className="prio-stat__val">{f.amdL(result.inputs.includeFixedCosts ? result.costPerLiterWithFixedAmd : result.costPerLiterAmd)}</span>
            <span className="prio-stat__unit">AMD/L</span>
          </div>
          <div className="prio-stat__sub">
            <span>${f.usd(result.inputs.includeFixedCosts ? result.usd.costPerLiterWithFixed : result.usd.costPerLiter, 4)}</span> /L · {result.inputs.includeFixedCosts ? "incl. fixed costs" : "excl. fixed costs"}
          </div>
        </div>

        <div className="priority__row2">
          <div className="prio-stat prio-stat--secondary">
            <div className="prio-stat__label">Break-even price</div>
            <div className="prio-stat__line">
              <span className="prio-stat__val">{f.amdL(result.breakEvenAmdPerLiter)}</span>
              <span className="prio-stat__unit">AMD/L</span>
            </div>
            <div className="prio-stat__sub">Zero-margin selling price</div>
          </div>

          <div className={"prio-stat prio-stat--secondary prio-stat--profit " + (profitPositive ? "is-positive" : "is-negative")}>
            <div className="prio-stat__label">Profit per batch</div>
            <div className="prio-stat__line">
              <span className="prio-stat__val">{f.amd(profitBatchDisplay)}</span>
              <span className="prio-stat__unit">AMD</span>
            </div>
            <div className="prio-stat__sub">
              <span>${f.usd(profitBatchUsd)}</span> · {result.inputs.batchTons} t · {result.inputs.includeFixedCosts ? "incl." : "excl."} fixed costs
            </div>
          </div>
        </div>
      </div>

      <div className="metrics">
        <Section label="Per ton" />
        <Metric k="Cost per ton" amd={result.totalCostAmdPerTon} usd={result.usd.totalCostPerTon} />
        <Metric k="Profit per ton" amd={result.profitPerTonAmd} usd={result.usd.profitPerTon} signed />
        <Metric k="Liters per ton" amd={result.litersPerTon} usd={null} unit="L" decimals={2} />

        <Section label="Per batch" />
        <Metric k="Total batch liters" amd={result.totalBatchLiters} usd={null} unit="L" decimals={1} />
        <Metric k="Total batch cost" amd={result.totalCostAmdPerBatch} usd={result.usd.batchCost} />
        <Metric k="Batch revenue" amd={result.batchRevenueAmd} usd={result.usd.batchRevenue} />
        {result.fixedCostAmdPerBatch > 0 ? (
          <Metric k="Fixed costs (incl.)" amd={result.fixedCostAmdPerBatch} usd={result.fixedCostAmdPerBatch / result.inputs.fxRate} />
        ) : null}

        <Section label="Taxes per ton" />
        <Metric k="VAT amount" amd={result.vatAmdPerTon} usd={result.usd.vat} />
        <Metric k="Excise amount" amd={result.exciseAmdPerTon} usd={result.usd.excise} />
        {result.customsAmdPerTon > 0 ? (
          <Metric k="Customs duty" amd={result.customsAmdPerTon} usd={result.customsAmdPerTon / result.inputs.fxRate} />
        ) : null}
        <Metric k="Eco tax amount" amd={result.ecoTaxAmdPerTon} usd={result.usd.ecoTax} />

        <Section label="Margin" />
        <Metric k="Margin per liter" amd={result.marginPerLiterAmd} usd={result.usd.marginPerLiter} signed decimals={2} />
        <Metric k="Margin %" amd={result.marginPct * 100} usd={null} unit="%" decimals={2} signed />

        <Section label="VAT overpayment (analytical)" />
        <Metric k="Per liter" amd={result.vatOverpaymentPerLiterAmd} usd={result.usd.vatOverpaymentPerLiter} decimals={2} dim />
        <Metric k="Per ton" amd={result.vatOverpaymentPerTonAmd} usd={result.usd.vatOverpaymentPerTon} dim />
        <Metric k="Profit after VAT overpayment" amd={result.profitAfterVatOverpaymentAmd} usd={result.usd.profitAfterVatOverpayment} signed />

        <Section label="Net result" />
        <Metric k="Net profit (18% tax)" amd={result.netProfitAfter18TaxAmd} usd={result.usd.netProfitAfter18Tax} signed strong />
      </div>

    </aside>
  );
}

function Section({ label }) {
  return (
    <div className="metrics__row is-section">
      <div className="metrics__k">{label}</div>
      <div></div>
      <div></div>
    </div>
  );
}

function Metric({ k, amd, usd, unit, decimals = 0, signed = false, dim = false, strong = false }) {
  const f = window.fmt;
  const sign = signed && amd > 0 ? "+" : "";
  const cls = "metrics__row" + (signed && amd > 0 ? " is-positive" : signed && amd < 0 ? " is-negative" : "");
  const display = unit === "%"
    ? sign + amd.toFixed(decimals) + "%"
    : sign + (decimals > 0 ? f.amdL(amd) : f.amd(amd)) + (unit ? " " + unit : "");
  return (
    <div className={cls} style={dim ? { opacity: 0.85 } : null}>
      <div className="metrics__k">{k}</div>
      <div className="metrics__v-amd" style={strong ? { fontWeight: 700, fontSize: 14 } : null}>{display}</div>
      <div className="metrics__v-usd">
        {usd != null ? "$" + f.usd(usd) : ""}
      </div>
    </div>
  );
}

window.ResultsPanel = ResultsPanel;
