/**
 * 文件职责：财务中心成本管理 API 契约与请求封装。
 */
import { requestClient } from '#/api/request';

/** 成本统计维度。 */
export type FinanceCostDimension = 'store' | 'tenant';

/** 成本分类编码。 */
export type FinanceCostCategoryCode = 'fixed' | 'food' | 'labor' | 'packaging';

/** 成本作用域查询参数。 */
export interface FinanceCostScopeQuery {
  dimension?: FinanceCostDimension;
  month?: string;
  storeId?: string;
}

/** 成本明细项。 */
export interface FinanceCostEntryDetailDto {
  amount: number;
  itemId?: string;
  itemName: string;
  quantity?: number;
  sortOrder: number;
  unitPrice?: number;
}

/** 成本分类数据。 */
export interface FinanceCostEntryCategoryDto {
  category: FinanceCostCategoryCode;
  categoryText: string;
  items: FinanceCostEntryDetailDto[];
  percentage: number;
  totalAmount: number;
}

/** 成本录入数据。 */
export interface FinanceCostEntryDto {
  categories: FinanceCostEntryCategoryDto[];
  costRate: number;
  dimension: FinanceCostDimension;
  month: string;
  monthRevenue: number;
  storeId?: string;
  totalCost: number;
}

/** 保存成本明细项请求。 */
export interface SaveFinanceCostDetailPayload {
  amount: number;
  itemId?: string;
  itemName: string;
  quantity?: number;
  sortOrder: number;
  unitPrice?: number;
}

/** 保存成本分类请求。 */
export interface SaveFinanceCostCategoryPayload {
  category: FinanceCostCategoryCode;
  items: SaveFinanceCostDetailPayload[];
  totalAmount: number;
}

/** 保存成本录入请求。 */
export interface SaveFinanceCostEntryPayload extends FinanceCostScopeQuery {
  categories: SaveFinanceCostCategoryPayload[];
}

/** 成本分析统计卡。 */
export interface FinanceCostAnalysisStatsDto {
  averageCostPerPaidOrder: number;
  foodCostRate: number;
  monthOnMonthChangeRate: number;
  paidOrderCount: number;
  revenue: number;
  totalCost: number;
}

/** 成本趋势点。 */
export interface FinanceCostTrendPointDto {
  costRate: number;
  month: string;
  revenue: number;
  totalCost: number;
}

/** 成本构成项。 */
export interface FinanceCostCompositionDto {
  amount: number;
  category: FinanceCostCategoryCode;
  categoryText: string;
  percentage: number;
}

/** 成本明细表行。 */
export interface FinanceCostMonthlyDetailRowDto {
  costRate: number;
  fixedAmount: number;
  foodAmount: number;
  laborAmount: number;
  month: string;
  packagingAmount: number;
  totalCost: number;
}

/** 成本分析数据。 */
export interface FinanceCostAnalysisDto {
  composition: FinanceCostCompositionDto[];
  detailRows: FinanceCostMonthlyDetailRowDto[];
  dimension: FinanceCostDimension;
  month: string;
  stats: FinanceCostAnalysisStatsDto;
  storeId?: string;
  trend: FinanceCostTrendPointDto[];
}

/** 查询成本录入数据。 */
export async function getFinanceCostEntryApi(params: FinanceCostScopeQuery) {
  return requestClient.get<FinanceCostEntryDto>('/finance/cost/entry', {
    params,
  });
}

/** 保存成本录入数据。 */
export async function saveFinanceCostEntryApi(
  payload: SaveFinanceCostEntryPayload,
) {
  return requestClient.post<FinanceCostEntryDto>(
    '/finance/cost/entry/save',
    payload,
  );
}

/** 查询成本分析数据。 */
export async function getFinanceCostAnalysisApi(
  params: FinanceCostScopeQuery & { trendMonthCount?: number },
) {
  return requestClient.get<FinanceCostAnalysisDto>('/finance/cost/analysis', {
    params,
  });
}
