import { render, screen, fireEvent } from '@testing-library/react';
import { ThemeProvider } from '../../theme';

jest.mock('next-intl', () => ({ useTranslations: () => (k: string) => k, useLocale: () => 'en' }));

import PartnerSettlementRow from './PartnerSettlementRow';
import type { CenterInvoice } from '@/services/partnerCenter/types';

// Reconciles: commission 750000 + bnpl 60000 + vat 75000 = 885000 total (VAT on commission only).
const INVOICE: CenterInvoice = {
  id: 7,
  bookingId: 5003,
  invoiceNumber: 'INV-1405-1007',
  grossIrr: '5000000',
  platformCommissionIrr: '750000',
  bnplCommissionIrr: '60000',
  vatRate: 0.1,
  vatIrr: '75000',
  totalIrr: '885000',
  moadianReferenceNumber: '1234567890123456789012',
  moadianStatus: 'registered',
  pdfUrl: 'https://mock.local/7.pdf',
  issuedAt: '2026-01-01T00:00:00Z',
};

describe('<PartnerSettlementRow/>', () => {
  const wrap = (ui: React.ReactNode) => render(<ThemeProvider>{ui}</ThemeProvider>);

  it('renders a reconciling commission breakdown without a console error', () => {
    const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
    wrap(<PartnerSettlementRow invoice={INVOICE} />);
    expect(screen.getByText('invoice_number')).toBeInTheDocument();
    // PriceBreakdown dev-guard would console.error if commission+bnpl+vat !== total.
    expect(errorSpy).not.toHaveBeenCalledWith(expect.stringContaining('must reconcile'));
    errorSpy.mockRestore();
  });

  it('exposes the مودیان reference', () => {
    wrap(<PartnerSettlementRow invoice={INVOICE} />);
    expect(screen.getByText(/1234567890123456789012/)).toBeInTheDocument();
  });

  it('fires onDownloadPdf', () => {
    const onDownloadPdf = jest.fn();
    wrap(<PartnerSettlementRow invoice={INVOICE} onDownloadPdf={onDownloadPdf} />);
    fireEvent.click(screen.getByText('invoice_download'));
    expect(onDownloadPdf).toHaveBeenCalledWith(INVOICE);
  });
});
