import { Request, Response } from "express";
import * as messageStrategyService from "../services/messageStrategyService";

class MessageStrategyController {
  /**
   * @route GET /api/message-strategies
   * @desc 获取所有消息策略
   * @access Private
   */
  public async getStrategies(req: Request, res: Response): Promise<Response> {
    try {
      const strategies = await messageStrategyService.getStrategies();
      return res.status(200).json(strategies);
    } catch (error: any) {
      console.error("获取消息策略失败:", error);
      return res.status(500).json({ success: false, message: "无法获取消息策略列表。" });
    }
  }

  /**
   * @route GET /api/message-strategy/:id
   * @desc 根据ID获取单个消息策略
   * @access Private
   */
  public async getStrategyById(req: Request, res: Response): Promise<Response> {
    try {
      const { id } = req.params;
      const strategy = await messageStrategyService.getStrategyById(id);

      if (!strategy) {
        return res.status(404).json({ success: false, message: "消息策略未找到。" });
      }

      return res.status(200).json({ success: true, data: strategy });
    } catch (error: any) {
      console.error(`获取消息策略失败 (ID: ${req.params.id}):`, error);
      return res.status(500).json({ success: false, message: "无法获取消息策略。" });
    }
  }

  /**
   * @route POST /api/message-strategy
   * @desc 创建一个新的消息策略
   * @access Private
   */
  public async createStrategy(req: Request, res: Response): Promise<Response> {
    try {
      const strategyData = req.body;
      const newStrategy = await messageStrategyService.createStrategy(strategyData);
      return res.status(201).json({ success: true, data: newStrategy });
    } catch (error: any) {
      console.error("创建消息策略失败:", error);
      return res.status(500).json({ success: false, message: "创建消息策略失败。" });
    }
  }

  /**
   * @route PUT /api/message-strategy/:id
   * @desc 更新一个消息策略
   * @access Private
   */
  public async updateStrategy(req: Request, res: Response): Promise<Response> {
    try {
      const { id } = req.params;
      const updateData = req.body;
      const updatedStrategy = await messageStrategyService.updateStrategy(id, updateData);

      if (!updatedStrategy) {
        return res.status(404).json({ success: false, message: "消息策略未找到。" });
      }

      return res.status(200).json({ success: true, data: updatedStrategy });
    } catch (error: any) {
      console.error(`更新消息策略失败 (ID: ${req.params.id}):`, error);
      return res.status(500).json({ success: false, message: "更新消息策略失败。" });
    }
  }

  /**
   * @route DELETE /api/message-strategy/:id
   * @desc 删除一个消息策略
   * @access Private
   */
  public async deleteStrategy(req: Request, res: Response): Promise<Response> {
    try {
      const { id } = req.params;
      const deletedStrategy = await messageStrategyService.deleteStrategy(id);

      if (!deletedStrategy) {
        return res.status(404).json({ success: false, message: "消息策略未找到。" });
      }

      return res.status(200).json({ success: true, message: "消息策略已成功删除。" });
    } catch (error: any) {
      console.error(`删除消息策略失败 (ID: ${req.params.id}):`, error);
      return res.status(500).json({ success: false, message: "删除消息策略失败。" });
    }
  }
}

export default new MessageStrategyController();
