const { BookingRule } = require('../models');

/**
 * 获取所有预约规则
 */
const getAllRules = async (req, res) => {
  try {
    const rules = await BookingRule.findAll({
      order: [['createdAt', 'ASC']]
    });
    
    res.json({
      code: 0,
      message: 'success',
      data: rules
    });
  } catch (error) {
    console.error('Get all booking rules error:', error);
    res.status(500).json({
      code: 500,
      message: '获取预约规则失败',
      data: null
    });
  }
};

/**
 * 根据服务类型获取规则
 */
const getRuleByServiceType = async (req, res) => {
  try {
    const { serviceType } = req.params;
    
    const rule = await BookingRule.findOne({
      where: { serviceType }
    });
    
    res.json({
      code: 0,
      message: 'success',
      data: rule
    });
  } catch (error) {
    console.error('Get booking rule error:', error);
    res.status(500).json({
      code: 500,
      message: '获取预约规则失败',
      data: null
    });
  }
};

/**
 * 创建或更新预约规则
 */
const upsertRule = async (req, res) => {
  try {
    const { serviceType } = req.params;
    const { rules, status } = req.body;
    
    const [rule, created] = await BookingRule.upsert({
      serviceType,
      rules: rules || '',
      status: status || 'active'
    }, {
      returning: true
    });
    
    res.json({
      code: 0,
      message: created ? '创建成功' : '更新成功',
      data: rule
    });
  } catch (error) {
    console.error('Upsert booking rule error:', error);
    res.status(500).json({
      code: 500,
      message: '保存预约规则失败',
      data: null
    });
  }
};

/**
 * 批量更新预约规则
 */
const batchUpsertRules = async (req, res) => {
  try {
    const { rules } = req.body; // [{ serviceType, rules, status }]
    
    if (!Array.isArray(rules)) {
      return res.status(400).json({
        code: 400,
        message: '参数格式错误',
        data: null
      });
    }
    
    const results = [];
    for (const item of rules) {
      const [rule] = await BookingRule.upsert({
        serviceType: item.serviceType,
        rules: item.rules || '',
        status: item.status || 'active'
      }, {
        returning: true
      });
      results.push(rule);
    }
    
    res.json({
      code: 0,
      message: '批量保存成功',
      data: results
    });
  } catch (error) {
    console.error('Batch upsert booking rules error:', error);
    res.status(500).json({
      code: 500,
      message: '批量保存预约规则失败',
      data: null
    });
  }
};

/**
 * 删除预约规则
 */
const deleteRule = async (req, res) => {
  try {
    const { serviceType } = req.params;
    
    const deleted = await BookingRule.destroy({
      where: { serviceType }
    });
    
    if (deleted) {
      res.json({
        code: 0,
        message: '删除成功',
        data: null
      });
    } else {
      res.status(404).json({
        code: 404,
        message: '规则不存在',
        data: null
      });
    }
  } catch (error) {
    console.error('Delete booking rule error:', error);
    res.status(500).json({
      code: 500,
      message: '删除预约规则失败',
      data: null
    });
  }
};

module.exports = {
  getAllRules,
  getRuleByServiceType,
  upsertRule,
  batchUpsertRules,
  deleteRule
};
