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

export const dynamic = "force-dynamic";

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

  return (
    <main className="auth-page">
      <section className="auth-card">
        <div className="title-block" style={{ marginBottom: 18 }}>
          <h1>Create account</h1>
          <p>Register a username and get a persistent room.</p>
        </div>
        <form className="form" action={registerUser}>
          <label>
            Username
            <input className="input" name="username" autoComplete="username" required />
          </label>
          <label>
            Password
            <input className="input" name="password" type="password" autoComplete="new-password" minLength={10} required />
          </label>
          <button className="button primary" type="submit">
            Create account
          </button>
          <Link className="button" href="/login">
            Login instead
          </Link>
        </form>
      </section>
    </main>
  );
}
