import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { runSetupAction } from "@/app/setup-actions";
import { needsSetup } from "@/lib/setup";

const errors: Record<string, string> = {
  url: "اكتب رابط الموقع كامل مثل https://earnvato.com",
  name: "اكتب اسم الأدمن.",
  email: "إيميل الأدمن غير صحيح.",
  password: "كلمة السر 8 أحرف على الأقل.",
  match: "كلمتا السر غير متطابقتين.",
};

export default async function SetupPage({
  searchParams,
}: {
  searchParams: Promise<{ error?: string }>;
}) {
  if (!(await needsSetup())) redirect("/ar/login");
  const query = await searchParams;
  const h = await headers();
  const host = h.get("x-forwarded-host") || h.get("host") || "localhost:3000";
  const proto = h.get("x-forwarded-proto") || (host.includes("localhost") ? "http" : "https");
  const guessedUrl = `${proto}://${host}`;

  return (
    <main className="mx-auto flex min-h-full max-w-xl flex-col justify-center px-4 py-10">
      <p className="page-kicker">Earnvato</p>
      <h1 className="page-title mt-2">تثبيت الموقع</h1>
      <p className="mt-2 text-muted">
        أول فتح للموقع. أنشئ حساب الأدمن واضبط الإعدادات. تقدر تعدّلها بعدين من لوحة الإدارة.
      </p>
      {query.error ? <p className="mt-4 text-sm text-red-700">{errors[query.error] ?? "تعذر التثبيت."}</p> : null}
      <form action={runSetupAction} className="card mt-6 space-y-4 p-5">
        <label className="block text-sm">
          اسم الموقع
          <input name="siteName" defaultValue="Earnvato" required className="field mt-1" />
        </label>
        <label className="block text-sm">
          رابط الموقع
          <input name="appUrl" defaultValue={guessedUrl} required className="field mt-1" dir="ltr" />
        </label>
        <label className="block text-sm">
          اسم الأدمن
          <input name="adminName" required className="field mt-1" />
        </label>
        <label className="block text-sm">
          إيميل الأدمن
          <input name="adminEmail" type="email" required className="field mt-1" dir="ltr" />
        </label>
        <label className="block text-sm">
          كلمة سر الأدمن
          <input name="adminPassword" type="password" required minLength={8} className="field mt-1" />
        </label>
        <label className="block text-sm">
          تأكيد كلمة السر
          <input name="confirmPassword" type="password" required minLength={8} className="field mt-1" />
        </label>
        <div className="grid gap-3 sm:grid-cols-2">
          <label className="block text-sm">
            ثواني الانتظار
            <input name="countdown" type="number" min={2} defaultValue={5} className="field mt-1" />
          </label>
          <label className="block text-sm">
            أدنى سحب بالدولار
            <input name="minPayoutUsd" type="number" min={1} defaultValue={5} className="field mt-1" />
          </label>
        </div>
        <label className="block text-sm">
          شبكة USDT
          <input name="usdtNetwork" defaultValue="TRC20" className="field mt-1" dir="ltr" />
        </label>
        <label className="block text-sm">
          محفظة الموقع (وسيط USDT)
          <input name="usdtWallet" className="field mt-1" dir="ltr" />
        </label>
        <label className="block text-sm">
          عمولة الوسيط ٪
          <input name="escrowFeePercent" type="number" min={0} max={20} defaultValue={5} className="field mt-1" />
        </label>
        <button className="btn-primary w-full">تثبيت ودخول لوحة الإدارة</button>
      </form>
    </main>
  );
}
