import { Request, Response } from 'express';
import { designService } from '../services/design.service';

class DesignController {
  // 获取颜色统计数据
  async getColorStatistics(req: Request, res: Response) {
    try {
      const stats = await designService.getAllColorsStatistics();
      res.json({
        success: true,
        data: stats
      });
    } catch (error) {
      const err = error as Error;
      console.error('获取颜色统计失败:', err.message);
      res.status(500).json({ 
        success: false,
        error: '获取颜色统计数据失败'
      });
    }
  }
  
  // 记录设计使用
  async recordDesignUsage(req: Request, res: Response) {
    try {
      const design = {
        part1: req.body.part1,
        part2: req.body.part2,
        part3: req.body.part3,
        part4: req.body.part4,
      };
      
      await designService.saveDesign(design);
      res.json({ success: true });
    } catch (error) {
      const err = error as Error;
      console.error('记录设计使用失败:', err.message);
      res.status(400).json({ 
        success: false,
        error: err.message
      });
    }
  }
}

export const designController = new DesignController();