/**
 * 文件职责：分类管理页面类型与常量定义。
 * 1. 统一页面、组件与组合式函数共享的类型。
 * 2. 维护渠道展示元数据与格式化工具。
 */
import type {
  ProductCategoryChannel,
  ProductPickerItemDto,
  ProductStatus,
  ProductSwitchStatus,
} from '#/api/product';

export type ChannelFilter = 'all' | ProductCategoryChannel;
export type DrawerMode = 'create' | 'edit';

export interface CategoryFormModel {
  channels: ProductCategoryChannel[];
  description: string;
  icon: string;
  id: string;
  name: string;
  sort: number;
  status: ProductSwitchStatus;
}

export interface CategoryStatsViewModel {
  dineInCount: number;
  enabledCategories: number;
  pickupCount: number;
  totalCategories: number;
  totalProducts: number;
  wmCount: number;
}

export interface CategoryChannelMeta {
  cardClass: 'ts' | 'wm' | 'zt';
  filterClass: 'ts' | 'wm' | 'zt';
  icon: string;
  label: string;
  shortLabel: string;
  subText: string;
}

export interface ProductPreviewItem extends ProductPickerItemDto {
  imageUrl?: string;
  monthlySales: number;
}

export interface StoreOption {
  label: string;
  value: string;
}

export const CATEGORY_CHANNEL_ORDER: ProductCategoryChannel[] = [
  'wm',
  'pickup',
  'dine_in',
];

export const CATEGORY_CHANNEL_META: Record<
  ProductCategoryChannel,
  CategoryChannelMeta
> = {
  wm: {
    icon: 'lucide:bike',
    label: '外卖',
    shortLabel: '外',
    subText: '外卖平台展示此分类',
    filterClass: 'wm',
    cardClass: 'wm',
  },
  pickup: {
    icon: 'lucide:shopping-bag',
    label: '自提',
    shortLabel: '自',
    subText: '到店自提展示此分类',
    filterClass: 'zt',
    cardClass: 'zt',
  },
  dine_in: {
    icon: 'lucide:utensils',
    label: '堂食',
    shortLabel: '堂',
    subText: '堂食扫码点餐展示此分类',
    filterClass: 'ts',
    cardClass: 'ts',
  },
};

/** 获取商品状态文案。 */
export function getProductStatusText(status: ProductStatus) {
  if (status === 'on_sale') return '在售';
  if (status === 'sold_out') return '售罄';
  return '下架';
}

/** 获取商品状态样式。 */
export function getProductStatusClass(status: ProductStatus) {
  if (status === 'on_sale') return 'on-sale';
  if (status === 'sold_out') return 'sold-out';
  return 'off-shelf';
}

/** 金额格式化。 */
export function formatPrice(price: number) {
  return `¥${price.toFixed(2)}`;
}

/** 稳定生成月销预览值。 */
export function deriveMonthlySales(seed: string) {
  let hash = 0;
  for (const char of seed) {
    hash = (hash * 31 + (char.codePointAt(0) ?? 0)) % 10_007;
  }
  return (hash % 160) + 20;
}
