import { CheckCircle, AlertCircle, MailCheck } from "lucide-react";
import Link from "next/link";
import prisma from "@/lib/prisma";

interface PageProps {
    searchParams: Promise<{ token?: string }>;
}

export default async function VerifyEmailPage({ searchParams }: PageProps) {
    const { token } = await searchParams;

    if (!token) {
        return <VerifyLayout icon="error" title="Invalid link" message="No verification token was found. Please check your email link or request a new one." cta={{ label: "Back to login", href: "/login" }} />;
    }

    // Find user with this token
    const user = await prisma.user.findFirst({
        where: { verification_token: token },
    });

    if (!user) {
        // Token already used or invalid — check if already verified
        const alreadyVerified = await prisma.user.findFirst({ where: { email_verified: { not: null } } });
        return (
            <VerifyLayout
                icon="info"
                title="Link already used"
                message="This verification link has expired or already been used. If your email is verified, you can sign in."
                cta={{ label: "Sign in", href: "/login" }}
            />
        );
    }

    if (user.email_verified) {
        return <VerifyLayout icon="success" title="Already verified" message="Your email is already verified. You can sign in to your account." cta={{ label: "Sign in", href: "/login" }} />;
    }

    // Check token expiry
    if (user.verification_token_expiry && user.verification_token_expiry < new Date()) {
        return (
            <VerifyLayout
                icon="error"
                title="Link expired"
                message="Your verification link has expired. Please request a new one from the login page."
                cta={{ label: "Sign in", href: "/login" }}
            />
        );
    }

    // Mark verified
    await prisma.user.update({
        where: { id: user.id },
        data: { email_verified: new Date(), verification_token: null, verification_token_expiry: null },
    });

    return (
        <VerifyLayout
            icon="success"
            title="Email verified!"
            message={`Welcome to ProtenConnect! Your account (${user.email}) is now verified. You can sign in and start exploring.`}
            cta={{ label: "Sign in to your account", href: "/login" }}
        />
    );
}

// ─── Layout helper ────────────────────────────────────────────────────────────
function VerifyLayout({ icon, title, message, cta }: {
    icon: "success" | "error" | "info";
    title: string;
    message: string;
    cta: { label: string; href: string };
}) {
    const iconMap = {
        success: { bg: "bg-emerald-500/15 border-emerald-500/30", color: "text-emerald-400", Icon: CheckCircle },
        error: { bg: "bg-red-500/15 border-red-500/30", color: "text-red-400", Icon: AlertCircle },
        info: { bg: "bg-blue-500/15 border-blue-500/30", color: "text-blue-400", Icon: MailCheck },
    };
    const { bg, color, Icon } = iconMap[icon];

    return (
        <div className="min-h-screen flex flex-col items-center justify-center px-4"
            style={{ background: "radial-gradient(ellipse 80% 60% at 50% -10%, #0d1f3c 0%, #0a0a0f 70%)" }}>

            <div className="flex items-center gap-2.5 mb-8">
                <div className="w-9 h-9 rounded-xl bg-[#0070f3] flex items-center justify-center shadow-lg shadow-[#0070f3]/30">
                    <svg viewBox="0 0 24 24" fill="white" className="w-5 h-5"><path d="M13 10V3L4 14h7v7l9-11h-7z" /></svg>
                </div>
                <span className="text-xl font-bold text-white">Proten<span className="text-[#0070f3]">Connect</span></span>
            </div>

            <div className="w-full max-w-[420px] bg-white/[0.04] border border-white/10 rounded-2xl p-8 backdrop-blur-xl shadow-2xl text-center">
                <div className={`w-16 h-16 rounded-full ${bg} border flex items-center justify-center mx-auto mb-5`}>
                    <Icon className={`w-8 h-8 ${color}`} />
                </div>
                <h1 className="text-2xl font-bold text-white mb-2">{title}</h1>
                <p className="text-sm text-slate-400 leading-relaxed mb-8">{message}</p>
                <Link href={cta.href}
                    className="inline-block px-6 py-3 rounded-xl bg-[#0070f3] text-white font-semibold hover:bg-[#0060d3] transition-all shadow-lg shadow-[#0070f3]/25">
                    {cta.label}
                </Link>
            </div>
        </div>
    );
}
