import { redirect } from "next/navigation";
import { setupSiteAction } from "@/lib/actions";
import { isSetupComplete } from "@/lib/setup";

type SetupPageProps = {
  searchParams: Promise<{ error?: string }>;
};

const errorMessages: Record<string, string> = {
  invalid: "Please check all required fields. Password must be at least 8 characters and both passwords must match.",
  setup: "Setup failed. Please confirm the database exists, database user has permission, and the credentials are correct."
};

export default async function SetupPage({ searchParams }: SetupPageProps) {
  if (await isSetupComplete()) {
    redirect("/");
  }

  const params = await searchParams;

  return (
    <main className="admin-login">
      <form action={setupSiteAction} className="admin-login-card form-grid" style={{ maxWidth: 860 }}>
        <span className="pill">First setup</span>
        <div className="admin-brand">
          <span className="admin-brand-icon">J</span>
          <h1 className="admin-brand-title" style={{ margin: 0 }}>Just Chill Nepal Installer</h1>
        </div>
        <p className="muted" style={{ gridColumn: "1 / -1", margin: 0 }}>
          Fill this once after uploading the files to your hosting. It will connect the database, create tables,
          and create the first Super Admin account.
        </p>
        {params.error ? (
          <p className="admin-error-message" style={{ gridColumn: "1 / -1" }}>
            {errorMessages[params.error] ?? "Setup failed. Please try again."}
          </p>
        ) : null}

        <div className="panel-header">Database Setup</div>
        <label className="field">
          Database Host
          <input defaultValue="localhost" name="dbHost" required />
        </label>
        <label className="field">
          Database Port
          <input defaultValue="3306" name="dbPort" required />
        </label>
        <label className="field">
          Database Name
          <input name="dbName" placeholder="justchillnepal" required />
        </label>
        <label className="field">
          Database Username
          <input name="dbUser" placeholder="database user" required />
        </label>
        <label className="field">
          Database Password
          <input name="dbPassword" type="password" />
        </label>
        <label className="field">
          Website URL
          <input defaultValue="https://justchillnepal.com" name="siteUrl" placeholder="https://justchillnepal.com" type="url" />
        </label>

        <div className="panel-header">Super Admin Setup</div>
        <label className="field">
          Admin Name
          <input defaultValue="Just Chill Nepal Admin" name="adminName" required />
        </label>
        <label className="field">
          Admin Email
          <input defaultValue="admin@justchillnepal.com" name="adminEmail" required type="email" />
        </label>
        <label className="field">
          Admin Password
          <input minLength={8} name="adminPassword" required type="password" />
        </label>
        <label className="field">
          Confirm Password
          <input minLength={8} name="confirmPassword" required type="password" />
        </label>

        <button className="btn" style={{ gridColumn: "1 / -1" }} type="submit">
          Setup Website
        </button>
      </form>
    </main>
  );
}
