"use client";

import { useState } from "react";
import Link from "next/link";
import { Activity, Loader2, Mail } from "lucide-react";
import { cn } from "@/lib/utils";

export default function VerifyEmailPage() {
  const [loading, setLoading] = useState(false);
  const [message, setMessage] = useState("");
  const [error, setError] = useState("");

  async function handleResend() {
    setLoading(true);
    setMessage("");
    setError("");

    try {
      const res = await fetch("/api/auth/resend-verification", {
        method: "POST",
      });

      if (!res.ok) {
        const data: { error?: string } = await res.json();
        setError(data.error ?? "Failed to resend email");
        setLoading(false);
        return;
      }

      setMessage("Verification email sent! Check your inbox.");
    } catch {
      setError("Something went wrong. Please try again.");
    } finally {
      setLoading(false);
    }
  }

  return (
    <div className="space-y-8">
      <div className="flex flex-col items-center gap-4">
        <div className="w-12 h-12 rounded-xl bg-gradient-to-br from-emerald-400 to-emerald-600 flex items-center justify-center shadow-lg shadow-emerald-500/20">
          <Activity className="w-6 h-6 text-white" />
        </div>
        <div className="text-center">
          <h1 className="text-2xl font-bold text-neutral-100">
            Check your email
          </h1>
          <p className="mt-1 text-sm text-neutral-400">
            We sent a verification link to your inbox
          </p>
        </div>
      </div>

      <div className="rounded-xl bg-neutral-900 border border-neutral-800 p-6 space-y-4">
        <div className="flex items-center justify-center">
          <div className="w-16 h-16 rounded-full bg-emerald-500/10 border border-emerald-500/20 flex items-center justify-center">
            <Mail className="w-8 h-8 text-emerald-400" />
          </div>
        </div>
        <p className="text-sm text-neutral-400 text-center leading-relaxed">
          Click the link in the email to verify your account. The link expires
          in 24 hours.
        </p>
      </div>

      {message && (
        <div className="rounded-lg bg-emerald-500/10 border border-emerald-500/20 px-4 py-3">
          <p className="text-sm text-emerald-400">{message}</p>
        </div>
      )}

      {error && (
        <div className="rounded-lg bg-red-500/10 border border-red-500/20 px-4 py-3">
          <p className="text-sm text-red-400">{error}</p>
        </div>
      )}

      <button
        onClick={handleResend}
        disabled={loading}
        className={cn(
          "w-full flex items-center justify-center gap-2 px-4 py-2.5 rounded-lg text-sm font-semibold transition-colors",
          loading
            ? "bg-emerald-500/50 text-neutral-950/50 cursor-not-allowed"
            : "bg-emerald-500 hover:bg-emerald-400 text-neutral-950"
        )}
      >
        {loading && <Loader2 className="w-4 h-4 animate-spin" />}
        {loading ? "Sending..." : "Resend verification email"}
      </button>

      <p className="text-center text-sm text-neutral-400">
        Already verified?{" "}
        <Link
          href="/login"
          className="text-emerald-400 hover:text-emerald-300 font-medium transition-colors"
        >
          Sign in
        </Link>
      </p>
    </div>
  );
}
