import React, { useState, useEffect } from 'react';
import {
  fetchRegistrationFileGroups,
  createRegistrationSchoolFileTemplate,
  cloneTemplate,
  generateToken,
} from '@/app/actions/registerFileGroupAction';
import { DocusealBuilder } from '@docuseal/react';
import logger from '@/utils/logger';
import MultiSelect from '@/components/MultiSelect'; // Import du composant MultiSelect
import { useCsrfToken } from '@/context/CsrfContext';
import { useEstablishment } from '@/context/EstablishmentContext';
import Popup from '@/components/Popup';

export default function FileUploadDocuSeal({
  handleCreateTemplateMaster,
  handleEditTemplateMaster,
  fileToEdit = null,
  onSuccess,
}) {
  const [groups, setGroups] = useState([]);
  const [token, setToken] = useState(null);
  const [templateMaster, setTemplateMaster] = useState(null);
  const [uploadedFileName, setUploadedFileName] = useState('');
  const [selectedGroups, setSelectedGroups] = useState([]);
  const [guardianDetails, setGuardianDetails] = useState([]);

  const [popupVisible, setPopupVisible] = useState(false);
  const [popupMessage, setPopupMessage] = useState('');

  const csrfToken = useCsrfToken();

  const { selectedEstablishmentId, user, apiDocuseal } = useEstablishment();

  useEffect(() => {
    fetchRegistrationFileGroups(selectedEstablishmentId).then((data) =>
      setGroups(data)
    );

    if (fileToEdit) {
      setUploadedFileName(fileToEdit.name || '');
      setSelectedGroups(fileToEdit.groups || []);
    }
  }, [fileToEdit]);

  useEffect(() => {
    if (!user && !user?.email) {
      return;
    }
    const id = fileToEdit ? fileToEdit.id : null;

    generateToken(user?.email, id, selectedEstablishmentId, apiDocuseal)
      .then((data) => {
        setToken(data.token);
      })
      .catch((error) =>
        logger.error('Erreur lors de la génération du token:', error)
      );
  }, [fileToEdit]);

  const handleGroupChange = (selectedGroups) => {
    setSelectedGroups(selectedGroups);

    const details = selectedGroups.flatMap((group) =>
      group.registration_forms.flatMap((form) =>
        form.guardians.map((guardian) => ({
          email: guardian.associated_profile_email,
          last_name: form.last_name,
          first_name: form.first_name,
          registration_form: form.student_id,
        }))
      )
    );
    setGuardianDetails(details); // Mettre à jour la variable d'état avec les détails des guardians
  };

  const handleLoad = (detail) => {
    logger.debug('loading template id : ', detail);
    setTemplateMaster(detail);
  };

  const handleUpload = (detail) => {
    logger.debug('Uploaded file detail:', detail);
    setUploadedFileName(detail.name);
  };

  const handleChange = (detail) => {
    logger.debug(detail);
    setUploadedFileName(detail.name);
  };

  const handleSubmit = (data) => {
    // Vérifier si au moins un champ a la propriété "required" à true
    const hasRequiredField = data.fields.some(
      (field) => field.required === true
    );

    if (!hasRequiredField) {
      setPopupMessage('Veuillez définir au moins un champ comme requis.');
      setPopupVisible(true);
      return;
    }

    const is_required = data.fields.length > 0;
    if (fileToEdit) {
      logger.debug('Modification du template master:', templateMaster?.id);
      handleEditTemplateMaster({
        name: uploadedFileName,
        group_ids: selectedGroups.map((group) => group.id),
        id: templateMaster?.id,
        is_required: is_required,
      });
    } else {
      logger.debug('Création du template master:', templateMaster?.id);
      handleCreateTemplateMaster({
        name: uploadedFileName,
        group_ids: selectedGroups.map((group) => group.id),
        id: templateMaster?.id,
        is_required: is_required,
      });

      guardianDetails.forEach((guardian, index) => {
        logger.debug('creation du clone avec required : ', is_required);
        cloneTemplate(templateMaster?.id, guardian.email, is_required, selectedEstablishmentId, apiDocuseal)
          .then((clonedDocument) => {
            // Sauvegarde des schoolFileTemplates clonés dans la base de données
            const data = {
              name: `${uploadedFileName}_${guardian.first_name}_${guardian.last_name}`,
              slug: clonedDocument.slug,
              id: clonedDocument.id,
              master: templateMaster?.id,
              registration_form: guardian.registration_form,
            };
            logger.debug('creation : ', data);
            createRegistrationSchoolFileTemplate(data, csrfToken)
              .then((response) => {
                logger.debug('Template enregistré avec succès:', response);
                onSuccess();
              })
              .catch((error) => {
                logger.error(
                  "Erreur lors de l'enregistrement du template:",
                  error
                );
              });

            // Logique pour envoyer chaque template au submitter
            logger.debug('Sending template to:', guardian.email);
          })
          .catch((error) => {
            logger.error('Error during cloning or sending:', error);
          });
      });
    }
  };

  return (
    <div className="h-full flex flex-col mt-4 space-y-6">
      <Popup
        isOpen={popupVisible}
        message={popupMessage}
        onConfirm={() => setPopupVisible(false)}
        onCancel={() => setPopupVisible(false)}
        uniqueConfirmButton={true}
      />
      {/* Contenu principal */}
      {selectedGroups.length === 0 ? (
        <div className="flex flex-col items-center justify-center h-full text-center space-y-6">
          {/* Description de l'étape */}
          <p className="text-gray-700 text-base font-medium mb-4">
            Étape 1 - Sélectionner au moins un dossier d&apos;inscription
          </p>

          {/* Section centrée pour la sélection des groupes */}
          <div className="bg-gray-50 p-8 rounded-lg shadow-md border border-gray-300 transform transition-transform hover:scale-105">
            <h3 className="text-xl font-semibold text-gray-800 mb-4 text-center">
              Dossiers d&apos;inscription
            </h3>
            <MultiSelect
              name="groups"
              label="Sélection dossier d'inscription"
              options={groups}
              selectedOptions={selectedGroups}
              onChange={handleGroupChange}
              errorMsg={null}
              className="bg-white rounded-md shadow-inner p-4"
            />
          </div>
        </div>
      ) : (
        <div className="grid grid-cols-10 gap-6 items-start">
          {/* Section Dossiers d'inscription repositionnée sur le côté */}
          <div className="col-span-2 bg-white p-4 rounded-lg shadow-md border border-gray-200">
            <h3 className="text-lg font-medium text-gray-800 mb-4">
              Dossiers d&apos;inscription
            </h3>
            <MultiSelect
              name="groups"
              label="Sélection dossier d'inscription"
              options={groups}
              selectedOptions={selectedGroups}
              onChange={handleGroupChange}
              errorMsg={null}
            />
          </div>

          {/* Zone de configuration des documents */}
          <div className="col-span-8 bg-white p-6 rounded-lg shadow-md border border-gray-200">
            {token && (
              <div className="h-full overflow-auto">
                {/* Description de l'étape */}
                <p className="text-gray-700 text-base font-medium mb-4">
                  Étape 2 - Sélectionnez un document
                </p>

                <DocusealBuilder
                  token={token}
                  headers={{
                    Authorization: `Bearer ${token}`,
                  }}
                  withSendButton={false}
                  withSignYourselfButton={false}
                  autosave={false}
                  withDocumentsList={false}
                  language={'fr'}
                  onLoad={handleLoad}
                  onUpload={handleUpload}
                  onChange={handleChange}
                  onSave={handleSubmit}
                  className="h-full overflow-auto"
                  style={{ maxHeight: '65vh' }}
                />
              </div>
            )}
          </div>
        </div>
      )}
    </div>
  );
}
