import { render, screen } from '@testing-library/react';
import { ThemeProvider } from '../../theme';
import type { NurseEarningsSummary } from '@/services/payouts/types';

// next-intl mocked to echo keys; locale 'en' so the money util renders ASCII digits for assertions.
jest.mock('next-intl', () => ({
  useTranslations: () => (key: string) => key,
  useLocale: () => 'en',
}));

import EarningsBalanceHeader from './EarningsBalanceHeader';

const BASE: NurseEarningsSummary = {
  pendingTotalIrr: '4250000',
  eligibleTotalIrr: '3400000',
  paidTotalIrr: '8500000',
  clawbackOutstandingIrr: '1700000',
  netPayableBalanceIrr: '5950000',
};

function renderHeader(summary: NurseEarningsSummary) {
  return render(
    <ThemeProvider>
      <EarningsBalanceHeader summary={summary} />
    </ThemeProvider>,
  );
}

describe('<EarningsBalanceHeader/> component', () => {
  it('renders a positive net balance as the payable state with the Toman magnitude', () => {
    const { container } = renderHeader(BASE);
    expect(container.querySelector('[data-balance-state="payable"]')).toBeInTheDocument();
    expect(screen.getByText('balance_net_label')).toBeInTheDocument();
    // 5,950,000 IRR ÷ 10 = 595,000 Toman
    expect(screen.getByText(/595,000/)).toBeInTheDocument();
  });

  it('renders a negative net balance as "owed back" — magnitude only, never a bare minus', () => {
    const { container } = renderHeader({ ...BASE, netPayableBalanceIrr: '-4350000' });
    expect(container.querySelector('[data-balance-state="owed"]')).toBeInTheDocument();
    expect(screen.getByText('balance_owed_label')).toBeInTheDocument();
    // The magnitude 435,000 shows; the raw "-435,000" must not.
    expect(screen.getByText(/435,000/)).toBeInTheDocument();
    expect(screen.queryByText(/-435,000/)).not.toBeInTheDocument();
  });

  it('renders all four buckets with their formatted Toman amounts', () => {
    const { container } = renderHeader(BASE);
    for (const bucket of ['pending', 'eligible', 'paid', 'clawback']) {
      expect(container.querySelector(`[data-bucket="${bucket}"]`)).toBeInTheDocument();
    }
    expect(screen.getByText(/425,000/)).toBeInTheDocument(); // pending 4,250,000
    expect(screen.getByText(/340,000/)).toBeInTheDocument(); // eligible 3,400,000
    expect(screen.getByText(/850,000/)).toBeInTheDocument(); // paid lifetime 8,500,000
    expect(screen.getByText(/170,000/)).toBeInTheDocument(); // clawback outstanding 1,700,000
  });
});
