"use client";

import React, { useState } from "react";
import Link from "next/link";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import { Award, ArrowLeft, CheckCircle2, Loader2 } from "lucide-react";
import { apiClient } from "@/lib/api-client";
import { toast } from "sonner";

const forgotSchema = z.object({
  identifier: z.string().min(1, "Please enter your registered email or username"),
});

type ForgotFormData = z.infer<typeof forgotSchema>;

export default function ForgotPasswordPage() {
  const [isSubmitted, setIsSubmitted] = useState(false);
  const [isLoading, setIsLoading] = useState(false);

  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm<ForgotFormData>({
    resolver: zodResolver(forgotSchema),
    defaultValues: {
      identifier: "",
    },
  });

  const onSubmit = async (data: ForgotFormData) => {
    setIsLoading(true);
    try {
      await apiClient.post("/auth/forgot-password", {
        email: data.identifier,
      });
      setIsSubmitted(true);
      toast.success("Recovery instructions sent if account exists.");
    } catch {
      // Show confirmation regardless to prevent account enumeration
      setIsSubmitted(true);
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <div className="min-h-screen w-full flex flex-col md:flex-row bg-[#F8FAFC] text-slate-800 font-sans selection:bg-[#C8A45D] selection:text-white">
      {/* ============================================================ */}
      {/* LEFT HALF: Campus Architecture Background & Golden Branding */}
      {/* ============================================================ */}
      <div className="relative w-full md:w-1/2 min-h-[340px] md:min-h-screen flex flex-col items-center justify-start p-8 md:p-12 lg:p-16 overflow-hidden bg-[#0B1F33]">
        {/* Campus Heritage Image */}
        <div
          className="absolute inset-0 bg-cover bg-center transition-transform duration-700 scale-105"
          style={{ backgroundImage: `url('/images/ihm_campus_bg.jpg')` }}
        />

        {/* Deep Navy Gradient Overlay */}
        <div className="absolute inset-0 bg-gradient-to-b from-[#0B1F33]/90 via-[#0B1F33]/65 to-[#0B1F33]/30 pointer-events-none" />

        {/* Content Overlaid on Campus Image */}
        <div className="relative z-10 w-full flex flex-col items-center text-center mt-6 md:mt-12">
          {/* Golden Institutional Emblem */}
          <div className="w-16 h-16 sm:w-20 sm:h-20 mb-4 flex items-center justify-center">
            <div className="w-16 h-16 sm:w-20 sm:h-20 rounded-full border-2 border-[#C8A45D] bg-[#0B1F33]/80 backdrop-blur-xs flex items-center justify-center shadow-lg">
              <Award className="w-9 h-9 sm:w-11 sm:h-11 text-[#C8A45D]" />
            </div>
          </div>

          {/* Institution Header Typography */}
          <div className="space-y-1">
            <h2 className="text-sm sm:text-base font-bold uppercase tracking-[0.25em] text-white">
              Institute of
            </h2>
            <h1 className="text-2xl sm:text-3xl lg:text-4xl font-extrabold uppercase tracking-[0.18em] text-white leading-tight">
              Hotel Management
            </h1>
            <h3 className="text-lg sm:text-xl font-bold uppercase tracking-[0.3em] text-white pt-1">
              Kolkata
            </h3>
          </div>

          {/* Golden Motto */}
          <div className="mt-5 pt-3">
            <span className="text-xs sm:text-sm font-semibold tracking-[0.35em] uppercase text-[#C8A45D]">
              Learn • Practice • Excel
            </span>
          </div>
        </div>
      </div>

      {/* ============================================================ */}
      {/* RIGHT HALF: Clean Minimal Floating Forgot Password Card     */}
      {/* ============================================================ */}
      <div className="w-full md:w-1/2 flex flex-col justify-center items-center p-6 sm:p-10 lg:p-14 bg-[#F8FAFC]">
        <div className="w-full max-w-[420px] bg-white rounded-2xl border border-slate-200/90 shadow-lg p-8 sm:p-10 text-center">
          {/* Top Emblem Seal Icon matching the screenshot */}
          <div className="flex flex-col items-center mb-5">
            <div className="w-12 h-12 rounded-full border-2 border-[#C8A45D]/60 bg-slate-50 flex items-center justify-center mb-2 shadow-2xs">
              <Award className="w-6 h-6 text-slate-800" />
            </div>
            <div className="w-12 h-0.5 bg-[#C8A45D]" />
          </div>

          {isSubmitted ? (
            <div className="py-4">
              <div className="w-12 h-12 rounded-full bg-emerald-50 text-emerald-600 flex items-center justify-center mx-auto mb-3 border border-emerald-200">
                <CheckCircle2 className="w-6 h-6" />
              </div>
              <h2 className="text-xl font-bold text-slate-900 tracking-tight mb-1.5">
                Check Your Email
              </h2>
              <p className="text-xs text-slate-500 mb-6 leading-relaxed">
                If an active account exists with that identifier, a secure password reset link has been dispatched to your registered address.
              </p>
              <Link
                href="/login"
                className="inline-flex items-center justify-center gap-1.5 w-full py-2.5 px-4 rounded-lg bg-[#C8A45D] hover:bg-[#b8934c] text-slate-950 font-bold text-xs uppercase tracking-wider transition-colors shadow-sm"
              >
                <ArrowLeft className="w-3.5 h-3.5" />
                Back to Login
              </Link>
            </div>
          ) : (
            <>
              {/* Card Title & Instructions */}
              <div className="mb-6">
                <h2 className="text-2xl font-bold text-slate-900 tracking-tight">
                  Reset Your Password
                </h2>
                <p className="text-xs text-slate-500 mt-1.5 leading-relaxed max-w-xs mx-auto">
                  Enter your registered email or username and we&apos;ll send you a reset link.
                </p>
              </div>

              {/* Form */}
              <form onSubmit={handleSubmit(onSubmit)} className="space-y-5 text-left">
                <div>
                  <label className="block text-xs font-bold text-slate-800 mb-1.5">
                    Email / Username
                  </label>
                  <input
                    type="text"
                    {...register("identifier")}
                    disabled={isLoading}
                    placeholder="Email or username"
                    className="w-full px-3.5 py-2.5 text-xs sm:text-sm rounded-lg border border-slate-300 bg-white placeholder:text-slate-400 focus:outline-none focus:border-[#C8A45D] focus:ring-1 focus:ring-[#C8A45D]/40 transition-colors"
                  />
                  {errors.identifier && (
                    <p className="text-[11px] text-red-600 mt-1">
                      {errors.identifier.message}
                    </p>
                  )}
                </div>

                {/* Send Reset Link Button (Champagne Gold #C8A45D) */}
                <button
                  type="submit"
                  disabled={isLoading}
                  className="w-full py-2.5 px-4 rounded-lg bg-[#C8A45D] hover:bg-[#b8934c] active:bg-[#a8823b] text-slate-950 font-bold text-sm tracking-wide shadow-sm hover:shadow transition-all disabled:opacity-50 flex items-center justify-center gap-2 cursor-pointer"
                >
                  {isLoading ? (
                    <>
                      <Loader2 className="w-4 h-4 animate-spin text-slate-950" />
                      <span>Sending Link...</span>
                    </>
                  ) : (
                    <span>Send Reset Link</span>
                  )}
                </button>
              </form>

              {/* Back to Login link */}
              <div className="pt-6 mt-4 border-t border-slate-100">
                <Link
                  href="/login"
                  className="text-xs font-semibold text-slate-600 hover:text-slate-950 transition-colors"
                >
                  Back to Login
                </Link>
              </div>
            </>
          )}
        </div>
      </div>
    </div>
  );
}
