'use client';

import { useState } from 'react';
import Link from 'next/link';
import { Mail, ArrowRight, CheckCircle2, Shield, HelpCircle } from 'lucide-react';
import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { authService } from '@/services/auth.service';
import { getErrorMessage } from '@/lib/errors';
import { useSystemConfig } from '@/hooks/useSystemConfig';

export default function ForgotPasswordPage() {
  const sysConfig = useSystemConfig();
  const [email, setEmail] = useState('');
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [sent, setSent] = useState(false);

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setError(null);
    setLoading(true);
    try {
      await authService.forgotPassword({ email });
      setSent(true);
    } catch (err) {
      setError(getErrorMessage(err));
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-surface-sunken px-4 py-10">
      <div className="pointer-events-none absolute inset-0">
        <div className="absolute -left-40 -top-40 h-96 w-96 rounded-full bg-primary-500/20 blur-3xl" />
        <div className="absolute -bottom-40 -right-40 h-96 w-96 rounded-full bg-indigo-500/20 blur-3xl" />
        <div className="absolute inset-0 bg-grid opacity-40" />
      </div>

      <div className="relative mx-auto w-full max-w-md">
        <div className="mb-6 text-center">
          <Link href="/" className="inline-flex items-center gap-2 mb-4">
            <div className="flex h-10 w-10 items-center justify-center rounded-xl bg-gradient-to-br from-primary-500 to-indigo-600 text-white">
              <Shield className="h-5 w-5" />
            </div>
            <span className="text-xl font-bold text-ink">{sysConfig.projectName}</span>
          </Link>
        </div>

        <Card className="card-hover">
          <CardHeader>
            <CardTitle className="text-2xl">Forgot password</CardTitle>
            <CardDescription className="mt-2">
              Enter your email and we&apos;ll send you a link to reset your password.
            </CardDescription>
          </CardHeader>

          {error && (
            <div className="mx-5 mb-3 rounded-xl border border-red-500/30 bg-red-500/10 p-3 text-sm text-red-600 dark:text-red-300">
              {error}
            </div>
          )}

          {sent ? (
            <CardContent>
              <div className="flex items-start gap-3 rounded-xl border border-emerald-500/30 bg-emerald-500/10 p-4 text-sm text-emerald-600 dark:text-emerald-300">
                <CheckCircle2 className="mt-0.5 h-5 w-5 shrink-0" />
                <div>
                  <p className="font-medium">Reset link sent</p>
                  <p className="mt-1 text-emerald-600/80 dark:text-emerald-300/80">
                    If an account exists for {email}, you&apos;ll receive a password reset link shortly.
                  </p>
                </div>
              </div>
              <div className="mt-4 rounded-xl border border-line bg-surface-raised p-4">
                <div className="flex items-start gap-2">
                  <HelpCircle className="h-4 w-4 text-ink-faint mt-0.5 shrink-0" />
                  <p className="text-xs text-ink-muted">
                    Didn&apos;t receive the email? Check your spam folder or{' '}
                    <button onClick={() => setSent(false)} className="text-primary-600 hover:underline">try again</button>.
                  </p>
                </div>
              </div>
            </CardContent>
          ) : (
            <form onSubmit={handleSubmit}>
              <CardContent className="space-y-4">
                <div>
                  <label htmlFor="email" className="mb-1.5 block text-sm font-medium text-ink">Email</label>
                  <Input
                    id="email"
                    type="email"
                    placeholder="you@example.com"
                    icon={<Mail className="h-4 w-4" />}
                    value={email}
                    onChange={(e) => setEmail(e.target.value)}
                    required
                    autoComplete="email"
                  />
                </div>
                <Button type="submit" className="w-full" loading={loading}>
                  {loading ? 'Sending...' : 'Send Reset Link'}
                  {!loading && <ArrowRight className="h-4 w-4" />}
                </Button>
              </CardContent>
            </form>
          )}

          <CardFooter className="justify-center">
            <p className="text-sm text-ink-muted">
              Remembered?{' '}
              <Link href="/login" className="font-semibold text-primary-600 hover:underline">Back to sign in</Link>
            </p>
          </CardFooter>
        </Card>

        <div className="mt-6 text-center">
          <Link href="/" className="text-sm text-ink-faint hover:text-ink">
            &larr; Back to home
          </Link>
        </div>
      </div>
    </div>
  );
}
