import React, { useState } from 'react';
import { Button } from '../../ui/button';
import { Input } from '../../ui/input';
import { Label } from '../../ui/label';
import { XerticaLogo } from '../../brand/xertica-logo';
import { ImageWithFallback } from '../../figma/ImageWithFallback';
import { LanguageSelector } from '../../brand/language-selector';
import { ArrowLeft, Lock } from 'lucide-react';
import { useNavigate } from 'react-router-dom';

/**
 * Forgot Password Page component.
 *
 * @description
 * A screen that allows users to request a password reset link via email.
 * Includes a hero image side-panel (desktop) and alternative social login
 * options similar to the main Login page.
 *
 * @ai-rules
 * 1. Navigation: Successfully submitting the email triggers a navigation to `/verify-email`.
 * 2. Visuals: Keeps consistency with `LoginPage` in terms of layout and branding.
 */
export function ForgotPasswordPage() {
  const navigate = useNavigate();
  const [email, setEmail] = useState('');
  const [isLoading, setIsLoading] = useState(false);

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

    // Simulate email sending
    await new Promise(resolve => setTimeout(resolve, 1500));

    // Navigate to verification screen
    navigate('/verify-email', { state: { email } });

    setIsLoading(false);
  };

  const handleSocialLogin = (_provider: string) => {
    // Wire your SSO/social provider integration here
  };

  return (
    <div className="h-screen w-full flex overflow-y-auto">
      {/* Left side - Full background image */}
      <div className="hidden lg:flex lg:flex-1 relative overflow-hidden">
        {/* Background image filling all space */}
        <ImageWithFallback
          src="https://images.unsplash.com/photo-1557804506-669a67965ba0?w=1200&h=800&fit=crop&auto=format"
          alt="Segurança e tecnologia"
          className="absolute inset-0 w-full h-full object-cover"
        />

        {/* Gradient overlay */}
        <div className="absolute inset-0 bg-[image:var(--gradient-diagonal)] opacity-80" />
      </div>

      {/* Right side - Form */}
      <div className="flex-1 flex items-center justify-center px-4 sm:px-6 lg:px-8 lg:flex-none lg:w-1/2 relative bg-muted">
        {/* Language selector in the top right corner */}
        <div className="absolute top-4 right-4 z-20">
          <LanguageSelector variant="minimal" showIcon={false} />
        </div>

        {/* Mobile background gradient */}
        <div className="absolute inset-0 lg:hidden bg-[image:var(--gradient-diagonal)] opacity-10 dark:opacity-5" />

        <div className="w-full max-w-sm space-y-6 relative z-10">
          {/* Form header */}
          <div className="text-center">
            <div className="flex items-center justify-center mb-4">
              <XerticaLogo
                className="h-12 w-auto text-primary dark:text-foreground"
                variant="theme"
              />
            </div>
            <h2 className="text-sm text-muted-foreground">Recuperar senha</h2>
            <p className="mt-2 text-muted-foreground">
              Digite seu e-mail e enviaremos as instruções para redefinir sua senha
            </p>
          </div>

          {/* Form */}
          <form className="space-y-6" onSubmit={handleSubmit}>
            <div className="form-group space-y-2">
              <Label htmlFor="email" className="form-label">
                E-mail
              </Label>
              <Input
                id="email"
                name="email"
                type="email"
                required
                className="w-full"
                placeholder="seu@email.com"
                value={email}
                onChange={e => setEmail(e.target.value)}
              />
            </div>

            <div className="space-y-3">
              <Button type="submit" className="w-full" disabled={isLoading}>
                {isLoading ? 'Enviando...' : 'Enviar instruções'}
              </Button>

              <Button
                type="button"
                onClick={() => navigate('/login')}
                variant="outline"
                className="w-full text-muted-foreground hover:text-foreground"
              >
                <ArrowLeft className="w-4 h-4 mr-2" />
                Voltar para o login
              </Button>
            </div>
          </form>

          {/* Divider */}
          <div className="relative">
            <div className="absolute inset-0 flex items-center">
              <div className="w-full border-t border-border"></div>
            </div>
            <div className="relative flex justify-center text-sm">
              <span className="bg-muted px-2 text-muted-foreground">ou continue com</span>
            </div>
          </div>

          {/* Social/SSO login options */}
          <div className="space-y-3">
            {/* Google */}
            <Button
              type="button"
              variant="outline"
              className="w-full justify-center"
              onClick={() => handleSocialLogin('Google')}
            >
              <svg className="w-5 h-5 mr-2" viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg">
                <path
                  fill="#EA4335"
                  d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"
                ></path>
                <path
                  fill="#4285F4"
                  d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"
                ></path>
                <path
                  fill="#FBBC05"
                  d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"
                ></path>
                <path
                  fill="#34A853"
                  d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"
                ></path>
                <path fill="none" d="M0 0h48v48H0z"></path>
              </svg>
              <span>Entrar com Google</span>
            </Button>

            {/* MT Login */}
            <Button
              type="button"
              variant="outline"
              className="w-full justify-center"
              onClick={() => handleSocialLogin('MT Login')}
            >
              <Lock className="w-5 h-5 mr-2 text-[var(--chart-4)]" />
              <span>Entrar com MT Login</span>
            </Button>

            {/* gov.br */}
            <Button
              type="button"
              variant="outline"
              className="w-full justify-center font-normal"
              onClick={() => handleSocialLogin('gov.br')}
            >
              <span>
                Entrar com <strong className="font-semibold">gov.br</strong>
              </span>
            </Button>
          </div>
        </div>
      </div>
    </div>
  );
}
