import Link from "next/link";
import { loginUser } from "@/lib/user-actions";
import { hasAdminUser } from "@/lib/setup";
import { redirect } from "next/navigation";

export const dynamic = "force-dynamic";

export default async function LoginPage() {
  if (!(await hasAdminUser())) {
    redirect("/setup");
  }

  return (
    <main className="auth-page">
      <section className="auth-card">
        <div className="title-block" style={{ marginBottom: 18 }}>
          <h1>Login</h1>
          <p>Enter WatchLink with your username and password.</p>
        </div>
        <form className="form" action={loginUser}>
          <label>
            Username
            <input className="input" name="username" autoComplete="username" required />
          </label>
          <label>
            Password
            <input className="input" name="password" type="password" autoComplete="current-password" required />
          </label>
          <button className="button primary" type="submit">
            Login
          </button>
          <Link className="button" href="/register">
            Create account
          </Link>
        </form>
      </section>
    </main>
  );
}
