/**
 * PlanDownloadMenuButton — 마케팅 기획 다운로드 단일 진입점 (PDF / CSV).
 *
 * PDF: 브라우저 네이티브 인쇄(window.print) → "PDF로 저장"
 * CSV: 기획의 표 데이터를 다중 섹션 CSV 단일 파일로 다운로드
 */
import { FileText, FileSpreadsheet, ChevronDown, Loader2 } from 'lucide-react';
import {
  DropdownMenu,
  DropdownMenuTrigger,
  DropdownMenuContent,
  DropdownMenuItem,
} from '@/shared/ui/dropdown-menu';
import { useExportPDF } from '@/features/report/hooks/useExportPDF';
import { useExportPlanCSV } from '@/features/plan/hooks/useExportPlanCSV';
import type { MarketingPlan } from '@/features/plan/types/plan';

interface PlanDownloadMenuButtonProps {
  /** 파일명 베이스. 자동으로 .pdf / .csv 확장자 붙음 */
  filename: string;
  /** 서버에서 받아온 기획 전체 (CSV 생성에 사용) */
  plan: MarketingPlan;
  className?: string;
}

export function PlanDownloadMenuButton({ filename, plan, className }: PlanDownloadMenuButtonProps) {
  const { exportPDF, isExporting } = useExportPDF();
  const { exportPlanCSV } = useExportPlanCSV();

  return (
    <DropdownMenu>
      <DropdownMenuTrigger
        disabled={isExporting}
        className={
          className ??
          'inline-flex items-center gap-1.5 px-3.5 py-2 rounded-full text-xs font-medium text-slate-600 bg-slate-50 border border-slate-200 hover:bg-white hover:border-slate-300 transition-all disabled:opacity-60 disabled:cursor-wait'
        }
      >
        {isExporting ? (
          <>
            <Loader2 size={12} className="animate-spin" />
            생성 중...
          </>
        ) : (
          <>
            다운로드
            <ChevronDown size={12} className="opacity-70" />
          </>
        )}
      </DropdownMenuTrigger>
      <DropdownMenuContent>
        <DropdownMenuItem onSelect={() => exportPDF(filename)}>
          <FileText size={14} className="text-slate-500" />
          <span>PDF로 저장</span>
        </DropdownMenuItem>
        <DropdownMenuItem onSelect={() => exportPlanCSV(filename, plan)}>
          <FileSpreadsheet size={14} className="text-slate-500" />
          <span>CSV로 저장</span>
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  );
}
