"use client";

import { useState } from "react";
import { useParams } from "next/navigation";
import { Mail, Loader2, CheckCircle2, AlertCircle } from "lucide-react";

export default function ClientLoginPage() {
  const params = useParams();
  const brandId = params.brandId as string;
  const [email, setEmail] = useState("");
  const [status, setStatus] = useState<"idle" | "loading" | "sent" | "error">("idle");

  async function submit(e: React.FormEvent) {
    e.preventDefault();
    if (!email.trim()) return;
    setStatus("loading");
    try {
      const res = await fetch("/api/client-portal/auth/send-magic-link", {
        method: "POST", headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ email: email.trim(), brandId }),
      });
      setStatus(res.ok ? "sent" : "error");
    } catch { setStatus("error"); }
  }

  return (
    <div className="min-h-screen flex items-center justify-center bg-slate-50 px-4">
      <div className="w-full max-w-sm">
        <div className="bg-white rounded-2xl border border-slate-200 shadow-sm overflow-hidden">
          <div className="px-8 pt-8 pb-4 text-center">
            <div className="w-12 h-12 rounded-2xl bg-brand-50 border border-brand-200 flex items-center justify-center mx-auto mb-4">
              <Mail className="w-6 h-6 text-brand-600" />
            </div>
            <h1 className="text-xl font-bold text-slate-900 mb-1">Client Portal</h1>
            <p className="text-sm text-slate-500">Enter your email to receive a login link</p>
          </div>

          {status === "sent" ? (
            <div className="px-8 pb-8 text-center">
              <CheckCircle2 className="w-10 h-10 text-green-500 mx-auto mb-3" />
              <p className="text-sm font-semibold text-slate-900 mb-1">Check your email</p>
              <p className="text-xs text-slate-500">We sent a login link to <span className="font-medium text-slate-700">{email}</span>. It expires in 24 hours.</p>
              <button onClick={() => setStatus("idle")} className="mt-4 text-xs text-brand-600 font-semibold hover:underline">Use a different email</button>
            </div>
          ) : (
            <form onSubmit={submit} className="px-8 pb-8 space-y-4">
              <div>
                <label className="block text-[11px] font-semibold text-slate-600 mb-1">Email address</label>
                <input
                  type="email" required value={email} onChange={(e) => setEmail(e.target.value)} placeholder="you@company.com"
                  className="w-full px-4 py-2.5 text-sm border border-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-brand-500/40 focus:border-brand-500"
                />
              </div>
              <button type="submit" disabled={status === "loading" || !email.trim()} className="w-full flex items-center justify-center gap-2 px-4 py-2.5 bg-brand-600 text-white text-sm font-semibold rounded-lg hover:bg-brand-700 disabled:opacity-60">
                {status === "loading" ? <Loader2 className="w-4 h-4 animate-spin" /> : <Mail className="w-4 h-4" />}
                {status === "loading" ? "Sending…" : "Send Login Link"}
              </button>
              {status === "error" && (
                <div className="flex items-center gap-2 text-xs text-red-600"><AlertCircle className="w-3.5 h-3.5" /> Something went wrong. Please try again.</div>
              )}
            </form>
          )}
        </div>
        <p className="text-center text-[10px] text-slate-400 mt-4">No password needed — we&apos;ll email you a secure link</p>
      </div>
    </div>
  );
}
