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

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

// Mock des sous-composants pour isoler FeeTypeSection
jest.mock(
  '@/components/Structure/Tarification/FeesSection',
  () =>
    function MockFeesSection({ type }) {
      return (
        <div data-testid={`fees-section-type-${type}`}>
          FeesSection type={type}
        </div>
      );
    }
);

jest.mock(
  '@/components/Structure/Tarification/DiscountsSection',
  () =>
    function MockDiscountsSection({ type }) {
      return (
        <div data-testid={`discounts-section-type-${type}`}>
          DiscountsSection type={type}
        </div>
      );
    }
);

jest.mock(
  '@/components/PaymentPlanSelector',
  () =>
    function MockPaymentPlanSelector({ type }) {
      return (
        <div data-testid={`payment-plan-type-${type}`}>
          PaymentPlanSelector type={type}
        </div>
      );
    }
);

jest.mock(
  '@/components/PaymentModeSelector',
  () =>
    function MockPaymentModeSelector({ type }) {
      return (
        <div data-testid={`payment-mode-type-${type}`}>
          PaymentModeSelector type={type}
        </div>
      );
    }
);

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',
}));

const defaultProps = {
  title: "Frais d'inscription",
  fees: [],
  setFees: jest.fn(),
  discounts: [],
  setDiscounts: jest.fn(),
  paymentPlans: [],
  setPaymentPlans: jest.fn(),
  paymentModes: [],
  setPaymentModes: jest.fn(),
  type: 0,
  handleCreate: jest.fn(),
  handleEdit: jest.fn(),
  handleDelete: jest.fn(),
  onDiscountDelete: jest.fn(),
};

describe('FeeTypeSection - type inscription (type=0)', () => {
  it('affiche le titre passé en props', () => {
    render(<FeeTypeSection {...defaultProps} />);
    expect(screen.getByText("Frais d'inscription")).toBeInTheDocument();
  });

  it('rend le composant FeesSection avec le bon type', () => {
    render(<FeeTypeSection {...defaultProps} />);
    expect(screen.getByTestId('fees-section-type-0')).toBeInTheDocument();
  });

  it('rend le composant DiscountsSection avec le bon type', () => {
    render(<FeeTypeSection {...defaultProps} />);
    expect(screen.getByTestId('discounts-section-type-0')).toBeInTheDocument();
  });

  it('rend le composant PaymentPlanSelector avec le bon type', () => {
    render(<FeeTypeSection {...defaultProps} />);
    expect(screen.getByTestId('payment-plan-type-0')).toBeInTheDocument();
  });

  it('rend le composant PaymentModeSelector avec le bon type', () => {
    render(<FeeTypeSection {...defaultProps} />);
    expect(screen.getByTestId('payment-mode-type-0')).toBeInTheDocument();
  });
});

describe('FeeTypeSection - type scolarité (type=1)', () => {
  const tuitionProps = {
    ...defaultProps,
    title: 'Frais de scolarité',
    type: 1,
  };

  it('affiche le titre "Frais de scolarité"', () => {
    render(<FeeTypeSection {...tuitionProps} />);
    expect(screen.getByText('Frais de scolarité')).toBeInTheDocument();
  });

  it('rend tous les sous-composants avec type=1', () => {
    render(<FeeTypeSection {...tuitionProps} />);
    expect(screen.getByTestId('fees-section-type-1')).toBeInTheDocument();
    expect(screen.getByTestId('discounts-section-type-1')).toBeInTheDocument();
    expect(screen.getByTestId('payment-plan-type-1')).toBeInTheDocument();
    expect(screen.getByTestId('payment-mode-type-1')).toBeInTheDocument();
  });
});

describe('FeeTypeSection - transmission des handlers', () => {
  it('passe les fonctions handleCreate, handleEdit, handleDelete aux sous-composants', () => {
    const handleCreate = jest.fn();
    const handleEdit = jest.fn();
    const handleDelete = jest.fn();

    // On vérifie que le composant se rend sans erreur avec les handlers
    expect(() =>
      render(
        <FeeTypeSection
          {...defaultProps}
          handleCreate={handleCreate}
          handleEdit={handleEdit}
          handleDelete={handleDelete}
        />
      )
    ).not.toThrow();
  });
});
