import type {
  FinanceCostAnalysisState,
  FinanceCostCategoryViewModel,
  FinanceCostEntryState,
  FinanceCostFilterState,
  FinanceCostTabKey,
} from '../../types';

/**
 * 文件职责：成本管理页面常量与默认状态定义。
 */
import type { FinanceCostDimension } from '#/api/finance/cost';

/** 成本管理查看权限。 */
export const FINANCE_COST_VIEW_PERMISSION = 'tenant:finance:cost:view';

/** 成本管理维护权限。 */
export const FINANCE_COST_MANAGE_PERMISSION = 'tenant:finance:cost:manage';

/** 页面 Tab 选项。 */
export const COST_TAB_OPTIONS: Array<{
  label: string;
  value: FinanceCostTabKey;
}> = [
  { label: '成本录入', value: 'entry' },
  { label: '成本分析', value: 'analysis' },
];

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

/** 分类颜色映射。 */
export const COST_CATEGORY_COLOR_MAP: Record<string, string> = {
  food: '#2563eb',
  labor: '#16a34a',
  fixed: '#ca8a04',
  packaging: '#db2777',
};

/** 分类图标映射。 */
export const COST_CATEGORY_ICON_MAP: Record<string, string> = {
  food: 'lucide:utensils-crossed',
  labor: 'lucide:users',
  fixed: 'lucide:building-2',
  packaging: 'lucide:package',
};

/** 默认录入状态。 */
export const DEFAULT_ENTRY_STATE: FinanceCostEntryState = {
  monthRevenue: 0,
  totalCost: 0,
  costRate: 0,
  categories: [],
};

/** 默认分析状态。 */
export const DEFAULT_ANALYSIS_STATE: FinanceCostAnalysisState = {
  stats: {
    totalCost: 0,
    foodCostRate: 0,
    averageCostPerPaidOrder: 0,
    monthOnMonthChangeRate: 0,
    revenue: 0,
    paidOrderCount: 0,
  },
  trend: [],
  composition: [],
  detailRows: [],
};

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

/** 趋势月数。 */
export const TREND_MONTH_COUNT = 6;

/** 复制分类列表并重置展开状态。 */
export function cloneCategoriesWithExpandState(
  categories: FinanceCostCategoryViewModel[],
) {
  return categories.map((item) => ({
    ...item,
    items: [...item.items],
    expanded: Boolean(item.expanded),
  }));
}
