"use client";

import { money } from "@/lib/fees";

export function InvoiceDownload(props: {
  title: string;
  labor: number;
  materials: number;
  travel: number;
  tolls: number;
  contractor: string;
}) {
  async function download() {
    const { jsPDF } = await import("jspdf");
    const doc = new jsPDF();
    const total = props.labor + props.materials + props.travel + props.tolls;
    doc.setFontSize(16);
    doc.text("Servicios PR — Factura", 14, 20);
    doc.setFontSize(11);
    doc.text(props.title, 14, 32);
    doc.text(`Contratista: ${props.contractor}`, 14, 40);
    doc.text(`Mano de obra: ${money(props.labor)}`, 14, 52);
    doc.text(`Materiales: ${money(props.materials)}`, 14, 60);
    doc.text(`Viaje: ${money(props.travel)}`, 14, 68);
    doc.text(`Peajes: ${money(props.tolls)}`, 14, 76);
    doc.text(`Total: ${money(total)}`, 14, 88);
    doc.save(`factura-${Date.now()}.pdf`);
  }

  return (
    <button type="button" className="btn-ghost mt-3 text-sm" onClick={download}>
      Descargar PDF
    </button>
  );
}
