/**
 * 文件职责：财务中心经营报表 API 契约与请求封装。
 */
import { requestClient } from '#/api/request';

/** 报表周期筛选值。 */
export type FinanceBusinessReportPeriodType = 'daily' | 'monthly' | 'weekly';

/** 经营报表状态值。 */
export type FinanceBusinessReportStatus =
  | 'failed'
  | 'queued'
  | 'running'
  | 'succeeded';

/** 经营报表列表查询参数。 */
export interface FinanceBusinessReportListQuery {
  page: number;
  pageSize: number;
  periodType?: FinanceBusinessReportPeriodType;
  storeId: string;
}

/** 经营报表详情查询参数。 */
export interface FinanceBusinessReportDetailQuery {
  reportId: string;
  storeId: string;
}

/** 经营报表批量导出查询参数。 */
export interface FinanceBusinessReportBatchExportQuery {
  page: number;
  pageSize: number;
  periodType?: FinanceBusinessReportPeriodType;
  storeId: string;
}

/** 经营报表列表行。 */
export interface FinanceBusinessReportListItemDto {
  averageOrderValue: number;
  canDownload: boolean;
  costTotalAmount: number;
  dateText: string;
  netProfitAmount: number;
  orderCount: number;
  profitRatePercent: number;
  refundRatePercent: number;
  reportId: string;
  revenueAmount: number;
  status: FinanceBusinessReportStatus;
  statusText: string;
}

/** 经营报表列表结果。 */
export interface FinanceBusinessReportListResultDto {
  items: FinanceBusinessReportListItemDto[];
  page: number;
  pageSize: number;
  total: number;
}

/** 经营报表 KPI 项。 */
export interface FinanceBusinessReportKpiDto {
  key: string;
  label: string;
  momChangeRate: number;
  valueText: string;
  yoyChangeRate: number;
}

/** 经营报表明细项。 */
export interface FinanceBusinessReportBreakdownItemDto {
  amount: number;
  key: string;
  label: string;
  ratioPercent: number;
}

/** 经营报表详情。 */
export interface FinanceBusinessReportDetailDto {
  costBreakdowns: FinanceBusinessReportBreakdownItemDto[];
  incomeBreakdowns: FinanceBusinessReportBreakdownItemDto[];
  kpis: FinanceBusinessReportKpiDto[];
  periodType: FinanceBusinessReportPeriodType;
  reportId: string;
  status: FinanceBusinessReportStatus;
  statusText: string;
  title: string;
}

/** 经营报表导出结果。 */
export interface FinanceBusinessReportExportDto {
  fileContentBase64: string;
  fileName: string;
  totalCount: number;
}

/** 查询经营报表列表。 */
export async function getFinanceBusinessReportListApi(
  params: FinanceBusinessReportListQuery,
) {
  return requestClient.get<FinanceBusinessReportListResultDto>(
    '/finance/report/list',
    {
      params,
    },
  );
}

/** 查询经营报表详情。 */
export async function getFinanceBusinessReportDetailApi(
  params: FinanceBusinessReportDetailQuery,
) {
  return requestClient.get<FinanceBusinessReportDetailDto>(
    '/finance/report/detail',
    {
      params,
    },
  );
}

/** 导出经营报表 PDF。 */
export async function exportFinanceBusinessReportPdfApi(
  params: FinanceBusinessReportDetailQuery,
) {
  return requestClient.get<FinanceBusinessReportExportDto>(
    '/finance/report/export/pdf',
    {
      params,
    },
  );
}

/** 导出经营报表 Excel。 */
export async function exportFinanceBusinessReportExcelApi(
  params: FinanceBusinessReportDetailQuery,
) {
  return requestClient.get<FinanceBusinessReportExportDto>(
    '/finance/report/export/excel',
    {
      params,
    },
  );
}

/** 批量导出经营报表（ZIP）。 */
export async function exportFinanceBusinessReportBatchApi(
  params: FinanceBusinessReportBatchExportQuery,
) {
  return requestClient.get<FinanceBusinessReportExportDto>(
    '/finance/report/export/batch',
    {
      params,
    },
  );
}
