import { Router, Request, Response } from 'express';
import { DomainRegistry } from '../../core/runtime/DomainRegistry';
import { logger } from '../../utils/logger';
import { SERVICE_CONFIGS, SERVICE_CATEGORIES, ServiceCategory } from '../../config/serviceConfig';

const router = Router();

router.get('/list', async (req: Request, res: Response) => {
  try {
    const services = DomainRegistry.getServiceStatus();
    
    const groupedServices = services.reduce((acc, service) => {
      const category = service.config?.category || 'BUSINESS';
      if (!acc[category]) {
        acc[category] = [];
      }
      acc[category].push(service);
      return acc;
    }, {} as Record<string, typeof services>);

    res.json({ 
      success: true, 
      data: {
        services,
        groupedServices,
        categories: SERVICE_CATEGORIES,
        stats: {
          total: services.length,
          enabled: services.filter(s => s.enabled).length,
          initialized: services.filter(s => s.initialized).length,
          failed: services.filter(s => s.error).length,
        }
      }
    });
  } catch (error: unknown) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error(`[ServiceManager] List error: ${errorMessage}`);
    res.status(500).json({ error: errorMessage });
  }
});

router.get('/stats', async (req: Request, res: Response) => {
  try {
    const services = DomainRegistry.getServiceStatus();
    
    const stats = {
      total: services.length,
      enabled: services.filter(s => s.enabled).length,
      disabled: services.filter(s => !s.enabled).length,
      initialized: services.filter(s => s.initialized).length,
      failed: services.filter(s => s.error).length,
      byCategory: {} as Record<string, { total: number; enabled: number; initialized: number }>,
      byMemoryImpact: {
        low: { total: 0, enabled: 0 },
        medium: { total: 0, enabled: 0 },
        high: { total: 0, enabled: 0 },
      },
    };

    services.forEach(service => {
      const category = service.config?.category || 'BUSINESS';
      if (!stats.byCategory[category]) {
        stats.byCategory[category] = { total: 0, enabled: 0, initialized: 0 };
      }
      stats.byCategory[category].total++;
      if (service.enabled) stats.byCategory[category].enabled++;
      if (service.initialized) stats.byCategory[category].initialized++;

      const memoryImpact = service.config?.memoryImpact || 'low';
      stats.byMemoryImpact[memoryImpact].total++;
      if (service.enabled) stats.byMemoryImpact[memoryImpact].enabled++;
    });

    res.json({ success: true, data: stats });
  } catch (error: unknown) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error(`[ServiceManager] Stats error: ${errorMessage}`);
    res.status(500).json({ error: errorMessage });
  }
});

router.get('/categories', async (req: Request, res: Response) => {
  try {
    res.json({ success: true, data: SERVICE_CATEGORIES });
  } catch (error: unknown) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error(`[ServiceManager] Categories error: ${errorMessage}`);
    res.status(500).json({ error: errorMessage });
  }
});

router.post('/toggle/:name', async (req: Request, res: Response) => {
  try {
    const name = String(req.params.name);
    const { enabled } = req.body;

    if (typeof enabled !== 'boolean') {
      return res.status(400).json({ error: 'Missing or invalid "enabled" field' });
    }

    const serviceConfig = SERVICE_CONFIGS.find(s => s.name === name);
    if (!serviceConfig) {
      return res.status(404).json({ error: `Service "${name}" not found` });
    }

    if (enabled) {
      DomainRegistry.enableService(name);
      logger.info(`[ServiceManager] Service enabled: ${name}`);
    } else {
      DomainRegistry.disableService(name);
      logger.info(`[ServiceManager] Service disabled: ${name}`);
    }

    res.json({ 
      success: true, 
      data: { 
        name, 
        enabled,
        message: enabled 
          ? `服务 "${name}" 已启用，重启后生效` 
          : `服务 "${name}" 已禁用，重启后生效`
      } 
    });
  } catch (error: unknown) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error(`[ServiceManager] Toggle error: ${errorMessage}`);
    res.status(500).json({ error: errorMessage });
  }
});

