"use client";
import { Eye, EyeOff } from "lucide-react";
import Link from "next/link";
import { useState } from "react";
import { useAuthStore } from "../store/useAuthStore";
import { useRouter } from "next/navigation";

export default function LoginPage() {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [showPassword, setShowPassword] = useState(false);

  const router = useRouter();

  const { login, loading, error } = useAuthStore();

  const handleSubmit = async (
    e: React.FormEvent<HTMLFormElement>
  ) => {
    e.preventDefault();

    const success = await login({
      email,
      password,
    });

    if (success) {
      const userType = useAuthStore.getState().user?.user_type;

      if (userType === 2) {
        router.push("/receptionist-dashboard");
      } else {
        router.push("/dashboard");
      }
    }
  };

  return (
    <main className="min-h-screen bg-white flex flex-col md:flex-row font-sans">
      {/* Left Side: Branding/Visual (Hidden on mobile) */}
      <div className="hidden md:flex md:w-1/2 bg-[#FBEAF0]/30 items-center justify-center p-12">
        <div className="max-w-md">
          <div className="flex items-center gap-2 mb-8">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#993556" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
              <path d="M9 3H5a2 2 0 0 0-2 2v4m6-6h10a2 2 0 0 1 2 2v4M9 3v18m0 0h10a2 2 0 0 0 2-2V9M9 21H5a2 2 0 0 1-2-2V9m0 0h18" />
            </svg>
            <span className="text-xl font-bold text-gray-900">RxH</span>
          </div>
          <h1 className="text-4xl font-serif font-bold text-gray-900 mb-6 leading-tight">
            Welcome back to your <br />
            <span className="text-[#993556]">modern practice.</span>
          </h1>
          <p className="text-gray-500 leading-relaxed">
            Manage your prescriptions, patient records, and clinic workflow with ease and security.
          </p>
        </div>
      </div>

      {/* Right Side: Login Form */}
      <div className="flex-1 flex items-center justify-center p-8 md:p-16">
        <div className="w-full max-w-[400px]">
          {/* Logo for Mobile */}
          <div className="md:hidden flex items-center gap-2 mb-10">
            <div className="w-8 h-8 rounded-lg bg-[#FBEAF0] flex items-center justify-center">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#993556" strokeWidth="2.5">
                <path d="M9 3H5a2 2 0 0 0-2 2v4m6-6h10a2 2 0 0 1 2 2v4M9 3v18m0 0h10a2 2 0 0 0 2-2V9M9 21H5a2 2 0 0 1-2-2V9m0 0h18" />
              </svg>
            </div>
            <span className="text-lg font-bold text-gray-900">RxH</span>
          </div>

          <div className="mb-10">
            <h2 className="text-3xl font-bold text-[#993556] mb-2">Login</h2>
            <p className="text-gray-500 text-sm">Enter your credentials to access the dashboard.</p>
          </div>

          <form onSubmit={handleSubmit} className="space-y-5">
            <div>
              <label className="block text-[13px] font-semibold text-gray-700 mb-1.5 uppercase tracking-wider">
                Email Address
              </label>
              <input
                type="email"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                placeholder="doctor@example.com"
                className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-[#993556] focus:ring-1 focus:ring-[#993556] outline-none transition-all text-sm text-gray-600"
                required
              />
            </div>

            <div>
              <div className="flex justify-between mb-1.5">
                <label className="block text-[13px] font-semibold text-gray-700 uppercase tracking-wider">
                  Password
                </label>

                <Link
                  href="#"
                  className="text-[12px] font-medium text-[#993556] hover:underline"
                >
                  Forgot Password?
                </Link>
              </div>

              <div className="relative">
                <input
                  type={showPassword ? "text" : "password"}
                  value={password}
                  onChange={(e) => setPassword(e.target.value)}
                  placeholder="••••••••"
                  className="w-full px-4 py-3 pr-12 rounded-xl border border-gray-200 focus:border-[#993556] focus:ring-1 focus:ring-[#993556] outline-none transition-all text-sm text-gray-600"
                  required
                />

                <button
                  type="button"
                  onClick={() => setShowPassword(!showPassword)}
                  className="absolute right-4 top-1/2 -translate-y-1/2 text-gray-400 hover:text-[#993556] transition-colors"
                >
                  {showPassword ? <Eye size={20} /> : <EyeOff size={20} />}
                </button>
              </div>
            </div>

            {error && (
              <div className="rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-600">
                {error}
              </div>
            )}

            <button
              type="submit"
              disabled={loading}
              className="w-full bg-[#993556] text-white font-semibold py-3.5 rounded-xl hover:bg-[#802a46] transition-colors shadow-lg shadow-[#993556]/10 mt-4 disabled:opacity-50"
            >
              {loading ? "Signing In..." : "Sign In"}
            </button>
          </form>

          <p className="text-center text-sm text-gray-500 mt-8">
            Don&apos;t have an account?{" "}
            <Link href="/register" className="text-[#993556] font-bold hover:underline">
              Register
            </Link>
          </p>
        </div>
      </div>
    </main>
  );
}