/**
 * 牛只类型控制器
 * 处理cattle_type表的CRUD操作
 */

const { sequelize } = require('../config/database-simple')

class CattleTypeController {
  /**
   * 获取所有牛只类型
   */
  async getAllTypes(req, res) {
    try {
      const [types] = await sequelize.query('SELECT * FROM cattle_type ORDER BY id ASC')
      
      res.json({
        success: true,
        data: types,
        message: '获取牛只类型列表成功'
      })
    } catch (error) {
      console.error('获取牛只类型列表失败:', error)
      res.status(500).json({
        success: false,
        message: '获取牛只类型列表失败',
        error: error.message
      })
    }
  }

  /**
   * 根据ID获取牛只类型
   */
  async getTypeById(req, res) {
    try {
      const { id } = req.params
      const [types] = await sequelize.query('SELECT * FROM cattle_type WHERE id = ?', [id])
      
      if (types.length === 0) {
        return res.status(404).json({
          success: false,
          message: '牛只类型不存在'
        })
      }

      res.json({
        success: true,
        data: types[0],
        message: '获取牛只类型成功'
      })
    } catch (error) {
      console.error('获取牛只类型失败:', error)
      res.status(500).json({
        success: false,
        message: '获取牛只类型失败',
        error: error.message
      })
    }
  }

  /**
   * 创建牛只类型
   */
  async createType(req, res) {
    try {
      const { name, description } = req.body

      if (!name) {
        return res.status(400).json({
          success: false,
          message: '类型名称不能为空'
        })
      }

      const [result] = await sequelize.query(
        'INSERT INTO cattle_type (name, description) VALUES (?, ?)',
        [name, description || null]
      )

      res.status(201).json({
        success: true,
        data: { id: result.insertId, name, description },
        message: '创建牛只类型成功'
      })
    } catch (error) {
      console.error('创建牛只类型失败:', error)
      res.status(500).json({
        success: false,
        message: '创建牛只类型失败',
        error: error.message
      })
    }
  }

  /**
   * 更新牛只类型
   */
  async updateType(req, res) {
    try {
      const { id } = req.params
      const { name, description } = req.body

      if (!name) {
        return res.status(400).json({
          success: false,
          message: '类型名称不能为空'
        })
      }

      const [result] = await sequelize.query(
        'UPDATE cattle_type SET name = ?, description = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
        [name, description || null, id]
      )

      if (result.affectedRows === 0) {
        return res.status(404).json({
          success: false,
          message: '牛只类型不存在'
        })
      }

      res.json({
        success: true,
        data: { id: parseInt(id), name, description },
        message: '更新牛只类型成功'
      })
    } catch (error) {
      console.error('更新牛只类型失败:', error)
      res.status(500).json({
        success: false,
        message: '更新牛只类型失败',
        error: error.message
      })
    }
  }

  /**
   * 删除牛只类型
   */
  async deleteType(req, res) {
    try {
      const { id } = req.params

      const [result] = await sequelize.query('DELETE FROM cattle_type WHERE id = ?', [id])

      if (result.affectedRows === 0) {
        return res.status(404).json({
          success: false,
          message: '牛只类型不存在'
        })
      }

      res.json({
        success: true,
        message: '删除牛只类型成功'
      })
    } catch (error) {
      console.error('删除牛只类型失败:', error)
      res.status(500).json({
        success: false,
        message: '删除牛只类型失败',
        error: error.message
      })
    }
  }
}

module.exports = new CattleTypeController()
