'use client';

import { useState, useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useNotification } from './NotificationProvider';
import useSWR from 'swr';
import ConfirmModal from './ConfirmModal';

interface User {
  id: string;
  email: string;
  name: string | null;
  photoUrl?: string | null;
  roleId: string | null;
  role: {
    id: string;
    name: string;
    description: string | null;
  } | null;
}

interface AdherentOption {
  id: string;
  type: 'situation' | 'prescripteur' | 'facturation';
  value: string;
  order: number;
}

interface OptionsByType {
  situation: AdherentOption[];
  prescripteur: AdherentOption[];
  facturation: AdherentOption[];
}

const fetcher = (url: string) => fetch(url).then((res) => res.json());

export default function ParametresContent() {
  const { showNotification } = useNotification();
  const { data: user } = useSWR<User>('/api/auth/me', fetcher);
  const [activeConfigSection, setActiveConfigSection] = useState<'adherents' | null>(null);
  const [options, setOptions] = useState<OptionsByType>({
    situation: [],
    prescripteur: [],
    facturation: [],
  });
  const [loading, setLoading] = useState(true);
  const [editingId, setEditingId] = useState<string | null>(null);
  const [editingValue, setEditingValue] = useState('');
  const [newValue, setNewValue] = useState<Record<string, string>>({
    situation: '',
    prescripteur: '',
    facturation: '',
  });
  const [confirmDeleteModal, setConfirmDeleteModal] = useState<{
    show: boolean;
    id: string | null;
  } | null>(null);

  useEffect(() => {
    fetchOptions();
  }, []);

  const fetchOptions = async () => {
    setLoading(true);
    try {
      const response = await fetch('/api/settings/adherent-options');
      if (response.ok) {
        const data = await response.json();
        setOptions({
          situation: data.situation || [],
          prescripteur: data.prescripteur || [],
          facturation: data.facturation || [],
        });
      }
    } catch (error) {
      console.error('Erreur lors du chargement des options:', error);
      showNotification('Erreur lors du chargement des options', 'error');
    } finally {
      setLoading(false);
    }
  };

  const handleAdd = async (type: 'situation' | 'prescripteur' | 'facturation') => {
    const value = newValue[type].trim();
    if (!value) {
      showNotification('Veuillez entrer une valeur', 'error');
      return;
    }

    try {
      const response = await fetch('/api/settings/adherent-options', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          type,
          value,
        }),
      });

      if (response.ok) {
        setNewValue({ ...newValue, [type]: '' });
        await fetchOptions();
        showNotification('Option ajoutée avec succès', 'success');
      } else {
        const error = await response.json();
        showNotification(error.error || 'Erreur lors de l\'ajout', 'error');
      }
    } catch (error) {
      console.error('Erreur:', error);
      showNotification('Erreur lors de l\'ajout', 'error');
    }
  };

  const handleEdit = (option: AdherentOption) => {
    setEditingId(option.id);
    setEditingValue(option.value);
  };

  const handleSaveEdit = async (id: string, type: 'situation' | 'prescripteur' | 'facturation') => {
    const value = editingValue.trim();
    if (!value) {
      showNotification('Veuillez entrer une valeur', 'error');
      return;
    }

    try {
      const response = await fetch(`/api/settings/adherent-options/${id}`, {
        method: 'PUT',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ value }),
      });

      if (response.ok) {
        setEditingId(null);
        setEditingValue('');
        await fetchOptions();
        showNotification('Option modifiée avec succès', 'success');
      } else {
        const error = await response.json();
        showNotification(error.error || 'Erreur lors de la modification', 'error');
      }
    } catch (error) {
      console.error('Erreur:', error);
      showNotification('Erreur lors de la modification', 'error');
    }
  };

  const handleCancelEdit = () => {
    setEditingId(null);
    setEditingValue('');
  };

  const handleDelete = (id: string) => {
    setConfirmDeleteModal({
      show: true,
      id,
    });
  };

  const confirmDelete = async () => {
    if (!confirmDeleteModal?.id) return;

    try {
      const response = await fetch(`/api/settings/adherent-options/${confirmDeleteModal.id}`, {
        method: 'DELETE',
      });

      if (response.ok) {
        await fetchOptions();
        showNotification('Option supprimée avec succès', 'success');
      } else {
        const error = await response.json();
        showNotification(error.error || 'Erreur lors de la suppression', 'error');
      }
    } catch (error) {
      console.error('Erreur:', error);
      showNotification('Erreur lors de la suppression', 'error');
    } finally {
      setConfirmDeleteModal(null);
    }
  };

  const OptionCard = ({
    type,
    label,
    icon,
  }: {
    type: 'situation' | 'prescripteur' | 'facturation';
    label: string;
    icon: React.ReactNode;
  }) => {
    const typeOptions = options[type] || [];

    return (
      <div className="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
        <div className="flex items-center gap-3 mb-6">
          <div className="w-10 h-10 rounded-lg bg-lblue/10 flex items-center justify-center">
            {icon}
          </div>
          <h3 className="text-lg font-bold text-gray-900">{label}</h3>
          <span className="ml-auto px-3 py-1 text-xs font-semibold rounded-full bg-gray-100 text-gray-600">
            {typeOptions.length} option{typeOptions.length > 1 ? 's' : ''}
          </span>
        </div>

        {/* Liste des options */}
        <div className="space-y-2 mb-4">
          {loading ? (
            <div className="text-center py-4 text-gray-500">Chargement...</div>
          ) : typeOptions.length === 0 ? (
            <div className="text-center py-4 text-gray-500 text-sm">
              Aucune option configurée
            </div>
          ) : (
            typeOptions.map((option) => (
              <div
                key={option.id}
                className="flex items-center gap-3 p-3 bg-gray-50 rounded-lg border border-gray-200 hover:border-gray-300 transition-colors"
              >
                {editingId === option.id ? (
                  <>
                    <input
                      type="text"
                      value={editingValue}
                      onChange={(e) => setEditingValue(e.target.value)}
                      className="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-lblue focus:border-transparent"
                      onKeyDown={(e) => {
                        if (e.key === 'Enter') {
                          handleSaveEdit(option.id, type);
                        } else if (e.key === 'Escape') {
                          handleCancelEdit();
                        }
                      }}
                      autoFocus
                    />
                    <button
                      onClick={() => handleSaveEdit(option.id, type)}
                      className="px-3 py-2 text-sm font-medium text-white bg-lgreen rounded-lg hover:bg-dgreen transition-colors"
                    >
                      Enregistrer
                    </button>
                    <button
                      onClick={handleCancelEdit}
                      className="px-3 py-2 text-sm font-medium text-gray-700 bg-gray-200 rounded-lg hover:bg-gray-300 transition-colors"
                    >
                      Annuler
                    </button>
                  </>
                ) : (
                  <>
                    <span className="flex-1 text-sm font-medium text-gray-900">
                      {option.value}
                    </span>
                    <button
                      onClick={() => handleEdit(option)}
                      className="px-3 py-1.5 text-xs font-medium text-lblue bg-lblue/10 rounded-lg hover:bg-lblue/20 transition-colors"
                    >
                      Modifier
                    </button>
                    <button
                      onClick={() => handleDelete(option.id)}
                      className="px-3 py-1.5 text-xs font-medium text-red-600 bg-red-50 rounded-lg hover:bg-red-100 transition-colors"
                    >
                      Supprimer
                    </button>
                  </>
                )}
              </div>
            ))
          )}
        </div>

        {/* Formulaire d'ajout */}
        <div className="flex items-center gap-2 pt-4 border-t border-gray-200">
          <input
            type="text"
            value={newValue[type]}
            onChange={(e) => setNewValue({ ...newValue, [type]: e.target.value })}
            placeholder={`Ajouter une nouvelle ${label.toLowerCase()}`}
            className="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-lblue focus:border-transparent"
            onKeyDown={(e) => {
              if (e.key === 'Enter') {
                handleAdd(type);
              }
            }}
          />
          <button
            onClick={() => handleAdd(type)}
            className="px-4 py-2 text-sm font-medium text-white bg-lblue rounded-lg hover:bg-dblue transition-colors flex items-center gap-2"
          >
            <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
            </svg>
            Ajouter
          </button>
        </div>
      </div>
    );
  };

  const getUserInitials = () => {
    if (user?.name) {
      const names = user.name.split(' ');
      if (names.length >= 2) {
        return `${names[0].charAt(0)}${names[1].charAt(0)}`.toUpperCase();
      }
      return user.name.charAt(0).toUpperCase();
    }
    return user?.email?.charAt(0).toUpperCase() || 'U';
  };

  const router = useRouter();

  return (
    <div className="p-6">
      <div className="mb-8">
        <h1 className="text-3xl font-bold text-gray-900 mb-2">Paramètres</h1>
        <p className="text-sm text-gray-600">
          Gérez votre profil et configurez la plateforme
        </p>
      </div>

      <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
        {/* Colonne de gauche - Profil/Compte */}
        <div>
          <div className="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <div className="text-center mb-6">
              {user?.photoUrl ? (
                <img
                  src={user.photoUrl}
                  alt={user.name || 'Utilisateur'}
                  className="w-20 h-20 rounded-full object-cover mx-auto mb-4 shadow-lg border-4 border-white"
                />
              ) : (
                <div className="w-20 h-20 rounded-full bg-gradient-to-br from-lblue to-dblue flex items-center justify-center mx-auto mb-4 shadow-lg">
                  <span className="text-white text-2xl font-bold">{getUserInitials()}</span>
                </div>
              )}
              <h2 className="text-xl font-bold text-gray-900 mb-1">
                {user?.name || 'Utilisateur'}
              </h2>
              <p className="text-sm text-gray-500">{user?.email}</p>
            </div>

            <button
              onClick={() => router.push('/dashboard/parametres/compte')}
              className="w-full px-4 py-3 bg-gray-50 hover:bg-gray-100 rounded-lg text-left transition-colors border border-gray-200"
            >
              <div className="flex items-center gap-3">
                <svg className="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
                </svg>
                <span className="font-medium text-gray-900">Modifier le compte</span>
              </div>
            </button>
          </div>
        </div>

        {/* Colonne de droite - Configuration de la plateforme (visible uniquement pour Admin) */}
        {(user?.role?.name === 'Admin' || user?.role?.name === 'Administrateur') && (
          <div>
            <button
              onClick={() => router.push('/dashboard/parametres/configuration')}
              className="w-full bg-white rounded-xl shadow-sm border border-gray-100 p-6 hover:shadow-md hover:border-lblue transition-all text-left group"
            >
              <div className="flex items-center gap-4">
                <div className="w-16 h-16 rounded-xl bg-lblue/10 flex items-center justify-center group-hover:bg-lblue/20 transition-colors">
                  <svg className="w-8 h-8 text-lblue" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                  </svg>
                </div>
                <div className="flex-1">
                  <h3 className="text-xl font-bold text-gray-900 mb-1 group-hover:text-lblue transition-colors">
                    Configuration
                  </h3>
                  <p className="text-sm text-gray-600">
                    Configurez les paramètres de la plateforme
                  </p>
                </div>
                <svg className="w-5 h-5 text-gray-400 group-hover:text-lblue transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                </svg>
              </div>
            </button>
          </div>
        )}
      </div>

      {/* Modal de confirmation de suppression */}
      {confirmDeleteModal && (
        <ConfirmModal
          isOpen={confirmDeleteModal.show}
          title="Supprimer l'option"
          message="Êtes-vous sûr de vouloir supprimer cette option ?"
          confirmText="Supprimer"
          cancelText="Annuler"
          confirmColor="danger"
          onConfirm={confirmDelete}
          onCancel={() => setConfirmDeleteModal(null)}
        />
      )}
    </div>
  );
}
