import { useState } from "react";
import { Link, useNavigate } from "react-router-dom";
import { loginUser } from "../api/authApi";

function LoginPage({ onLoginSuccess }) {
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const [message, setMessage] = useState("");
  const [error, setError] = useState("");
  const navigate = useNavigate();

  const handleLogin = async (event) => {
    event.preventDefault();
    setError("");
    setMessage("");
    const cleanUsername = username.trim();
    if (cleanUsername.length < 3) {
      setError("Username must be at least 3 characters");
      return;
    }
    if (password.length < 6) {
      setError("Password must be at least 6 characters");
      return;
    }

    try {
      const response = await loginUser({ username: cleanUsername, password });
      onLoginSuccess(response.data.username);
      navigate("/employee");
    } catch (err) {
      setError(err.response?.data?.message || "Login failed");
    }
  };

  return (
    <div className="min-h-screen bg-[radial-gradient(circle_at_top_left,#dbeafe,transparent_32%),linear-gradient(135deg,#f8fafc,#e2e8f0)] px-4 py-8">
      <div className="mx-auto flex min-h-[calc(100vh-4rem)] w-100  items-center justify-center">
        <div className="grid w-full overflow-hidden rounded-2xl bg-white shadow-2xl ring-1 ring-slate-200 lg:grid-cols-[1fr_1.05fr] items-center  lg:flex lg:flex-col lg:justify-center  ">
            {/* <section className="hidden bg-slate-950 p-8 text-white lg:flex lg:flex-col lg:justify-between">
              <div>
                <div className="inline-flex rounded-full bg-blue-500/15 px-3 py-1 text-sm font-semibold text-blue-200 ring-1 ring-blue-400/25">
                  EPMS Payroll
                </div>
                <h1 className="mt-6 text-4xl font-bold">Welcome back</h1>
                <p className="mt-4 max-w-sm text-sm leading-6 text-slate-300">
                  Sign in to manage employees, departments, salary records, and monthly payroll reports from one workspace.
                </p>
              </div>

              <div className="grid gap-3">
                <div className="rounded-xl bg-white/10 p-4 ring-1 ring-white/10">
                  <div className="text-sm font-semibold text-blue-100">Fast payroll checks</div>
                  <p className="mt-1 text-sm text-slate-300">Review staff records and report totals with fewer clicks.</p>
                </div>
                <div className="rounded-xl bg-white/10 p-4 ring-1 ring-white/10">
                  <div className="text-sm font-semibold text-blue-100">Secure session access</div>
                  <p className="mt-1 text-sm text-slate-300">Your dashboard opens only after a valid login.</p>
                </div>
              </div>
            </section> */}

          <section className="p-6 sm:p-8">
            <div className="mx-auto max-w-md">
              <div className="mb-8">
                <p className="text-sm font-semibold text-blue-700">Account access</p>
                <h2 className="mt-2 text-3xl font-bold text-slate-950">Login to EPMS</h2>
                <p className="mt-2 text-sm text-slate-600">Use your account to manage employee payroll data.</p>
              </div>

              {(error || message) && (
                <div
                  className={`mb-4 rounded-xl px-4 py-3 text-sm font-medium shadow-sm ring-1 ${
                    error ? "bg-red-50 text-red-700 ring-red-200" : "bg-green-50 text-green-700 ring-green-200"
                  }`}
                >
                  {error || message}
                </div>
              )}

              <form onSubmit={handleLogin} className="space-y-4">
                <label className="block">
                  <span className="mb-2 block text-sm font-semibold text-slate-700">Username</span>
                  <input
                    className="w-full rounded-xl border border-slate-300 bg-white px-4 py-3 text-slate-950 shadow-sm outline-none transition hover:border-blue-300 focus:border-blue-500 focus:shadow-md focus:ring-4 focus:ring-blue-100"
                    placeholder="Enter your username"
                    value={username}
                    onChange={(event) => setUsername(event.target.value)}
                    minLength={3}
                    required
                  />
                </label>

                <label className="block">
                  <span className="mb-2 block text-sm font-semibold text-slate-700">Password</span>
                  <input
                    type="password"
                    className="w-full rounded-xl border border-slate-300 bg-white px-4 py-3 text-slate-950 shadow-sm outline-none transition hover:border-blue-300 focus:border-blue-500 focus:shadow-md focus:ring-4 focus:ring-blue-100"
                    placeholder="Enter your password"
                    value={password}
                    onChange={(event) => setPassword(event.target.value)}
                    minLength={6}
                    required
                  />
                </label>

                <button className="w-full rounded-xl bg-blue-700 px-4 py-3 font-semibold text-white shadow-lg shadow-blue-700/20 transition hover:-translate-y-0.5 hover:bg-blue-800 hover:shadow-xl hover:shadow-blue-700/25 focus:outline-none focus:ring-4 focus:ring-blue-200">
                  Login
                </button>
              </form>

              <div className="mt-6 flex flex-col gap-3 text-sm sm:flex-row sm:items-center sm:justify-between">
                <Link to="/register" className="font-semibold text-emerald-700 transition hover:text-emerald-800 hover:underline">
                  Create new account
                </Link>
                <Link to="/reset-password" className="font-semibold text-amber-700 transition hover:text-amber-800 hover:underline">
                  Forgot password?
                </Link>
              </div>
            </div>
          </section>
        </div>
      </div>
    </div>
  );
}

export default LoginPage;
