/**
 * 应用全局配置中心
 * 所有可配置项集中管理，消除散落的 hardcode
 */

// ==================== 环境配置 ====================

// webpack DefinePlugin 注入的环境变量声明
declare const __APP_ENV__: string | undefined;

/** 当前运行环境 */
export const APP_ENV = (typeof __APP_ENV__ !== 'undefined' ? __APP_ENV__ : 'development') as 'development' | 'production';

/** 是否为开发环境 */
export const IS_DEV = APP_ENV === 'development';

/** 是否为生产环境 */
export const IS_PROD = APP_ENV === 'production';

// ==================== 应用信息 ====================

export const APP_CONFIG = {
  /** 应用名称 */
  name: '昱森',
  /** Slogan */
  slogan: '以AI为梭 织天地经纬',
  /** 定位描述 */
  description: '纺织行业全流程协作管理平台',
  /** 版本号 */
  version: '2.2.0',
  /** 默认密码（仅 Demo 环境） */
  defaultDemoPassword: '123456',
  /** 免密登录有效期（毫秒），默认30天 */
  autoLoginExpiryMs: 30 * 24 * 60 * 60 * 1000,
} as const;

// ==================== 演示账号配置 ====================

export interface DemoAccount {
  username: string;
  label: string;
  color: string;
  icon: string;
  enabled: boolean;
}

/**
 * 演示账号列表
 * 仅在非生产环境或显式启用时显示
 */
export const DEMO_ACCOUNTS: DemoAccount[] = [
  { username: 'purchaser', label: '采购商（布行）', color: 'from-blue-500 to-blue-600', icon: 'Building2', enabled: true },
  { username: 'textile', label: '纺织厂', color: 'from-emerald-500 to-green-600', icon: 'Factory', enabled: true },
  { username: 'washing', label: '水洗厂', color: 'from-violet-400 to-fuchsia-400', icon: 'Droplets', enabled: true },
];

/** 是否显示演示账号入口 */
export const SHOW_DEMO_ACCOUNTS = true;

// ==================== 缓存配置 ====================

export const CACHE_CONFIG = {
  /** 计划数据缓存时长（毫秒） */
  planDataDuration: 10_000,
  /** 库存数据缓存时长（毫秒） */
  inventoryDataDuration: 5_000,
  /** Dashboard 数据缓存时长（毫秒） */
  dashboardDataDuration: 10_000,
} as const;

// ==================== 分页配置 ====================

export const PAGINATION_CONFIG = {
  /** 默认每页条数 */
  defaultPageSize: 20,
  /** 入库记录最大加载数 */
  maxInventoryRecords: 100,
  /** 价格历史最大加载数 */
  maxPriceHistory: 50,
} as const;

// ==================== 同步与轮询配置 ====================

export const SYNC_CONFIG = {
  /** 计划状态同步间隔（毫秒）- 测试用10秒，生产环境应改为 5 * 60 * 1000 */
  planStatusSyncInterval: IS_DEV ? 10_000 : 5 * 60_000,
  /** Realtime 重连延迟（毫秒） */
  realtimeReconnectDelay: 3_000,
} as const;

// ==================== 动画配置 ====================

export const ANIMATION_CONFIG = {
  /** 标准过渡 */
  transition: {
    duration: 0.25,
    ease: [0.4, 0, 0.2, 1] as number[],
  },
  /** 快速过渡 */
  fastTransition: {
    duration: 0.2,
    ease: [0.4, 0, 0.2, 1] as number[],
  },
  /** 慢速过渡 */
  slowTransition: {
    duration: 0.4,
    ease: [0.4, 0, 0.2, 1] as number[],
  },
  /** 弹簧动画 */
  spring: {
    type: 'spring' as const,
    stiffness: 200,
    damping: 25,
    mass: 0.8,
  },
  /** 页面过渡 */
  pageTransition: {
    duration: 0.35,
    ease: [0.25, 0.46, 0.45, 0.94] as number[],
  },
  /** 列表交错延迟 */
  staggerDelay: 0.08,
} as const;

// ==================== 进度阈值 ====================

export const PROGRESS_THRESHOLDS = {
  /** 计划完成阈值（百分比） */
  complete: 97,
  /** 低进度警告阈值 */
  lowWarning: 30,
  /** 中进度提示阈值 */
  mediumNotice: 60,
} as const;

