/**
 * [PLUGIN-001] 插拔式功能模块架构
 * 
 * 核心设计原则:
 * 1. 每个功能模块独立定义，可单独启用/禁用
 * 2. 功能模块包含: 路由、菜单、权限、组件
 * 3. 通过 FeatureRegistry 统一注册和管理
 * 4. 基于订阅计划和用户权限动态控制访问
 * 
 * @module plugins/FeatureTypes
 * @created 2026-03-21
 */

import type { ReactNode, ComponentType } from 'react';

export type FeatureStatus = 'active' | 'inactive' | 'pending' | 'expired';

export type FeatureTier = 'free' | 'basic' | 'pro' | 'enterprise';

export interface FeatureDependency {
  featureId: string;
  required?: boolean;
  minVersion?: string;
}

export interface FeatureRoute {
  path: string;
  component: ComponentType<any>;
  exact?: boolean;
  children?: FeatureRoute[];
}

export interface FeatureMenuItem {
  key: string;
  label: ReactNode;
  icon?: ReactNode;
  path?: string;
  children?: FeatureMenuItem[];
  requiredPermission?: string | null;
  requiredFeature?: string;
  badge?: ReactNode;
}

export interface FeaturePermission {
  key: string;
  label: string;
  description: string;
  defaultRoles?: string[];
}

export interface FeatureConfig {
  id: string;
  name: string;
  description: string;
  version: string;
  icon: ReactNode;
  category: FeatureCategory;
  
  tier: FeatureTier[];
  status: FeatureStatus;
  
  dependencies?: FeatureDependency[];
  conflicts?: string[];
  
  routes?: FeatureRoute[];
  menus?: FeatureMenuItem[];
  permissions?: FeaturePermission[];
  
  settings?: FeatureSetting[];
  
  hooks?: {
    onEnable?: () => Promise<void>;
    onDisable?: () => Promise<void>;
    onUpgrade?: (fromVersion: string) => Promise<void>;
  };
  
  i18n?: {
    [locale: string]: {
      name: string;
      description: string;
    };
  };
}

export type FeatureCategory = 
  | 'ai'
  | 'product'
  | 'order'
  | 'logistics'
  | 'finance'
  | 'marketing'
  | 'analytics'
  | 'b2b'
  | 'independent_site'
  | 'compliance'
  | 'system';

export interface FeatureSetting {
  key: string;
  type: 'boolean' | 'string' | 'number' | 'select' | 'multiselect';
  label: string;
  description?: string;
  defaultValue: any;
  options?: { label: string; value: any }[];
  validation?: {
    required?: boolean;
    min?: number;
    max?: number;
    pattern?: string;
  };
}

export interface FeatureState {
  enabled: boolean;
  installed: boolean;
  version: string;
  settings: Record<string, any>;
  installedAt?: string;
  updatedAt?: string;
}

export interface FeatureRegistryOptions {
  strictMode?: boolean;
  autoEnable?: boolean;
  logLevel?: 'debug' | 'info' | 'warn' | 'error';
}

export interface IFeatureRegistry {
  register(feature: FeatureConfig): void;
  unregister(featureId: string): void;
  getFeature(featureId: string): FeatureConfig | undefined;
  getFeatureState(featureId: string): FeatureState | undefined;
  getAllFeatures(): FeatureConfig[];
  getEnabledFeatures(): FeatureConfig[];
  getFeaturesByCategory(category: FeatureCategory): FeatureConfig[];
  
  enableFeature(featureId: string): Promise<boolean>;
  disableFeature(featureId: string): Promise<boolean>;
  isFeatureEnabled(featureId: string): boolean;
  
  checkDependencies(featureId: string): { satisfied: boolean; missing: string[] };
  checkConflicts(featureId: string): { hasConflicts: boolean; conflicts: string[] };
  
  getRoutes(): FeatureRoute[];
  getMenus(): FeatureMenuItem[];
  getPermissions(): FeaturePermission[];
  
  updateSettings(featureId: string, settings: Record<string, any>): void;
  getSettings(featureId: string): Record<string, any>;
}

export const FEATURE_EVENTS = {
  FEATURE_REGISTERED: 'feature:registered',
  FEATURE_UNREGISTERED: 'feature:unregistered',
  FEATURE_ENABLED: 'feature:enabled',
  FEATURE_DISABLED: 'feature:disabled',
  FEATURE_UPDATED: 'feature:updated',
  SETTINGS_CHANGED: 'feature:settings_changed',
} as const;
