/**
 * 文件职责：财务概览 API 契约与请求封装。
 */
import { requestClient } from '#/api/request';

/** 财务概览维度。 */
export type FinanceOverviewDimension = 'store' | 'tenant';

/** 趋势周期值。 */
export type FinanceOverviewTrendRange = '7' | '30';

/** 财务概览查询参数。 */
export interface FinanceOverviewDashboardQuery {
  dimension: FinanceOverviewDimension;
  storeId?: string;
}

/** KPI 卡片数据。 */
export interface FinanceOverviewKpiCardDto {
  amount: number;
  changeRate: number;
  compareAmount: number;
  compareLabel: string;
  trend: 'down' | 'flat' | 'up';
}

/** 收入趋势点。 */
export interface FinanceOverviewIncomeTrendPointDto {
  amount: number;
  date: string;
  dateLabel: string;
}

/** 收入趋势数据。 */
export interface FinanceOverviewIncomeTrendDto {
  last30Days: FinanceOverviewIncomeTrendPointDto[];
  last7Days: FinanceOverviewIncomeTrendPointDto[];
}

/** 利润趋势点。 */
export interface FinanceOverviewProfitTrendPointDto {
  costAmount: number;
  date: string;
  dateLabel: string;
  netProfitAmount: number;
  revenueAmount: number;
}

/** 利润趋势数据。 */
export interface FinanceOverviewProfitTrendDto {
  last30Days: FinanceOverviewProfitTrendPointDto[];
  last7Days: FinanceOverviewProfitTrendPointDto[];
}

/** 收入构成项。 */
export interface FinanceOverviewIncomeCompositionItemDto {
  amount: number;
  channel: 'delivery' | 'dine_in' | 'pickup';
  channelText: string;
  percentage: number;
}

/** 收入构成数据。 */
export interface FinanceOverviewIncomeCompositionDto {
  items: FinanceOverviewIncomeCompositionItemDto[];
  totalAmount: number;
}

/** 成本构成项。 */
export interface FinanceOverviewCostCompositionItemDto {
  amount: number;
  category: 'fixed' | 'food' | 'labor' | 'packaging' | 'platform';
  categoryText: string;
  percentage: number;
}

/** 成本构成数据。 */
export interface FinanceOverviewCostCompositionDto {
  items: FinanceOverviewCostCompositionItemDto[];
  totalAmount: number;
}

/** TOP 商品项。 */
export interface FinanceOverviewTopProductItemDto {
  percentage: number;
  productName: string;
  rank: number;
  revenueAmount: number;
  salesQuantity: number;
}

/** TOP 商品排行。 */
export interface FinanceOverviewTopProductDto {
  items: FinanceOverviewTopProductItemDto[];
  periodDays: number;
}

/** 财务概览驾驶舱数据。 */
export interface FinanceOverviewDashboardDto {
  actualReceived: FinanceOverviewKpiCardDto;
  costComposition: FinanceOverviewCostCompositionDto;
  dimension: FinanceOverviewDimension;
  incomeComposition: FinanceOverviewIncomeCompositionDto;
  incomeTrend: FinanceOverviewIncomeTrendDto;
  netIncome: FinanceOverviewKpiCardDto;
  profitTrend: FinanceOverviewProfitTrendDto;
  refundAmount: FinanceOverviewKpiCardDto;
  storeId?: string;
  todayRevenue: FinanceOverviewKpiCardDto;
  topProducts: FinanceOverviewTopProductDto;
  withdrawableBalance: FinanceOverviewKpiCardDto;
}

/** 查询财务概览驾驶舱。 */
export async function getFinanceOverviewDashboardApi(
  params: FinanceOverviewDashboardQuery,
) {
  return requestClient.get<FinanceOverviewDashboardDto>(
    '/finance/overview/dashboard',
    { params },
  );
}
