"use client";

import { fmtCurrency, fmtLarge, fmtPct } from "@/lib/format";
import type { DcfComputed } from "@/lib/dcf";
import type { DcfResult } from "@/types/api";

type Props = {
  dcf: DcfResult;
  computed: DcfComputed | { error: string } | null;
  livePrice: number | null;
  sharesOutstanding: number | null;
  currency: string;
};

export function DcfResults({ dcf, computed, livePrice, sharesOutstanding, currency }: Props) {
  return (
    <div className="psm-val-dcf-results">
      <div className="psm-val-dcf-col-label">Results</div>

      {computed && "error" in computed ? (
        <p className="psm-val-dcf-error">{computed.error}</p>
      ) : (
        <>
          <div className="psm-val-kv-list">
            <div className="psm-val-kv-row">
              <span className="psm-val-kv-label">Base FCF (TTM)</span>
              <span className="psm-val-kv-val">
                {dcf.base_fcf != null ? fmtLarge(dcf.base_fcf, currency) : "—"}
              </span>
            </div>
            <div className="psm-val-kv-row">
              <span className="psm-val-kv-label">Historical growth</span>
              <span className="psm-val-kv-val">
                {dcf.growth_rate_used != null ? fmtPct(dcf.growth_rate_used) : "—"}
              </span>
            </div>
            <div className="psm-val-kv-row is-divider">
              <span className="psm-val-kv-label">FCF PV Sum</span>
              <span className="psm-val-kv-val">
                {computed ? fmtLarge(computed.fcfPvSum, currency) : "—"}
              </span>
            </div>
            <div className="psm-val-kv-row">
              <span className="psm-val-kv-label">Terminal Value PV</span>
              <span className="psm-val-kv-val">
                {computed ? fmtLarge(computed.terminalValuePv, currency) : "—"}
              </span>
            </div>
            <div className="psm-val-kv-row">
              <span className="psm-val-kv-label">Enterprise Value</span>
              <span className="psm-val-kv-val">
                {computed ? fmtLarge(computed.enterpriseValue, currency) : "—"}
              </span>
            </div>
            <div className="psm-val-kv-row is-total">
              <span className="psm-val-kv-label">Net Debt</span>
              <span className="psm-val-kv-val">
                {dcf.net_debt != null ? fmtLarge(dcf.net_debt, currency) : "—"}
              </span>
            </div>
          </div>
          <div className="psm-val-intrinsic">
            <span className="psm-val-intrinsic-label">Intrinsic Value</span>
            <span className={`psm-val-intrinsic-price${livePrice != null && livePrice < 0 ? " invalid" : ""}`}>
              {livePrice != null && livePrice < 0
                ? "Invalid assumptions"
                : livePrice != null
                  ? fmtCurrency(livePrice, 2, currency)
                  : "—"}
            </span>
            {sharesOutstanding != null && (
              <span className="psm-val-intrinsic-shares">
                {fmtLarge(sharesOutstanding, currency)} shares
              </span>
            )}
          </div>
        </>
      )}
    </div>
  );
}
