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

import Logo from "@/assets/images/logo.png";
import Image from "next/image";

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

  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState("");

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

    setLoading(true);
    setError("");

    try {
      const res = await fetch("/api/admin/login", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          email,
          password,
        }),
      });

      const data = await res.json();

      if (!res.ok) {
        setError(data.message || "Login failed");
        return;
      }

      router.push("/admin/dashboard");
    } catch {
      setError("Something went wrong. Please try again.");
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="admin-bg d-flex align-items-center justify-content-center p-3">
      <div className="card">
        <div className="text-center text-white">
          <div className="logo">
            <Image src={Logo} alt="Logo" />
          </div>


          <p className="text-light opacity-75">
            Sign in to access your dashboard
          </p>
        </div>
        <div className="card-body">
          {error && (
            <div className="alert alert-danger">
              {error}
            </div>
          )}

          <form onSubmit={handleSubmit}>
            
            <div className="form-group mb-3 mb-sm-4">
              <label className="form-label text-white fw-semibold">
                Email Address
              </label>
              <div className="position-relative">
                <i className="bi bi-envelope"></i>
                <input
                  type="email"
                  className="form-control"
                  placeholder="Enter email address"
                  value={email}
                  onChange={(e) =>
                    setEmail(e.target.value)
                  }
                  required
                />
              </div>
            </div>
    
            <div className="form-group mb-3 mb-sm-4">
              <label className="form-label text-white fw-semibold">
                Password
              </label>
              <div className="position-relative">
                <i className="bi bi-lock"></i>
                <input
                  type="password"
                  className="form-control"
                  placeholder="Enter password"
                  value={password}
                  onChange={(e) =>
                    setPassword(e.target.value)
                  }
                  required
                />
              </div>
            </div>
            <button
              type="submit"
              disabled={loading}
              className="w-100 admin-theme-btn"
            >
              {loading ? (
                <>
                  <span className="spinner-border spinner-border-sm me-2" />
                  Signing In...
                </>
              ) : (
                "Sign In"
              )}
            </button>
          </form>
        </div>
      </div>
    </div>
  );
}