import React, { useState, useEffect } from 'react';
import {
  Trash2,
  Edit3,
  Check,
  X,
  EyeOff,
  Eye,
  CreditCard,
  Plus,
} from 'lucide-react';
import Table from '@/components/Table';
import Popup from '@/components/Popup';
import CheckBox from '@/components/Form/CheckBox';
import InputText from '@/components/Form/InputText';
import logger from '@/utils/logger';
import SectionHeader from '@/components/SectionHeader';
import { useEstablishment } from '@/context/EstablishmentContext';
import EmptyState from '@/components/EmptyState';

const FEE_TYPE_LABELS = { 0: 'Inscription', 1: 'Scolarité' };

/**
 * @param {boolean} [unified=false] - true : tableau mixte inscription+scolarité avec colonne TYPE.
 *   Dans ce cas, `fees` contient les frais des deux types et `handleCreate`/`handleEdit`/`handleDelete`
 *   sont des fonctions (url, data, setter) déjà partiellement appliquées par le parent.
 */
const FeesSection = ({
  fees,
  setFees,
  handleCreate,
  handleEdit,
  handleDelete,
  type,
  unified = false,
  subscriptionMode = false,
  selectedFees,
  handleFeeSelection,
}) => {
  const [editingFee, setEditingFee] = useState(null);
  const [newFee, setNewFee] = useState(null);
  const [formData, setFormData] = useState({});
  const [localErrors, setLocalErrors] = useState({});
  const [popupVisible, setPopupVisible] = useState(false);
  const [popupMessage, setPopupMessage] = useState('');
  const [removePopupVisible, setRemovePopupVisible] = useState(false);
  const [removePopupMessage, setRemovePopupMessage] = useState('');
  const [removePopupOnConfirm, setRemovePopupOnConfirm] = useState(() => {});
  const ITEMS_PER_PAGE = 10;
  const [currentPage, setCurrentPage] = useState(1);
  useEffect(() => {
    setCurrentPage(1);
  }, [fees]);
  useEffect(() => {
    if (newFee) setCurrentPage(1);
  }, [newFee]);
  const totalPages = Math.ceil(fees.length / ITEMS_PER_PAGE);
  const pagedFees = fees.slice(
    (currentPage - 1) * ITEMS_PER_PAGE,
    currentPage * ITEMS_PER_PAGE
  );
  // En mode unifié, le type effectif est celui du frais ou celui du formulaire de création
  const labelTypeFrais = (feeType) =>
    feeType === 0 ? "Frais d'inscription" : 'Frais de scolarité';

  const { selectedEstablishmentId } = useEstablishment();
  // Récupération des messages d'erreur
  const getError = (field) => {
    return localErrors?.[field]?.[0];
  };

  const handleAddFee = () => {
    setNewFee({
      id: Date.now(),
      name: '',
      base_amount: '',
      description: '',
      discounts: [],
      type: unified ? 0 : type,
      establishment: selectedEstablishmentId,
    });
  };

  const handleRemoveFee = (id) => {
    return handleDelete(id)
      .then(() => {
        setFees((prevFees) => prevFees.filter((fee) => fee.id !== id));
      })
      .catch((error) => {
        logger.error(error);
      });
  };

  const handleSaveNewFee = () => {
    if (newFee.name && newFee.base_amount) {
      const feeData = {
        ...newFee,
        establishment: selectedEstablishmentId,
      };

      handleCreate(feeData)
        .then((createdFee) => {
          setFees([createdFee, ...fees]);
          setNewFee(null);
          setLocalErrors({});
        })
        .catch((error) => {
          logger.error('Error:', error.message);
          if (error.details) {
            logger.error('Form errors:', error.details);
            setLocalErrors(error.details);
          }
        });
    } else {
      setPopupMessage('Tous les champs doivent être remplis et valides');
      setPopupVisible(true);
    }
  };

  const handleUpdateFee = (id, updatedFee) => {
    if (updatedFee.name && updatedFee.base_amount) {
      handleEdit(id, updatedFee)
        .then((updated) => {
          setFees(fees.map((fee) => (fee.id === id ? updated : fee)));
          setEditingFee(null);
          setLocalErrors({});
        })
        .catch((error) => {
          logger.error('Error:', error.message);
          if (error.details) {
            logger.error('Form errors:', error.details);
            setLocalErrors(error.details);
          }
        });
    } else {
      setPopupMessage('Tous les champs doivent être remplis et valides');
      setPopupVisible(true);
    }
  };

  const handleToggleActive = (id, isActive) => {
    const fee = fees.find((fee) => fee.id === id);
    if (!fee) return;

    const updatedData = {
      is_active: !isActive,
      discounts: fee.discounts,
    };

    handleEdit(id, updatedData)
      .then(() => {
        setFees((prevFees) =>
          prevFees.map((fee) =>
            fee.id === id ? { ...fee, is_active: !isActive } : fee
          )
        );
      })
      .catch((error) => {
        logger.error(error);
      });
  };

  const handleChange = (e) => {
    const { name, value } = e.target;
    let parsedValue = value;
    if (name === 'discounts') {
      parsedValue = value.split(',').map((v) => parseInt(v, 10));
    }
    if (editingFee) {
      setFormData((prevData) => ({
        ...prevData,
        [name]: parsedValue,
      }));
    } else if (newFee) {
      setNewFee((prevData) => ({
        ...prevData,
        [name]: parsedValue,
      }));
    }
  };

  const renderInputField = (field, value, onChange, placeholder) => (
    <div className="flex justify-center space-x-2">
      <div className="w-full max-w-xs">
        <InputText
          name={field}
          type={field === 'base_amount' ? 'number' : 'text'}
          value={value}
          onChange={onChange}
          placeholder={placeholder}
          errorMsg={getError(field)}
        />
      </div>
    </div>
  );

  const renderFeeCell = (fee, column) => {
    const isEditing = editingFee === fee.id;
    const isCreating = newFee && newFee.id === fee.id;
    const currentData = isEditing ? formData : newFee;

    if (isEditing || isCreating) {
      switch (column) {
        case 'NOM':
          return renderInputField(
            'name',
            currentData.name,
            handleChange,
            'Nom des frais'
          );
        case 'MONTANT':
          return renderInputField(
            'base_amount',
            currentData.base_amount,
            handleChange,
            'Montant de base'
          );
        case 'DESCRIPTION':
          return renderInputField(
            'description',
            currentData.description,
            handleChange,
            'Description'
          );
        case 'TYPE':
          return (
            <select
              className="border rounded px-2 py-1 text-sm"
              value={currentData.type}
              onChange={(e) => {
                const val = parseInt(e.target.value, 10);
                if (isEditing) setFormData((p) => ({ ...p, type: val }));
                else setNewFee((p) => ({ ...p, type: val }));
              }}
            >
              <option value={0}>Inscription</option>
              <option value={1}>Scolarité</option>
            </select>
          );
        case 'ACTIONS':
          return (
            <div className="flex justify-center space-x-2">
              <button
                type="button"
                onClick={() =>
                  isEditing
                    ? handleUpdateFee(editingFee, formData)
                    : handleSaveNewFee()
                }
                className="text-primary hover:text-secondary"
              >
                <Check className="w-5 h-5" />
              </button>
              <button
                type="button"
                onClick={() =>
                  isEditing ? setEditingFee(null) : setNewFee(null)
                }
                className="text-red-500 hover:text-red-700"
              >
                <X className="w-5 h-5" />
              </button>
            </div>
          );
        default:
          return null;
      }
    } else {
      const feeLabel = labelTypeFrais(fee.type);
      switch (column) {
        case 'NOM':
          return fee.name;
        case 'MONTANT':
          return fee.base_amount + ' €';
        case 'MISE A JOUR':
          return fee.updated_at_formatted;
        case 'DESCRIPTION':
          return fee.description;
        case 'TYPE':
          return (
            <span
              className={`text-xs font-semibold px-2 py-1 rounded-full ${
                fee.type === 0
                  ? 'bg-blue-100 text-blue-700'
                  : 'bg-purple-100 text-purple-700'
              }`}
            >
              {FEE_TYPE_LABELS[fee.type]}
            </span>
          );
        case 'ACTIONS':
          return (
            <div className="flex justify-center space-x-2">
              <button
                type="button"
                onClick={() => handleToggleActive(fee.id, fee.is_active)}
                className={`text-${fee.is_active ? 'green' : 'orange'}-500 hover:text-${fee.is_active ? 'green' : 'orange'}-700`}
              >
                {fee.is_active ? (
                  <Eye className="w-5 h-5" />
                ) : (
                  <EyeOff className="w-5 h-5" />
                )}
              </button>
              <button
                type="button"
                onClick={() => setEditingFee(fee.id) || setFormData(fee)}
                className="text-blue-500 hover:text-blue-700"
              >
                <Edit3 className="w-5 h-5" />
              </button>
              <button
                type="button"
                onClick={() => {
                  setRemovePopupVisible(true);
                  setRemovePopupMessage(
                    `Attentions ! \nVous êtes sur le point de supprimer un ${feeLabel}.\nÊtes-vous sûr(e) de vouloir poursuivre l'opération ?`
                  );
                  setRemovePopupOnConfirm(() => () => {
                    handleRemoveFee(fee.id)
                      .then((data) => {
                        logger.debug('Success:', data);
                        setPopupMessage(feeLabel + ' correctement supprimé');
                        setPopupVisible(true);
                        setRemovePopupVisible(false);
                      })
                      .catch((error) => {
                        logger.error('Error archiving data:', error);
                        setPopupMessage(
                          'Erreur lors de la suppression du ' + feeLabel
                        );
                        setPopupVisible(true);
                        setRemovePopupVisible(false);
                      });
                  });
                }}
                className="text-red-500 hover:text-red-700"
              >
                <Trash2 className="w-5 h-5" />
              </button>
            </div>
          );
        case '':
          return (
            <div className="flex justify-center">
              <CheckBox
                item={fee}
                formData={{ selectedFees }}
                handleChange={() => handleFeeSelection(fee.id)}
                fieldName="selectedFees"
              />
            </div>
          );
        default:
          return null;
      }
    }
  };

  const columns = subscriptionMode
    ? [
        { name: 'NOM', label: 'Nom' },
        { name: 'DESCRIPTION', label: 'Description' },
        { name: 'MONTANT', label: 'Montant de base' },
        ...(unified ? [{ name: 'TYPE', label: 'Type' }] : []),
        { name: '', label: 'Sélection' },
      ]
    : [
        { name: 'NOM', label: 'Nom' },
        { name: 'MONTANT', label: 'Montant de base' },
        { name: 'DESCRIPTION', label: 'Description' },
        ...(unified ? [{ name: 'TYPE', label: 'Type' }] : []),
        { name: 'MISE A JOUR', label: 'Date mise à jour' },
        { name: 'ACTIONS', label: 'Actions' },
      ];

  const emptyMessage = (
    <EmptyState
      icon={CreditCard}
      title={`Aucun frais ${type === 0 ? "d'inscription" : 'de scolarité'} enregistré`}
      description={`Commencez par ajouter vos frais ${type === 0 ? "d'inscription" : 'de scolarité'} pour les proposer lors des inscriptions.`}
      actionLabel={!subscriptionMode ? 'Ajouter un frais' : undefined}
      onAction={!subscriptionMode ? handleAddFee : undefined}
      actionIcon={!subscriptionMode ? Plus : undefined}
    />
  );

  return (
    <div className="space-y-4">
      {!subscriptionMode && (pagedFees.length > 0 || newFee) && (
        <SectionHeader
          icon={CreditCard}
          title={`Liste des frais ${type === 0 ? "d'inscription" : 'de scolarité'}`}
          description={`Gérez vos frais ${type === 0 ? "d'inscription" : 'de scolarité'}.`}
          button={true}
          onClick={handleAddFee}
        />
      )}
      <Table
        data={newFee ? [newFee, ...pagedFees] : pagedFees}
        columns={columns}
        renderCell={renderFeeCell}
        emptyMessage={emptyMessage}
        itemsPerPage={ITEMS_PER_PAGE}
        currentPage={currentPage}
        totalPages={totalPages}
        onPageChange={setCurrentPage}
      />
      <Popup
        isOpen={popupVisible}
        setIsOpen={setPopupVisible}
        message={popupMessage}
        onConfirm={() => setPopupVisible(false)}
        onCancel={() => setPopupVisible(false)}
        uniqueConfirmButton={true}
        popupClassName="w-full max-w-xs sm:max-w-md"
      />
      <Popup
        isOpen={removePopupVisible}
        setIsOpen={setRemovePopupVisible}
        message={removePopupMessage}
        onConfirm={removePopupOnConfirm}
        onCancel={() => setRemovePopupVisible(false)}
        popupClassName="w-full max-w-xs sm:max-w-md"
      />
    </div>
  );
};

export default FeesSection;
