import { notFound } from "next/navigation";
import { getProposalBySlug } from "@/lib/proposal/queries";
import { ProposalDeck } from "@/components/public/proposal/ProposalDeck";
import { acceptProposal, rejectProposal } from "./actions";

export const revalidate = 0;

export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const proposal = await getProposalBySlug(slug);
  if (!proposal) return { title: "Preventivo non trovato" };
  return {
    title: proposal.title ?? "Documento di Soluzione · iamcavalli",
    description: `Proposta riservata — ${proposal.content.header.clientName} × ${proposal.content.consultant.name}`,
  };
}

export default async function ProposalPage({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const proposal = await getProposalBySlug(slug);
  if (!proposal) notFound();

  // Proposta non ancora pubblicata
  if (proposal.state === "draft") {
    return (
      <div className="min-h-screen flex items-center justify-center bg-white">
        <p className="text-muted-foreground text-sm">Questa proposta non è ancora disponibile.</p>
      </div>
    );
  }

  return (
    <ProposalDeck
      proposal={proposal}
      onAccept={acceptProposal}
      onReject={rejectProposal}
    />
  );
}
