import type { TokenAggregationHoReserveCoverage } from '@/services/api/tokenAggregation'
import type { TokenLiquidityMarketView } from '@/utils/tokenMarket'
import {
  formatCashInBankAuditLabel,
  formatCashInBankLabel,
  formatCashLiquidityLabel,
  formatHoLiquidityAuditFreshness,
  formatHoReserveCoverageLabel,
  formatSupplyNostroMatchLabel,
  resolveDisplayedCashInBankUsd,
  resolveDisplayedCashLiquidityUsd,
} from '@/utils/tokenMarket'

type TokenLiquidityStackProps = {
  market: TokenLiquidityMarketView
  className?: string
  /** Include supply nostro detail on the cash-in-bank line (default false for compact stack). */
  verboseCashInBank?: boolean
}

export default function TokenLiquidityStack({
  market,
  className = 'space-y-1 text-xs text-gray-500 dark:text-gray-400',
  verboseCashInBank = false,
}: TokenLiquidityStackProps) {
  const cashInBankUsd = resolveDisplayedCashInBankUsd(market)
  const coverage = market.hoReserveCoverage

  return (
    <div className={className}>
      <div>Cash liq. {formatCashLiquidityLabel(resolveDisplayedCashLiquidityUsd(market))}</div>
      <div>
        Cash-in-bank{' '}
        {verboseCashInBank
          ? formatCashInBankLabel(cashInBankUsd, market.cashInBankAudit, market.supplyNostroMatch)
          : cashInBankUsd != null
            ? `${formatCashInBankLabel(cashInBankUsd, market.cashInBankAudit)} · GL 13010 nostro`
            : 'Cash-in-bank unavailable'}
      </div>
      {coverage ? (
        <div title="OMNL Fineract book reserve assets (M0), not correspondent nostro cash.">
          Reserve coverage {formatHoReserveCoverageLabel(coverage)}
        </div>
      ) : null}
      {market.supplyNostroMatch ? (
        <div className="text-[11px] opacity-80" title="Per-token supply vs 1.2× HO nostro cash (GL 13010). May break for large c* while aggregate reserve coverage remains sufficient.">
          Nostro match {formatSupplyNostroMatchLabel(market.supplyNostroMatch)}
        </div>
      ) : null}
      <div className="text-[11px] opacity-80">{formatHoLiquidityAuditFreshness(market)}</div>
    </div>
  )
}

export function formatHoReserveCoverageShort(coverage: TokenAggregationHoReserveCoverage | undefined): string {
  if (!coverage) return 'Reserve coverage unavailable'
  return formatHoReserveCoverageLabel(coverage)
}
