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

export default function LoginPage() {
  const router = useRouter();

  const { register, loading, error } = useAuthStore();
  const [fullName, setFullName] = useState("");
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [confirmPassword, setConfirmPassword] = useState("");
  const [showPassword, setShowPassword] = useState(false);
  const [showConfirmPassword, setShowConfirmPassword] = useState(false);

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

    if (password !== confirmPassword) {
      alert("Password and Confirm Password do not match");
      return;
    }

    const success = await register({
      fullname: fullName,
      email,
      password,
    });

    if (success) {
      router.push("/login");
    }
  };

  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">Register</h2>
            <p className="text-gray-500 text-sm">Create an account to get started.</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">
                Full Name
              </label>
              <input
                type="text"
                value={fullName}
                onChange={(e) => setFullName(e.target.value)}
                placeholder="John Doe"
                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>
              <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>
              <label className="block text-[13px] font-semibold text-gray-700 mb-1.5 uppercase tracking-wider">
                Password
              </label>

              <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]"
                >
                  {showPassword ? <Eye size={20} /> : <EyeOff size={20} />}
                </button>
              </div>
            </div>
            <div>
              <label className="block text-[13px] font-semibold text-gray-700 mb-1.5 uppercase tracking-wider">
                Confirm Password
              </label>

              <div className="relative">
                <input
                  type={showConfirmPassword ? "text" : "password"}
                  value={confirmPassword}
                  onChange={(e) => setConfirmPassword(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={() =>
                    setShowConfirmPassword(!showConfirmPassword)
                  }
                  className="absolute right-4 top-1/2 -translate-y-1/2 text-gray-400 hover:text-[#993556]"
                >
                  {showConfirmPassword ? (
                    <Eye size={20} />
                  ) : (
                    <EyeOff size={20} />
                  )}
                </button>
              </div>

              {confirmPassword && password !== confirmPassword && (
                <p className="text-red-500 text-xs mt-1">
                  Passwords do not match
                </p>
              )}
            </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 || password !== confirmPassword}
              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 disabled:cursor-not-allowed"
            >
              {loading ? "Creating Account..." : "Sign Up"}
            </button>
          </form>

          <p className="text-center text-sm text-gray-500 mt-8">
            Already have an account?{" "}
            <Link href="/login" className="text-[#993556] font-bold hover:underline">
              Log in
            </Link>
          </p>
        </div>
      </div>
    </main>
  );
}