import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import FeesManagement from '@/components/Structure/Tarification/FeesManagement';

jest.mock('@/context/EstablishmentContext', () => ({
  useEstablishment: () => ({ selectedEstablishmentId: 1 }),
}));

jest.mock('@/utils/Url', () => ({
  BE_SCHOOL_FEES_URL: '/api/fees',
  BE_SCHOOL_DISCOUNTS_URL: '/api/discounts',
  BE_SCHOOL_PAYMENT_PLANS_URL: '/api/payment-plans',
  BE_SCHOOL_PAYMENT_MODES_URL: '/api/payment-modes',
}));

jest.mock('@/utils/logger', () => ({ error: jest.fn() }));

jest.mock(
  '@/components/Tab',
  () =>
    function MockTab({ text, active, onClick }) {
      return (
        <button
          data-testid={`tab-${text}`}
          data-active={active}
          onClick={onClick}
        >
          {text}
        </button>
      );
    }
);

jest.mock(
  '@/components/Structure/Tarification/FeeTypeSection',
  () =>
    function MockFeeTypeSection({ title, type, fees, discounts }) {
      return (
        <div data-testid={`fee-type-section-${type}`} data-title={title}>
          {fees.map((f) => (
            <span key={f.id}>{f.name}</span>
          ))}
          {discounts.map((d) => (
            <span key={d.id}>{d.name}</span>
          ))}
        </div>
      );
    }
);

const defaultProps = {
  registrationFees: [],
  setRegistrationFees: jest.fn(),
  tuitionFees: [],
  setTuitionFees: jest.fn(),
  registrationDiscounts: [],
  setRegistrationDiscounts: jest.fn(),
  tuitionDiscounts: [],
  setTuitionDiscounts: jest.fn(),
  registrationPaymentPlans: [],
  setRegistrationPaymentPlans: jest.fn(),
  tuitionPaymentPlans: [],
  setTuitionPaymentPlans: jest.fn(),
  registrationPaymentModes: [],
  setRegistrationPaymentModes: jest.fn(),
  tuitionPaymentModes: [],
  setTuitionPaymentModes: jest.fn(),
  handleCreate: jest.fn(),
  handleEdit: jest.fn(),
  handleDelete: jest.fn(),
};

describe("FeesManagement - système d'onglets", () => {
  it("affiche l'onglet inscription actif par défaut", () => {
    render(<FeesManagement {...defaultProps} />);
    expect(screen.getByTestId('fee-type-section-0')).toBeInTheDocument();
    expect(screen.queryByTestId('fee-type-section-1')).not.toBeInTheDocument();
  });

  it("bascule sur l'onglet scolarité au clic", () => {
    render(<FeesManagement {...defaultProps} />);
    fireEvent.click(screen.getByTestId('tab-Frais de scolarité'));
    expect(screen.getByTestId('fee-type-section-1')).toBeInTheDocument();
    expect(screen.queryByTestId('fee-type-section-0')).not.toBeInTheDocument();
  });

  it("passe les frais d'inscription à la section type=0", () => {
    render(
      <FeesManagement
        {...defaultProps}
        registrationFees={[{ id: 1, name: 'Inscription A', type: 0 }]}
      />
    );
    expect(screen.getByText('Inscription A')).toBeInTheDocument();
  });

  it('passe les frais de scolarité à la section type=1', () => {
    render(
      <FeesManagement
        {...defaultProps}
        tuitionFees={[{ id: 2, name: 'Scolarité B', type: 1 }]}
      />
    );
    fireEvent.click(screen.getByTestId('tab-Frais de scolarité'));
    expect(screen.getByText('Scolarité B')).toBeInTheDocument();
  });
});
