"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { signIn } from "next-auth/react";

export default function LoginPage() {
  const router = useRouter();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState<string | null>(null);
  const [loading, setLoading] = useState(false);

  async function onSubmit(e: React.FormEvent) {
    e.preventDefault();
    setError(null);
    setLoading(true);
    const res = await signIn("credentials", {
      email,
      password,
      redirect: false,
    });
    setLoading(false);
    if (res?.error) {
      setError("Invalid email or password.");
      return;
    }
    router.push("/sites");
    router.refresh();
  }

  return (
    <div className="min-h-screen bg-bg flex items-center justify-center p-5">
      <div className="w-full max-w-[380px] bg-bg-card border border-border rounded-2xl p-8">
        <div className="flex items-center gap-2.5 mb-6">
          <div className="w-[30px] h-[30px] rounded-lg bg-gradient-to-br from-accent to-accent-2 flex-none" />
          <span className="font-heading font-bold text-lg tracking-tight">Webstation</span>
        </div>
        <div className="font-heading text-xl font-bold mb-1.5">Sign in</div>
        <div className="text-[12.5px] text-text-secondary mb-5">
          Monitor and manage every site you own
        </div>
        <form onSubmit={onSubmit} className="flex flex-col gap-3.5">
          <div>
            <label className="block text-xs text-text-secondary mb-1.5">Email</label>
            <input
              type="email"
              required
              value={email}
              onChange={(e) => setEmail(e.target.value)}
              placeholder="owner@webstation.app"
              className="w-full bg-bg-input border border-border-input rounded-lg px-3 py-2.5 text-[13px] font-mono outline-none"
            />
          </div>
          <div>
            <label className="block text-xs text-text-secondary mb-1.5">Password</label>
            <input
              type="password"
              required
              value={password}
              onChange={(e) => setPassword(e.target.value)}
              placeholder="••••••••"
              className="w-full bg-bg-input border border-border-input rounded-lg px-3 py-2.5 text-[13px] outline-none"
            />
          </div>
          {error && <div className="text-[12px] text-error">{error}</div>}
          <button
            type="submit"
            disabled={loading}
            className="w-full bg-accent text-white font-bold text-[13.5px] py-2.5 rounded-lg disabled:opacity-60"
          >
            {loading ? "Signing in…" : "Sign in"}
          </button>
        </form>
        <div className="text-[11.5px] text-text-tertiary text-center mt-4">
          No account?{" "}
          <Link href="/register" className="text-accent">
            Create an organization
          </Link>
        </div>
      </div>
    </div>
  );
}