router.post('/batch-toggle', async (req: Request, res: Response) => {
  try {
    const { services, enabled } = req.body;

    if (!Array.isArray(services) || typeof enabled !== 'boolean') {
      return res.status(400).json({ error: 'Invalid request body' });
    }

    const results: Array<{ name: string; success: boolean; error?: string }> = [];

    for (const name of services) {
      const serviceConfig = SERVICE_CONFIGS.find(s => s.name === name);
      if (!serviceConfig) {
        results.push({ name, success: false, error: 'Service not found' });
        continue;
      }

      if (enabled) {
        DomainRegistry.enableService(name);
      } else {
        DomainRegistry.disableService(name);
      }
      results.push({ name, success: true });
    }

    logger.info(`[ServiceManager] Batch toggle: ${services.length} services ${enabled ? 'enabled' : 'disabled'}`);

    res.json({ 
      success: true, 
      data: { 
        results,
        message: `已${enabled ? '启用' : '禁用'} ${results.filter(r => r.success).length} 个服务，重启后生效`
      } 
    });
  } catch (error: unknown) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error(`[ServiceManager] Batch toggle error: ${errorMessage}`);
    res.status(500).json({ error: errorMessage });
  }
});

router.post('/apply-preset/:preset', async (req: Request, res: Response) => {
  try {
    const preset = String(req.params.preset);
    
    const presets: Record<string, string[]> = {
      minimal: SERVICE_CONFIGS
        .filter(s => s.category === 'CORE' && s.enabled)
        .map(s => s.name),
      standard: SERVICE_CONFIGS
        .filter(s => s.enabled)
        .map(s => s.name),
      full: SERVICE_CONFIGS.map(s => s.name),
      development: SERVICE_CONFIGS
        .filter(s => s.category === 'CORE' || s.category === 'BUSINESS')
        .filter(s => s.memoryImpact === 'low' || s.memoryImpact === 'medium')
        .map(s => s.name),
    };

    if (!presets[preset]) {
      return res.status(400).json({ 
        error: `Unknown preset: ${preset}. Available: ${Object.keys(presets).join(', ')}` 
      });
    }

    DomainRegistry.setEnabledServices(presets[preset]);
    
    logger.info(`[ServiceManager] Applied preset: ${preset} (${presets[preset].length} services)`);

    res.json({ 
      success: true, 
      data: { 
        preset,
        enabledCount: presets[preset].length,
        message: `已应用 "${preset}" 预设，重启后生效`
      } 
    });
  } catch (error: unknown) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error(`[ServiceManager] Apply preset error: ${errorMessage}`);
    res.status(500).json({ error: errorMessage });
  }
});

router.get('/presets', async (req: Request, res: Response) => {
  try {
    const presets = [
      {
        id: 'minimal',
        name: '最小模式',
        description: '仅启动核心服务，内存占用最低',
        serviceCount: SERVICE_CONFIGS.filter(s => s.category === 'CORE' && s.enabled).length,
      },
      {
        id: 'standard',
        name: '标准模式',
        description: '启动默认启用的服务，适合日常开发',
        serviceCount: SERVICE_CONFIGS.filter(s => s.enabled).length,
      },
      {
        id: 'development',
        name: '开发模式',
        description: '核心+业务服务，低内存占用',
        serviceCount: SERVICE_CONFIGS
          .filter(s => s.category === 'CORE' || s.category === 'BUSINESS')
          .filter(s => s.memoryImpact === 'low' || s.memoryImpact === 'medium').length,
      },
      {
        id: 'full',
        name: '完整模式',
        description: '启动所有服务，内存占用最高',
        serviceCount: SERVICE_CONFIGS.length,
      },
    ];

    res.json({ success: true, data: presets });
  } catch (error: unknown) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error(`[ServiceManager] Presets error: ${errorMessage}`);
    res.status(500).json({ error: errorMessage });
  }
});

export default router;
