// app/(app)/staff/clients/nouveau/page.tsx
export const dynamic = "force-dynamic";
import Link from "next/link";
import ConfirmableForm from "@/components/ConfirmableForm";
import { createSbServer } from "@/lib/supabaseServer";

export default async function CreateClientPage() {
  const sb = createSbServer();
  const { data: { user } } = await sb.auth.getUser();
  if (!user) {
    return (
      <main className="p-6">
        <h1 className="text-lg font-semibold">Accès refusé</h1>
        <p className="text-sm text-slate-600">Vous devez être connecté.</p>
      </main>
    );
  }
  const { data: me } = await sb
    .from("staff_users")
    .select("is_staff")
    .eq("user_id", user.id)
    .maybeSingle();
  if (!me?.is_staff) {
    return (
      <main className="p-6">
        <h1 className="text-lg font-semibold">Accès refusé</h1>
        <p className="text-sm text-slate-600">Cette page est réservée au Staff.</p>
      </main>
    );
  }

  return (
    <main className="p-6 space-y-4">
      <div className="flex items-center justify-between">
        <h1 className="text-lg font-semibold">Créer un client</h1>
        <Link href="/staff/utilisateurs" className="text-sm underline">← Retour</Link>
      </div>

      <div className="rounded-2xl border bg-white p-4">
        <form action="/api/staff/organizations/create" method="post" className="space-y-4">
          <div>
            <label className="block text-sm font-medium mb-1">Nom de la structure *</label>
            <input name="name" required className="w-full px-3 py-2 rounded-lg border bg-transparent" placeholder="Ex : Compagnie Les Étoiles" />
          </div>
          <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
              <label className="block text-sm font-medium mb-1">Identifiant Structure API</label>
              <input name="structure_api" className="w-full px-3 py-2 rounded-lg border bg-transparent" placeholder="Ex : CIE-ETOILES" />
            </div>
            <div>
              <label className="block text-sm font-medium mb-1">SIRET (optionnel)</label>
              <input name="siret" className="w-full px-3 py-2 rounded-lg border bg-transparent" placeholder="Ex : 123 456 789 00012" />
            </div>
          </div>
          <div>
            <label className="block text-sm font-medium mb-1">Email de contact (optionnel)</label>
            <input type="email" name="contact_email" className="w-full px-3 py-2 rounded-lg border bg-transparent" placeholder="contact@exemple.fr" />
          </div>
          <div>
            <label className="block text-sm font-medium mb-1">Notes (interne)</label>
            <textarea name="notes" rows={3} className="w-full px-3 py-2 rounded-lg border bg-transparent" placeholder="Informations internes…" />
          </div>
          <button type="submit" className="px-4 py-2 rounded-lg bg-emerald-600 text-white text-sm hover:bg-emerald-700">Créer l’organisation</button>
        </form>
      </div>
    </main>
  );
}