import { useState } from "react";
import { exportReport } from "../utils/exportReport";

/**
 * Button to export simulation results as a downloadable PDF report.
 */
export function ExportButton({ result, scenario, chartRef }) {
  const [exporting, setExporting] = useState(false);

  async function handleExport() {
    setExporting(true);
    try {
      const chartElement = chartRef?.current || null;
      await exportReport(result, scenario, chartElement);
    } catch (err) {
      console.error("Export failed:", err);
      alert("Export failed. Please try again.");
    } finally {
      setExporting(false);
    }
  }

  return (
    <div className="export-section">
      <button
        className="export-button"
        onClick={handleExport}
        disabled={exporting}
      >
        {exporting ? "Generating PDF..." : "Export Report (PDF)"}
      </button>
      <p className="export-note">
        Download a formatted PDF report with scenario inputs, growth chart, risk analysis, and recommendations.
      </p>
    </div>
  );
}
