/**
 * 文件职责：经营报表预览抽屉与单条导出动作。
 */
import type { FinanceBusinessReportDetailDto } from '#/api/finance';

import { message } from 'ant-design-vue';

import {
  exportFinanceBusinessReportExcelApi,
  exportFinanceBusinessReportPdfApi,
  getFinanceBusinessReportDetailApi,
} from '#/api/finance';

import { buildReportDetailQueryPayload, downloadBase64File } from './helpers';

interface DetailActionOptions {
  canExport: { value: boolean };
  currentPreviewReportId: { value: string };
  isDownloadingExcel: { value: boolean };
  isDownloadingPdf: { value: boolean };
  isPreviewDrawerOpen: { value: boolean };
  isPreviewLoading: { value: boolean };
  previewDetail: { value: FinanceBusinessReportDetailDto | null };
  selectedStoreId: { value: string };
}

/** 创建预览与单条导出动作。 */
export function createDetailActions(options: DetailActionOptions) {
  function setPreviewDrawerOpen(value: boolean) {
    options.isPreviewDrawerOpen.value = value;
    if (!value) {
      options.previewDetail.value = null;
      options.currentPreviewReportId.value = '';
    }
  }

  async function openPreview(reportId: string) {
    if (!options.selectedStoreId.value || !reportId) {
      return;
    }

    options.currentPreviewReportId.value = reportId;
    options.isPreviewDrawerOpen.value = true;
    options.previewDetail.value = null;
    options.isPreviewLoading.value = true;
    try {
      const payload = buildReportDetailQueryPayload(
        options.selectedStoreId.value,
        reportId,
      );
      options.previewDetail.value =
        await getFinanceBusinessReportDetailApi(payload);
      options.currentPreviewReportId.value =
        options.previewDetail.value.reportId || reportId;
    } finally {
      options.isPreviewLoading.value = false;
    }
  }

  function resolveTargetReportId(reportId?: string) {
    const explicit = String(reportId ?? '').trim();
    if (explicit) {
      return explicit;
    }

    const current = String(options.currentPreviewReportId.value || '').trim();
    if (current) {
      return current;
    }

    return String(options.previewDetail.value?.reportId ?? '').trim();
  }

  async function downloadPdf(reportId?: string) {
    if (!options.canExport.value || !options.selectedStoreId.value) {
      return;
    }

    const targetReportId = resolveTargetReportId(reportId);
    if (!targetReportId) {
      return;
    }

    options.isDownloadingPdf.value = true;
    try {
      const payload = buildReportDetailQueryPayload(
        options.selectedStoreId.value,
        targetReportId,
      );
      const result = await exportFinanceBusinessReportPdfApi(payload);
      downloadBase64File(
        result.fileName,
        result.fileContentBase64,
        'application/pdf',
      );
      message.success('PDF 下载成功');
    } finally {
      options.isDownloadingPdf.value = false;
    }
  }

  async function downloadExcel(reportId?: string) {
    if (!options.canExport.value || !options.selectedStoreId.value) {
      return;
    }

    const targetReportId = resolveTargetReportId(reportId);
    if (!targetReportId) {
      return;
    }

    options.isDownloadingExcel.value = true;
    try {
      const payload = buildReportDetailQueryPayload(
        options.selectedStoreId.value,
        targetReportId,
      );
      const result = await exportFinanceBusinessReportExcelApi(payload);
      downloadBase64File(
        result.fileName,
        result.fileContentBase64,
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      );
      message.success('Excel 下载成功');
    } finally {
      options.isDownloadingExcel.value = false;
    }
  }

  return {
    downloadExcel,
    downloadPdf,
    openPreview,
    setPreviewDrawerOpen,
  };
}
