import type { FinanceOverviewTrendState } from '../../types';

import type {
  FinanceOverviewDashboardDto,
  FinanceOverviewDimension,
  FinanceOverviewKpiCardDto,
  FinanceOverviewTrendRange,
} from '#/api/finance/overview';

/**
 * 文件职责：财务概览页面常量定义。
 */

/** 财务概览查看权限。 */
export const FINANCE_OVERVIEW_VIEW_PERMISSION = 'tenant:finance:overview:view';

/** 维度切换选项。 */
export const OVERVIEW_DIMENSION_OPTIONS: Array<{
  label: string;
  value: FinanceOverviewDimension;
}> = [
  { label: '租户汇总', value: 'tenant' },
  { label: '门店视角', value: 'store' },
];

/** KPI 键。 */
export type FinanceOverviewKpiKey =
  | 'actualReceived'
  | 'netIncome'
  | 'refundAmount'
  | 'todayRevenue'
  | 'withdrawableBalance';

/** KPI 卡片配置信息。 */
export const OVERVIEW_KPI_CONFIG: Array<{
  icon: string;
  key: FinanceOverviewKpiKey;
  label: string;
  tone: 'blue' | 'green' | 'orange' | 'purple' | 'red';
}> = [
  {
    key: 'todayRevenue',
    label: '今日营业额',
    icon: 'lucide:coins',
    tone: 'blue',
  },
  {
    key: 'actualReceived',
    label: '实收',
    icon: 'lucide:badge-check',
    tone: 'green',
  },
  {
    key: 'refundAmount',
    label: '退款',
    icon: 'lucide:undo-2',
    tone: 'red',
  },
  {
    key: 'netIncome',
    label: '净收入',
    icon: 'lucide:wallet',
    tone: 'purple',
  },
  {
    key: 'withdrawableBalance',
    label: '可提现余额',
    icon: 'lucide:landmark',
    tone: 'orange',
  },
];

/** 收入构成颜色映射。 */
export const INCOME_COMPOSITION_COLOR_MAP: Record<string, string> = {
  delivery: '#1677ff',
  pickup: '#22c55e',
  dine_in: '#f59e0b',
};

/** 成本构成颜色映射。 */
export const COST_COMPOSITION_COLOR_MAP: Record<string, string> = {
  food: '#ef4444',
  labor: '#f59e0b',
  fixed: '#8b5cf6',
  packaging: '#06b6d4',
  platform: '#94a3b8',
};

/** 默认筛选状态。 */
export const DEFAULT_OVERVIEW_FILTER = {
  dimension: 'tenant',
  storeId: '',
} as const;

/** 默认趋势状态。 */
export const DEFAULT_OVERVIEW_TREND_STATE: FinanceOverviewTrendState = {
  incomeRange: '7',
  profitRange: '7',
};

/** 默认 KPI 卡片。 */
export const EMPTY_KPI_CARD: FinanceOverviewKpiCardDto = {
  amount: 0,
  compareAmount: 0,
  changeRate: 0,
  compareLabel: '较昨日',
  trend: 'flat',
};

/** 默认财务概览数据。 */
export function createDefaultFinanceOverviewDashboard(): FinanceOverviewDashboardDto {
  return {
    dimension: 'tenant',
    storeId: undefined,
    todayRevenue: { ...EMPTY_KPI_CARD },
    actualReceived: { ...EMPTY_KPI_CARD },
    refundAmount: { ...EMPTY_KPI_CARD },
    netIncome: { ...EMPTY_KPI_CARD },
    withdrawableBalance: {
      ...EMPTY_KPI_CARD,
      compareLabel: '较上周',
    },
    incomeTrend: {
      last7Days: [],
      last30Days: [],
    },
    profitTrend: {
      last7Days: [],
      last30Days: [],
    },
    incomeComposition: {
      totalAmount: 0,
      items: [],
    },
    costComposition: {
      totalAmount: 0,
      items: [],
    },
    topProducts: {
      periodDays: 30,
      items: [],
    },
  };
}

/** 趋势范围枚举。 */
export const TREND_RANGE_OPTIONS: Array<{
  label: string;
  value: FinanceOverviewTrendRange;
}> = [
  { label: '近7天', value: '7' },
  { label: '近30天', value: '30' },
];
