'use client';

/**
 * Logout Page Component
 */

import { useState } from 'react';
import { useRouter } from 'next/navigation';

export default function LogoutPage({ onLogout, onSetSessionCookie }) {
  const [error, setError] = useState('');
  const [success, setSuccess] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const router = useRouter();

  const handleLogout = async () => {
    setError('');
    setSuccess('');
    setIsLoading(true);

    try {
      // Call the logout action if provided
      if (onLogout) {
        const result = await onLogout();
        
        if (result && !result.success) {
          setError(result.error || 'Échec de la déconnexion');
          setIsLoading(false);
          return;
        }
      }

      // Clear session cookie if provided
      if (onSetSessionCookie) {
        await onSetSessionCookie('', { expires: new Date(0) });
      }

      // Show success message
      setSuccess('Vous avez été déconnecté. Redirection...');
      setIsLoading(false);
      
      // Wait for user to see the success message, then redirect
      setTimeout(() => {
        router.push('/');
      }, 100);
      
    } catch (err) {
      console.error('Logout error:', err);
      setError('Une erreur inattendue s\'est produite lors de la déconnexion');
      setIsLoading(false);
    }
  };

  return (
    <div className="group bg-white/80 dark:bg-neutral-900/40 backdrop-blur-sm border border-neutral-200/80 dark:border-neutral-800/50 rounded-2xl px-4 py-6 md:px-6 md:py-8 hover:bg-neutral-50/90 hover:border-neutral-300/80 dark:hover:bg-neutral-900/50 dark:hover:border-neutral-700/50 transition-all duration-300 w-full max-w-md">
      {/* Header */}
      <div className="text-center mb-6">
        <h1 className="text-2xl font-bold text-neutral-900 dark:text-white mb-2">
          Prêt à vous déconnecter ?
        </h1>
        <p className="text-sm text-neutral-600 dark:text-neutral-400">
          Cela mettra fin à votre session et vous déconnectera de votre compte.
        </p>
      </div>

      {/* Success Message */}
      {success && (
        <div className="mb-4 p-3 bg-green-50 border border-green-200 rounded-lg dark:bg-green-500/10 dark:border-green-500/20">
          <div className="flex items-center space-x-2">
            <div className="w-1.5 h-1.5 bg-green-500 rounded-full flex-shrink-0"></div>
            <span className="text-xs text-green-700 dark:text-green-400">{success}</span>
          </div>
        </div>
      )}

      {/* Error Message */}
      {error && (
        <div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg dark:bg-red-500/10 dark:border-red-500/20">
          <div className="flex items-center space-x-2">
            <div className="w-1.5 h-1.5 bg-red-500 rounded-full flex-shrink-0"></div>
            <span className="text-xs text-red-700 dark:text-red-400">{error}</span>
          </div>
        </div>
      )}

      {/* Logout Button */}
      <div className="flex flex-col gap-4">
        <button
          type="button"
          onClick={handleLogout}
          disabled={isLoading || success}
          className="cursor-pointer w-full bg-red-600 text-white mt-2 py-2.5 px-4 rounded-lg text-sm font-medium hover:bg-red-700 disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-200 focus:outline-none focus:ring-1 focus:ring-red-500/20 dark:focus:ring-red-400/30"
        >
          {isLoading ? (
            <div className="flex items-center justify-center space-x-2">
              <div className="w-3.5 h-3.5 border-2 border-white/30 border-t-white rounded-full animate-spin"></div>
              <span>Déconnexion en cours...</span>
            </div>
          ) : (
            'Se déconnecter'
          )}
        </button>
      </div>

      {/* Cancel Link */}
      <div className="mt-6 text-center">
          <span className="text-sm text-neutral-600 dark:text-neutral-400">Vous avez changé d'avis ? </span>
          <a
            href="/"
            className="text-sm text-neutral-900 hover:text-neutral-600 font-medium transition-colors duration-200 dark:text-white dark:hover:text-neutral-300"
          >
            Retour
          </a>
        </div>
    </div>
  );
}
