"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { Lock } from "lucide-react";

export default function AdminPage() {
  const [password, setPassword] = useState("");
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(null);
  const router = useRouter();

  async function handleLogin(e) {
    e.preventDefault();
    setLoading(true);
    setError(null);
    try {
      const res = await fetch("/api/auth", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ password }),
      });
      if (res.ok) {
        sessionStorage.setItem("adminToken", password);
        router.push("/");
      } else {
        const data = await res.json();
        setError(data.error || "Invalid password");
      }
    } catch {
      setError("Network error");
    } finally {
      setLoading(false);
    }
  }

  return (
    <div className="min-h-screen flex items-center justify-center p-6 bg-gray-50">
      <div className="w-full max-w-sm">
        {/* Card */}
        <div className="rounded-xl overflow-hidden shadow-lg border border-gray-200 bg-white">
          {/* Header */}
          <div className="p-6 text-center bg-indigo-600">
            <Lock className="inline-block mb-2" size={28} color="#fff" />
            <h1 className="text-2xl font-bold text-white">Admin Access</h1>
            <p className="text-xs mt-1 text-indigo-200">All In One Notebook</p>
          </div>

          {/* Form */}
          <div className="p-6">
            <form onSubmit={handleLogin} className="space-y-4">
              <div>
                <label className="block text-sm font-semibold mb-1 text-gray-700">
                  Password
                </label>
                <div className="relative">
                  <Lock
                    size={14}
                    className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400"
                  />
                  <input
                    type="password"
                    value={password}
                    onChange={(e) => setPassword(e.target.value)}
                    required
                    placeholder="Enter admin password"
                    className="w-full pl-8 pr-3 py-2 border border-gray-300 rounded
                               bg-white focus:outline-none focus:border-indigo-400 text-sm text-gray-800"
                  />
                </div>
              </div>

              {error && (
                <p className="text-red-700 text-sm bg-red-50 border border-red-200 rounded px-3 py-2">
                  {error}
                </p>
              )}

              <button
                type="submit"
                disabled={loading}
                className="w-full py-2 rounded font-semibold text-sm bg-indigo-600 text-white
                           hover:bg-indigo-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
              >
                {loading ? "Checking…" : "Login"}
              </button>
            </form>

            <p className="text-center text-xs mt-4 text-gray-400">
              Set ADMIN_PASSWORD in your .env file
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}