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

function RegisterPage() {
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const [message, setMessage] = useState("");
  const [error, setError] = useState("");

  const handleRegister = 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 registerUser({ username: cleanUsername, password });
      setMessage(response.data.message);
      setUsername("");
      setPassword("");
    } catch (err) {
      setError(err.response?.data?.message || "Registration failed");
    }
  };

  return (
    <div className="min-h-screen bg-[radial-gradient(circle_at_top_right,#ccfbf1,transparent_30%),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-[1.05fr_1fr] items-center  lg:flex lg:flex-col lg:justify-center ">
          <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-emerald-700">New workspace user</p>
                <h1 className="mt-2 text-3xl font-bold text-slate-950">Create Account</h1>
                <p className="mt-2 text-sm text-slate-600">Register a new EPMS user account for payroll management.</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={handleRegister} 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-emerald-300 focus:border-emerald-500 focus:shadow-md focus:ring-4 focus:ring-emerald-100"
                    placeholder="Choose a 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-emerald-300 focus:border-emerald-500 focus:shadow-md focus:ring-4 focus:ring-emerald-100"
                    placeholder="Create a secure password"
                    value={password}
                    onChange={(event) => setPassword(event.target.value)}
                    minLength={6}
                    required
                  />
                </label>

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

              <div className="mt-6 text-sm">
                <Link to="/" className="font-semibold text-blue-700 transition hover:text-blue-800 hover:underline">
                  Back to login
                </Link>
              </div>
            </div>
          </section>

        </div>
      </div>
    </div>
  );
}

export default RegisterPage;
