import React from 'react';
import { CashFlowPanelData } from '../../types/financial';
import { StatementTableMinimal, formatMoney } from '../ui/StatementTableMinimal';

interface CashFlowPanelProps {
  data: CashFlowPanelData;
}

const SourceAttribution: React.FC<{ status: CashFlowPanelData['sourceStatus'] }> = ({ status }) => {
  return (
    <div className="text-[11px] font-mono text-term-text-muted">
      SEC EDGAR • companyfacts
      {status.degradedReason && (
        <div className="mt-1 text-warning">{status.degradedReason}</div>
      )}
    </div>
  );
};

export const CashFlowPanel: React.FC<CashFlowPanelProps> = ({ data }) => {
  return (
    <div className="cashflow-panel py-4 overflow-auto">
      {/* Header - Minimal */}
      <header className="mb-4">
        <div className="flex items-start justify-between">
          <div>
            <h2 className="text-heading-lg text-term-text">{data.symbol} Cash Flow</h2>
            <p className="mt-1 text-body-xs text-term-text-muted">
              {data.companyName} • {data.frequency}
            </p>
          </div>
          <div className="text-right text-body-xs">
            <div className="text-term-text-muted">CIK {data.cik}</div>
            {data.latestFiling && (
              <div className="mt-1 text-term-text-tertiary">
                {data.latestFiling.form} filed {data.latestFiling.filingDate}
              </div>
            )}
          </div>
        </div>
      </header>

      {/* Table - Minimal styling */}
      <section>
        <StatementTableMinimal
          periods={data.periods}
          metrics={[
            { key: 'cfo', label: 'Operating Cash Flow', render: (period) => formatMoney(period.operatingCashFlow) },
            { key: 'cfi', label: 'Investing Cash Flow', render: (period) => formatMoney(period.investingCashFlow) },
            { key: 'cff', label: 'Financing Cash Flow', render: (period) => formatMoney(period.financingCashFlow) },
            { key: 'capex', label: 'Capex', render: (period) => formatMoney(period.capex) },
            { key: 'fcf', label: 'Free Cash Flow', render: (period) => formatMoney(period.freeCashFlow) },
            { key: 'endingCash', label: 'Ending Cash', render: (period) => formatMoney(period.endingCash) },
          ]}
        />
      </section>

      {/* Footer - Minimal attribution */}
      <footer className="mt-4 border-t border-term-border-subtle pt-4">
        <SourceAttribution status={data.sourceStatus} />
      </footer>
    </div>
  );
};
