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

const samplePanel: BalanceSheetPanelData = {
  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: [],
  selectedCurrency: 'EUR',
  availableCurrencies: ['EUR', 'USD'],
  mappingWarnings: [],
  availablePeriods: [],
  periods: [
    {
      label: 'FY2024',
      fiscalYear: '2024',
      fiscalPeriod: 'FY',
      periodEnd: '2024-09-28',
      filedDate: '2025-02-01',
      form: '10-K',
      cashAndEquivalents: undefined,
      shortTermInvestments: 15,
      accountsReceivableNet: 30,
      inventory: 5,
      otherCurrentAssets: 8,
      totalCurrentAssets: 58,
      propertyPlantAndEquipmentNet: 40,
      goodwill: 2,
      intangibleAssets: 4,
      otherNonCurrentAssets: 9,
      totalAssets: undefined,
      accountsPayable: 18,
      accruedExpensesAndOtherCurrentLiabilities: 12,
      shortTermDebt: 6,
      totalCurrentLiabilities: 36,
      longTermDebt: 70,
      otherNonCurrentLiabilities: 11,
      totalLiabilities: undefined,
      retainedEarnings: 22,
      accumulatedOtherComprehensiveIncomeLoss: -3,
      totalEquity: undefined,
      sharesOutstanding: undefined,
      bookValuePerShare: 4.25,
    },
  ],
};

describe('BalanceSheetPanel', () => {
  it('renders the granular balance sheet metric set', () => {
    const html = renderToStaticMarkup(<BalanceSheetPanel data={samplePanel} />);

    expect(html).toContain('Short-Term Investments');
    expect(html).toContain('Accounts Receivable, Net');
    expect(html).toContain('Property, Plant &amp; Equipment, Net');
    expect(html).toContain('Long-Term Debt');
    expect(html).toContain('Book Value / Share');
  });

  it('only offers inline mapping for mapped balance-sheet totals', () => {
    const html = renderToStaticMarkup(<BalanceSheetPanel 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('builds canonical /bs commands with currency', () => {
    expect(buildBalanceSheetCommand('SAP', 'annual', { currency: 'eur' })).toBe(
      '/bs SAP annual --currency EUR',
    );
  });

  it('renders the currency selector when multiple currencies are available', () => {
    const html = renderToStaticMarkup(
      <BalanceSheetPanel data={samplePanel} onRunCommand={() => {}} />,
    );

    expect(html).toContain('Currency');
    expect(html).toContain('aria-label="Balance sheet currency"');
    expect(html).toContain('EUR');
    expect(html).toContain('USD');
  });

  it('renders a static currency label when only one currency is available', () => {
    const html = renderToStaticMarkup(
      <BalanceSheetPanel
        data={{
          ...samplePanel,
          selectedCurrency: 'EUR',
          availableCurrencies: ['EUR'],
        }}
      />,
    );

    expect(html).toContain('Currency');
    expect(html).not.toContain('aria-label="Balance sheet currency"');
    expect(html).toContain('>EUR<');
  });
});
