import React from 'react';
import FeesSection from '@/components/Structure/Tarification/FeesSection';
import DiscountsSection from '@/components/Structure/Tarification/DiscountsSection';
import PaymentPlanSelector from '@/components/PaymentPlanSelector';
import PaymentModeSelector from '@/components/PaymentModeSelector';
import {
  BE_SCHOOL_FEES_URL,
  BE_SCHOOL_DISCOUNTS_URL,
  BE_SCHOOL_PAYMENT_PLANS_URL,
  BE_SCHOOL_PAYMENT_MODES_URL,
} from '@/utils/Url';

/**
 * Bloc complet de gestion des frais pour un type donné (inscription ou scolarité).
 * Regroupe : liste des frais, réductions, plans et modes de paiement.
 *
 * @param {string}   title            - Titre affiché dans le séparateur de section
 * @param {Array}    fees             - Liste des frais du type
 * @param {Function} setFees          - Setter des frais
 * @param {Array}    discounts        - Liste des réductions du type
 * @param {Function} setDiscounts     - Setter des réductions
 * @param {Array}    paymentPlans     - Plans de paiement du type
 * @param {Function} setPaymentPlans  - Setter des plans de paiement
 * @param {Array}    paymentModes     - Modes de paiement du type
 * @param {Function} setPaymentModes  - Setter des modes de paiement
 * @param {number}   type             - 0 = inscription, 1 = scolarité
 * @param {Function} handleCreate     - (url, newData, setter) => Promise
 * @param {Function} handleEdit       - (url, id, updatedData, setter) => Promise
 * @param {Function} handleDelete     - (url, id, setter) => Promise
 * @param {Function} onDiscountDelete - Callback invoqué après suppression d'une réduction
 */
const FeeTypeSection = ({
  title,
  fees,
  setFees,
  discounts,
  setDiscounts,
  paymentPlans,
  setPaymentPlans,
  paymentModes,
  setPaymentModes,
  type,
  handleCreate,
  handleEdit,
  handleDelete,
  onDiscountDelete,
}) => {
  return (
    <div className="space-y-6">
      {/* Frais */}
      <div className="bg-white rounded-md border border-gray-200 shadow-sm p-6">
        <FeesSection
          fees={fees}
          setFees={setFees}
          discounts={discounts}
          handleCreate={(newData) =>
            handleCreate(`${BE_SCHOOL_FEES_URL}`, newData, setFees)
          }
          handleEdit={(id, updatedData) =>
            handleEdit(`${BE_SCHOOL_FEES_URL}`, id, updatedData, setFees)
          }
          handleDelete={(id) =>
            handleDelete(`${BE_SCHOOL_FEES_URL}`, id, setFees)
          }
          type={type}
        />
      </div>

      {/* Réductions */}
      <div className="bg-white rounded-md border border-gray-200 shadow-sm p-6">
        <DiscountsSection
          discounts={discounts}
          setDiscounts={setDiscounts}
          handleCreate={(newData) =>
            handleCreate(`${BE_SCHOOL_DISCOUNTS_URL}`, newData, setDiscounts)
          }
          handleEdit={(id, updatedData) =>
            handleEdit(
              `${BE_SCHOOL_DISCOUNTS_URL}`,
              id,
              updatedData,
              setDiscounts
            )
          }
          handleDelete={(id) =>
            handleDelete(`${BE_SCHOOL_DISCOUNTS_URL}`, id, setDiscounts)
          }
          onDiscountDelete={onDiscountDelete}
          type={type}
        />
      </div>

      {/* Plans et modes de paiement */}
      <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
        <div className="bg-white rounded-md border border-gray-200 shadow-sm p-6">
          <PaymentPlanSelector
            paymentPlans={paymentPlans}
            setPaymentPlans={setPaymentPlans}
            handleCreate={(newData) =>
              handleCreate(
                `${BE_SCHOOL_PAYMENT_PLANS_URL}`,
                newData,
                setPaymentPlans
              )
            }
            handleDelete={(id) =>
              handleDelete(
                `${BE_SCHOOL_PAYMENT_PLANS_URL}`,
                id,
                setPaymentPlans
              )
            }
            type={type}
          />
        </div>
        <div className="bg-white rounded-md border border-gray-200 shadow-sm p-6">
          <PaymentModeSelector
            paymentModes={paymentModes}
            setPaymentModes={setPaymentModes}
            handleCreate={(newData) =>
              handleCreate(
                `${BE_SCHOOL_PAYMENT_MODES_URL}`,
                newData,
                setPaymentModes
              )
            }
            handleDelete={(id) =>
              handleDelete(
                `${BE_SCHOOL_PAYMENT_MODES_URL}`,
                id,
                setPaymentModes
              )
            }
            type={type}
          />
        </div>
      </div>
    </div>
  );
};

export default FeeTypeSection;
