import { createClient } from "@/lib/supabase/server";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { ArrowLeft } from "lucide-react";
import { InvoiceForm } from "@/components/billing/invoice-form";

export const dynamic = "force-dynamic";

export default async function NewInvoicePage() {
  const supabase = await createClient();
  const { data: patients } = await supabase.from("patients").select("id, first_name, last_name, phone").eq("is_active", true).order("last_name").limit(200);
  const { data: services } = await supabase.from("services_catalog").select("*").eq("is_active", true).order("name");

  return (
    <div className="mx-auto max-w-3xl space-y-6">
      <div className="flex items-center gap-4">
        <Link href="/billing"><Button variant="ghost" size="icon"><ArrowLeft className="h-4 w-4" /></Button></Link>
        <div>
          <h1 className="text-2xl font-bold">Nueva factura</h1>
          <p className="text-sm text-muted-foreground">Crea una nueva factura para un paciente.</p>
        </div>
      </div>
      <InvoiceForm patients={patients || []} services={services || []} />
    </div>
  );
}
