"use client";

import { Suspense } from "react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { AppShell } from "@/components/AppShell";
import { PolicyForm } from "@/components/PolicyForm";
import { useCan } from "@/lib/abilities";

export default function NuevaPolizaPage() {
  return (
    <AppShell>
      <Suspense fallback={null}>
        <NuevaPoliza />
      </Suspense>
    </AppShell>
  );
}

function NuevaPoliza() {
  const allowed = useCan("policy:create");
  const params = useSearchParams();
  const customerId = params.get("customerId") ?? undefined;
  const customerName = params.get("customerName") ?? undefined;

  return (
    <>
      <div className="page-head">
        <Link href="/polizas" className="back-link">← Pólizas</Link>
        <h1 className="page-title">Nueva póliza</h1>
      </div>
      {allowed ? (
        <PolicyForm fixedCustomerId={customerId} fixedCustomerName={customerName} />
      ) : (
        <div className="state-box state-error">
          No tiene permisos para crear pólizas.
        </div>
      )}
    </>
  );
}