// ==================== 角色主题色配置 ====================

export type RoleTheme = 'amber' | 'emerald' | 'violet' | 'blue';

export interface ThemeColors {
  primary: string;
  gradient: string;
  button: string;
  buttonHover: string;
  text: string;
  bg: string;
  bgHover: string;
  border: string;
  ring: string;
  notificationBg: string;
  notificationIcon: string;
}

export const ROLE_THEMES: Record<string, ThemeColors> = {
  purchaser: {
    primary: 'amber-400',
    gradient: 'from-amber-400 to-orange-500',
    button: 'bg-amber-400',
    buttonHover: 'hover:bg-amber-500',
    text: 'text-amber-600',
    bg: 'bg-amber-50',
    bgHover: 'hover:bg-amber-100',
    border: 'border-amber-200',
    ring: 'focus:ring-amber-400',
    notificationBg: 'bg-amber-100',
    notificationIcon: 'text-amber-700',
  },
  textile: {
    primary: 'emerald-400',
    gradient: 'from-emerald-400 to-teal-400',
    button: 'bg-emerald-400',
    buttonHover: 'hover:bg-emerald-500',
    text: 'text-emerald-600',
    bg: 'bg-emerald-50',
    bgHover: 'hover:bg-emerald-100',
    border: 'border-emerald-200',
    ring: 'focus:ring-emerald-400',
    notificationBg: 'bg-emerald-100',
    notificationIcon: 'text-emerald-700',
  },
  washing: {
    primary: 'violet-400',
    gradient: 'from-violet-400 to-fuchsia-400',
    button: 'bg-violet-400',
    buttonHover: 'hover:bg-violet-500',
    text: 'text-violet-600',
    bg: 'bg-violet-50',
    bgHover: 'hover:bg-violet-100',
    border: 'border-violet-200',
    ring: 'focus:ring-violet-400',
    notificationBg: 'bg-violet-100',
    notificationIcon: 'text-violet-500',
  },
  system: {
    primary: 'blue-500',
    gradient: 'from-blue-500 to-blue-600',
    button: 'bg-blue-500',
    buttonHover: 'hover:bg-blue-600',
    text: 'text-blue-600',
    bg: 'bg-blue-50',
    bgHover: 'hover:bg-blue-100',
    border: 'border-blue-200',
    ring: 'focus:ring-blue-500',
    notificationBg: 'bg-blue-100',
    notificationIcon: 'text-blue-700',
  },
};

/** 根据角色获取主题色 */
export function getRoleTheme(role: string): ThemeColors {
  return ROLE_THEMES[role] || ROLE_THEMES.system;
}

// ==================== 文件上传配置 ====================

export const UPLOAD_CONFIG = {
  /** 产品图片最大大小（字节）- 5MB */
  maxImageSize: 5 * 1024 * 1024,
  /** 允许的图片类型 */
  allowedImageTypes: ['image/jpeg', 'image/png', 'image/webp'],
  /** 头像存储桶 */
  avatarBucket: 'avatars',
  /** 产品图片存储桶 */
  productImageBucket: 'product_images',
} as const;

// ==================== 分享链接配置 ====================

export const SHARE_CONFIG = {
  /** 链接有效期（毫秒）- 30分钟 */
  linkExpiryMs: 30 * 60 * 1000,
  /** 短码长度 */
  shortCodeLength: 8,
} as const;

// ==================== LocalStorage Key 常量 ====================

export const STORAGE_KEYS = {
  savedUsername: 'saved_username',
  savedPassword: 'saved_password',
  autoLogin: 'auto_login',
  autoLoginExpiry: 'auto_login_expiry',
  textileRejectedPlans: 'textile_rejected_plans',
  viewedRejectedModals: 'viewed_rejected_modals',
  onboardingCompleted: 'onboarding_completed',
  themePreference: 'theme',
  performanceTier: 'performance_tier',
  demoDisclaimerShown: 'demo_disclaimer_shown',
  appVersion: 'app_version',
  updateModalShown: 'update_modal_shown',
  purchaserSidebarCollapsed: 'purchaser_sidebar_collapsed',
  textileSidebarCollapsed: 'textile_sidebar_collapsed',
  washingSidebarCollapsed: 'washing_sidebar_collapsed',
} as const;
