'use client';

import { useState, useEffect } from 'react';
import Link from 'next/link';
import { Mail, Shield, CheckCircle2, AlertTriangle, ArrowRight } 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';

export default function VerifyEmailPage() {
  const [token, setToken] = useState('');
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [success, setSuccess] = useState(false);

  useEffect(() => {
    const params = new URLSearchParams(window.location.search);
    const t = params.get('token');
    if (t) setToken(t);
  }, []);

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setError(null);
    setSuccess(false);
    setLoading(true);
    try {
      await authService.verifyEmail({ token });
      setSuccess(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">
          <div className="mx-auto mb-4 flex h-12 w-12 items-center justify-center rounded-2xl bg-gradient-to-br from-primary-500 to-indigo-600 text-white">
            <Shield className="h-6 w-6" />
          </div>
          <h1 className="text-2xl font-bold text-ink">Verify Your Email</h1>
          <p className="mt-1 text-sm text-ink-muted">Enter the verification code sent to your email</p>
        </div>

        <Card className="card-hover">
          <CardHeader>
            <CardTitle className="text-xl">Email Verification</CardTitle>
            <CardDescription className="mt-1">
              We sent a verification code to your email address. Please enter it below.
            </CardDescription>
          </CardHeader>

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

          {success ? (
            <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">Email Verified!</p>
                  <p className="mt-1 text-emerald-600/80 dark:text-emerald-300/80">
                    Your email has been successfully verified. You can now access all features.
                  </p>
                </div>
              </div>
              <div className="mt-4">
                <a href="/login" className="block">
                  <Button className="w-full">Continue to Login</Button>
                </a>
              </div>
            </CardContent>
          ) : (
            <form onSubmit={handleSubmit}>
              <CardContent className="space-y-4">
                <div>
                  <label htmlFor="token" className="mb-1.5 block text-sm font-medium text-ink">Verification Token</label>
                  <Input
                    id="token"
                    placeholder="Paste your verification token"
                    value={token}
                    onChange={(e) => setToken(e.target.value)}
                    required
                  />
                  <p className="mt-1.5 text-xs text-ink-faint">
                    Check your email for the verification link and paste the token here.
                  </p>
                </div>
                <Button type="submit" className="w-full" loading={loading}>
                  {loading ? 'Verifying...' : 'Verify Email'}
                  {!loading && <ArrowRight className="h-4 w-4" />}
                </Button>
              </CardContent>
            </form>
          )}

          {!success && (
            <CardFooter className="justify-center">
              <p className="text-sm text-ink-muted">
                Didn&apos;t receive the code?{' '}
                <button className="font-semibold text-primary-600 hover:underline">Resend</button>
              </p>
            </CardFooter>
          )}
        </Card>
      </div>
    </div>
  );
}
