'use client'

import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { PieChart, Download } from 'lucide-react'
import { getExecutiveReportData } from '@/app/dashboard/reports/actions'
import { generateExecutiveReport } from '@/lib/executive-report-generator'

export function ExecutiveExportButton({ siteName }: { siteName: string }) {
  const [isExporting, setIsExporting] = useState(false)

  const handleExport = async () => {
    setIsExporting(true)
    try {
      const data = await getExecutiveReportData()
      await generateExecutiveReport(data, siteName)
    } catch (error) {
      console.error(error)
      alert('Failed to generate executive report')
    } finally {
      setIsExporting(false)
    }
  }

  return (
    <Button 
      onClick={handleExport} 
      disabled={isExporting}
      className="bg-primary hover:bg-primary/90 text-white gap-2 shadow-lg shadow-primary/20"
    >
      {isExporting ? (
        <>
          <div className="h-4 w-4 border-2 border-white/30 border-t-white rounded-full animate-spin" />
          Processing...
        </>
      ) : (
        <>
          <PieChart className="w-4 h-4" />
          Export Executive PDF
        </>
      )}
    </Button>
  )
}
