import { describe, expect, it } from 'bun:test';
import { renderToStaticMarkup } from 'react-dom/server';
import { FinancialsPanel } from './FinancialsPanel';
import type { FinancialsPanelData } from '../../types/financial';

const samplePanel: FinancialsPanelData = {
  symbol: 'AAPL',
  companyName: 'Apple Inc.',
  cik: '0000320193',
  frequency: 'annual',
  latestFiling: {
    accessionNumber: '0000320193-25-000010',
    filingDate: '2025-02-01',
    form: '10-K',
  },
  sourceStatus: {
    companyfactsUsed: true,
  },
  availableConcepts: [],
  mappingWarnings: [],
  periods: [
    {
      label: 'FY2024',
      fiscalYear: '2024',
      fiscalPeriod: 'FY',
      periodEnd: '2024-09-28',
      filedDate: '2025-02-01',
      form: '10-K',
      revenue: 100,
      grossProfit: 50,
      operatingIncome: 25,
      netIncome: 20,
      dilutedEps: 1.25,
      cashAndEquivalents: undefined,
      totalAssets: undefined,
      totalLiabilities: undefined,
      totalEquity: undefined,
      sharesOutstanding: undefined,
    },
  ],
};

describe('FinancialsPanel', () => {
  it('only offers inline mapping for missing balance-sheet rows', () => {
    const html = renderToStaticMarkup(<FinancialsPanel data={samplePanel} />);

    expect((html.match(/>Map</g) || []).length).toBe(5);
    expect(html).toContain('Cash &amp; Equivalents');
    expect(html).toContain('Total Assets');
    expect(html).toContain('Total Liabilities');
    expect(html).toContain('Total Equity');
    expect(html).toContain('Shares Outstanding');
  });

  it('renders mapping and calculation warnings when present', () => {
    const html = renderToStaticMarkup(
      <FinancialsPanel
        data={{
          ...samplePanel,
          mappingWarnings: [
            'TotalAssets: Concept not found in XBRL document: us-gaap:AssetsCurrent',
          ],
        }}
      />,
    );

    expect(html).toContain('Mapping &amp; Calculation Warnings');
    expect(html).toContain('TotalAssets: Concept not found in XBRL document');
  });
});
