const AnimalService = require('../../services/animal');
const { success } = require('../../utils/response');
const { AppError } = require('../../utils/errors');

class AnimalController {
  // 获取动物列表
  static async getAnimals(req, res, next) {
    try {
      const { page, pageSize, species, status } = req.query;
      
      const result = await AnimalService.getAnimals({
        merchantId: req.userId,
        page: parseInt(page) || 1,
        pageSize: parseInt(pageSize) || 10,
        species,
        status
      });

      res.json(success(result));
    } catch (error) {
      next(error);
    }
  }

  // 获取单个动物详情
  static async getAnimal(req, res, next) {
    try {
      const { animalId } = req.params;
      
      if (!animalId) {
        throw new AppError('动物ID不能为空', 400);
      }

      const animal = await AnimalService.getAnimalById(animalId);
      res.json(success({ animal }));
    } catch (error) {
      next(error);
    }
  }

  // 创建动物
  static async createAnimal(req, res, next) {
    try {
      const {
        name,
        species,
        breed,
        age,
        gender,
        price,
        description,
        images,
        health_status,
        vaccination_status
      } = req.body;

      // 验证必要字段
      if (!name || !species || !price) {
        throw new AppError('缺少必要字段: name, species, price', 400);
      }

      const animalData = {
        merchant_id: req.userId,
        name,
        species,
        breed: breed || null,
        age: age || null,
        gender: gender || null,
        price: parseFloat(price),
        description: description || null,
        images: images || null,
        health_status: health_status || null,
        vaccination_status: vaccination_status || null,
        status: 'available'
      };

      const animal = await AnimalService.createAnimal(animalData);
      res.status(201).json(success({ animal }));
    } catch (error) {
      next(error);
    }
  }

  // 更新动物信息
  static async updateAnimal(req, res, next) {
    try {
      const { animalId } = req.params;
      const updateData = req.body;

      if (!animalId) {
        throw new AppError('动物ID不能为空', 400);
      }

      const animal = await AnimalService.updateAnimal(animalId, updateData);
      res.json(success({ animal }));
    } catch (error) {
      next(error);
    }
  }

  // 删除动物
  static async deleteAnimal(req, res, next) {
    try {
      const { animalId } = req.params;

      if (!animalId) {
        throw new AppError('动物ID不能为空', 400);
      }

      await AnimalService.deleteAnimal(animalId);
      res.json(success({ message: '动物删除成功' }));
    } catch (error) {
      next(error);
    }
  }

  // 获取动物统计信息
  static async getAnimalStatistics(req, res, next) {
    try {
      const statistics = await AnimalService.getAnimalStatistics();
      res.json(success({ statistics }));
    } catch (error) {
      next(error);
    }
  }

  // 搜索动物
  static async searchAnimals(req, res, next) {
    try {
      const { keyword, species, minPrice, maxPrice, page, pageSize } = req.query;
      
      const result = await AnimalService.searchAnimals({
        keyword,
        species,
        minPrice: minPrice ? parseFloat(minPrice) : null,
        maxPrice: maxPrice ? parseFloat(maxPrice) : null,
        page: parseInt(page) || 1,
        pageSize: parseInt(pageSize) || 10
      });

      res.json(success(result));
    } catch (error) {
      next(error);
    }
  }

  // 获取所有动物（管理员）
  static async getAllAnimals(req, res, next) {
    try {
      const { page, pageSize, species, status } = req.query;
      
      const result = await AnimalService.getAnimals({
        page: parseInt(page) || 1,
        pageSize: parseInt(pageSize) || 10,
        species,
        status
      });

      res.json(success(result));
    } catch (error) {
      next(error);
    }
  }
}

module.exports = AnimalController;