import type { MoneySupply } from '../central-bank/useCentralBank';

import NasaIcon from '../../components/icons/NasaIcon';



type Props = {

  moneySupply: MoneySupply | null;

  loading?: boolean;

  officeName?: string;

};



function formatAsOf(asOf?: string) {

  if (!asOf) return null;

  return asOf.slice(0, 19).replace('T', ' ') + ' UTC';

}



function BannerIcon({ variant }: { variant: 'ok' | 'warn' | 'error' | 'loading' }) {

  const map = {

    ok: { name: 'check' as const, className: 'nasa-icon--nominal' },

    warn: { name: 'alert' as const, className: 'nasa-icon--caution' },

    error: { name: 'x' as const, className: 'nasa-icon--critical' },

    loading: { name: 'loading' as const, className: 'nasa-icon--telemetry nasa-icon--spin' },

  };

  const { name, className } = map[variant];

  return <NasaIcon name={name} size={18} className={`shrink-0 mt-0.5 ${className}`} />;

}



export default function FineractLedgerBanner({ moneySupply, loading, officeName }: Props) {

  if (loading && !moneySupply) {

    return (

      <div className="dash-banner dash-banner--loading mb-6">

        <BannerIcon variant="loading" />

        <div>

          <p className="dash-banner__title">Loading live ledger balances</p>

          <p className="dash-banner__detail">Fetching Fineract data for Office 24</p>

        </div>

      </div>

    );

  }



  if (!moneySupply) {

    return (

      <div className="dash-banner dash-banner--warn mb-6">

        <BannerIcon variant="warn" />

        <div>

          <p className="dash-banner__title">Ledger data not available</p>

          <p className="dash-banner__detail">

            The money-supply API did not respond. Check settlement middleware on port 3011.

          </p>

        </div>

      </div>

    );

  }



  const asOf = formatAsOf(moneySupply.asOf);

  const officeLabel = officeName ?? `Office ${moneySupply.officeId ?? 24}`;



  if (moneySupply.fineractConnected === false) {

    return (

      <div className="dash-banner dash-banner--error mb-6">

        <BannerIcon variant="error" />

        <div>

          <p className="dash-banner__title">Fineract offline</p>

          <p className="dash-banner__detail">

            Cannot reach Fineract for {officeLabel}. Verify OMNL_FINERACT_* credentials on the

            settlement service.

          </p>

        </div>

      </div>

    );

  }



  if (moneySupply.fineractConnected && !moneySupply.fineractLive) {

    return (

      <div className="dash-banner dash-banner--warn mb-6">

        <BannerIcon variant="warn" />

        <div>

          <p className="dash-banner__title">Fineract connected — opening journal pending</p>

          <p className="dash-banner__detail">

            {officeLabel} has no M1 allocation yet. Post the opening entry: Debit GL 1410 (Due from

            Head Office) · Credit GL 2100 (M1 liabilities).

            {asOf ? ` · Checked ${asOf}` : ''}

          </p>

        </div>

      </div>

    );

  }



  return (

    <div className="dash-banner dash-banner--ok mb-6">

      <BannerIcon variant="ok" />

      <div>

        <p className="dash-banner__title">Live Fineract ledger · {officeLabel}</p>

        <p className="dash-banner__detail">

          M0 / M1 / M2 balances below are read from Fineract in real time.

          {asOf ? ` Last updated ${asOf}.` : ''}

        </p>

      </div>

    </div>

  );

}

