/**
 * Login Page - Xpeditis
 *
 * Modern split-screen login page with:
 * - Left side: Login form with social authentication
 * - Right side: Brand features and visual elements
 */

'use client';

import { useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import Image from 'next/image';
import { login } from '@/lib/api';

export default function LoginPage() {
  const router = useRouter();
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const [rememberMe, setRememberMe] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState('');

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setError('');
    setIsLoading(true);

    try {
      await login({ email, password });
      router.push('/dashboard');
    } catch (err: any) {
      setError(err.message || 'Identifiants incorrects');
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <div className="min-h-screen flex">
      {/* Left Side - Form */}
      <div className="w-full lg:w-1/2 flex flex-col justify-center px-8 sm:px-12 lg:px-16 xl:px-24 bg-white">
        <div className="max-w-md w-full mx-auto">
          {/* Logo */}
          <div className="mb-10">
            <Link href="/">
              <Image
                src="/assets/logos/logo-black.svg"
                alt="Xpeditis"
                width={50}
                height={60}
                priority
                className="h-auto"
              />
            </Link>
          </div>

          {/* Header */}
          <div className="mb-8">
            <h1 className="text-h1 text-brand-navy mb-2">Connexion</h1>
            <p className="text-body text-neutral-600">
              Bienvenue ! Connectez-vous pour accéder à votre compte
            </p>
          </div>

          {/* Error Message */}
          {error && (
            <div className="mb-6 p-4 bg-red-50 border border-red-200 rounded-lg">
              <p className="text-body-sm text-red-800">{error}</p>
            </div>
          )}

          {/* Form */}
          <form onSubmit={handleSubmit} className="space-y-6">
            {/* Email */}
            <div>
              <label htmlFor="email" className="label">
                Adresse email
              </label>
              <input
                id="email"
                type="email"
                required
                value={email}
                onChange={e => setEmail(e.target.value)}
                className="input w-full"
                placeholder="votre.email@entreprise.com"
                autoComplete="email"
                disabled={isLoading}
              />
            </div>

            {/* Password */}
            <div>
              <label htmlFor="password" className="label">
                Mot de passe
              </label>
              <input
                id="password"
                type="password"
                required
                value={password}
                onChange={e => setPassword(e.target.value)}
                className="input w-full"
                placeholder="••••••••••"
                autoComplete="current-password"
                disabled={isLoading}
              />
            </div>

            {/* Remember Me & Forgot Password */}
            <div className="flex items-center justify-between">
              <label className="flex items-center cursor-pointer">
                <input
                  type="checkbox"
                  checked={rememberMe}
                  onChange={e => setRememberMe(e.target.checked)}
                  className="w-4 h-4 text-accent border-neutral-300 rounded focus:ring-accent focus:ring-2"
                  disabled={isLoading}
                />
                <span className="ml-2 text-body-sm text-neutral-700">Se souvenir de moi</span>
              </label>

              <Link href="/forgot-password" className="text-body-sm link">
                Mot de passe oublié ?
              </Link>
            </div>

            {/* Submit Button */}
            <button
              type="submit"
              disabled={isLoading}
              className="btn-primary w-full text-lg disabled:opacity-50 disabled:cursor-not-allowed"
            >
              {isLoading ? 'Connexion en cours...' : 'Se connecter'}
            </button>
          </form>

          {/* Sign Up Link */}
          <div className="mt-8 text-center">
            <p className="text-body text-neutral-600">
              Vous n'avez pas de compte ?{' '}
              <Link href="/register" className="link font-semibold">
                Créer un compte
              </Link>
            </p>
          </div>

          {/* Footer Links */}
          <div className="mt-8 pt-8 border-t border-neutral-200">
            <div className="flex flex-wrap justify-center gap-6 text-body-sm text-neutral-500">
              <Link href="/help" className="hover:text-accent transition-colors">
                Centre d'aide
              </Link>
              <Link href="/contact" className="hover:text-accent transition-colors">
                Contactez-nous
              </Link>
              <Link href="/privacy" className="hover:text-accent transition-colors">
                Confidentialité
              </Link>
              <Link href="/terms" className="hover:text-accent transition-colors">
                Conditions
              </Link>
            </div>
          </div>
        </div>
      </div>

      {/* Right Side - Brand Features */}
      <div className="hidden lg:block lg:w-1/2 relative bg-brand-navy">
        {/* Background Gradient */}
        <div className="absolute inset-0 bg-gradient-to-br from-brand-navy to-neutral-800 opacity-95"></div>

        {/* Content */}
        <div className="absolute inset-0 flex flex-col justify-center px-16 xl:px-24 text-white">
          <div className="max-w-xl">
            <h2 className="text-display-sm mb-6 text-white">Simplifiez votre fret maritime</h2>
            <p className="text-body-lg text-neutral-200 mb-12">
              Accédez à des tarifs en temps réel de plus de 50 compagnies maritimes. Réservez,
              suivez et gérez vos expéditions LCL en quelques clics.
            </p>

            {/* Features */}
            <div className="space-y-6">
              <div className="flex items-start">
                <div className="flex-shrink-0 w-12 h-12 bg-brand-turquoise rounded-lg flex items-center justify-center">
                  <svg
                    className="w-6 h-6 text-white"
                    fill="none"
                    stroke="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M13 10V3L4 14h7v7l9-11h-7z"
                    />
                  </svg>
                </div>
                <div className="ml-4">
                  <h3 className="text-h5 mb-1 text-white">Tarifs instantanés</h3>
                  <p className="text-body-sm text-neutral-300">
                    Comparez les prix de toutes les compagnies en temps réel
                  </p>
                </div>
              </div>

              <div className="flex items-start">
                <div className="flex-shrink-0 w-12 h-12 bg-brand-turquoise rounded-lg flex items-center justify-center">
                  <svg
                    className="w-6 h-6 text-white"
                    fill="none"
                    stroke="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
                    />
                  </svg>
                </div>
                <div className="ml-4">
                  <h3 className="text-h5 mb-1 text-white">Réservation simplifiée</h3>
                  <p className="text-body-sm text-neutral-300">
                    Réservez vos conteneurs en moins de 5 minutes
                  </p>
                </div>
              </div>

              <div className="flex items-start">
                <div className="flex-shrink-0 w-12 h-12 bg-brand-turquoise rounded-lg flex items-center justify-center">
                  <svg
                    className="w-6 h-6 text-white"
                    fill="none"
                    stroke="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      strokeWidth={2}
                      d="M17 8h2a2 2 0 012 2v6a2 2 0 01-2 2h-2v4l-4-4H9a1.994 1.994 0 01-1.414-.586m0 0L11 14h4a2 2 0 002-2V6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2v4l.586-.586z"
                    />
                  </svg>
                </div>
                <div className="ml-4">
                  <h3 className="text-h5 mb-1. text-white">Suivi en temps réel</h3>
                  <p className="text-body-sm text-neutral-300">
                    Suivez vos expéditions à chaque étape du voyage
                  </p>
                </div>
              </div>
            </div>

            {/* Stats */}
            <div className="grid grid-cols-3 gap-8 mt-12 pt-12 border-t border-neutral-700">
              <div>
                <div className="text-display-sm text-brand-turquoise">50+</div>
                <div className="text-body-sm text-neutral-300 mt-1">Compagnies</div>
              </div>
              <div>
                <div className="text-display-sm text-brand-turquoise">10k+</div>
                <div className="text-body-sm text-neutral-300 mt-1">Expéditions</div>
              </div>
              <div>
                <div className="text-display-sm text-brand-turquoise">99.5%</div>
                <div className="text-body-sm text-neutral-300 mt-1">Satisfaction</div>
              </div>
            </div>
          </div>
        </div>

        {/* Decorative Elements */}
        <div className="absolute bottom-0 right-0 opacity-10">
          <svg width="400" height="400" viewBox="0 0 400 400" fill="none">
            <circle
              cx="200"
              cy="200"
              r="150"
              stroke="currentColor"
              strokeWidth="2"
              className="text-white"
            />
            <circle
              cx="200"
              cy="200"
              r="100"
              stroke="currentColor"
              strokeWidth="2"
              className="text-white"
            />
            <circle
              cx="200"
              cy="200"
              r="50"
              stroke="currentColor"
              strokeWidth="2"
              className="text-white"
            />
          </svg>
        </div>
      </div>
    </div>
  );
}
