package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.common.Result;
import com.zhentao.entity.PointsProduct;
import com.zhentao.service.PointsProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 积分商品管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin/points-product")
@CrossOrigin(origins = "*")
public class PointsProductController {
    
    private final PointsProductService pointsProductService;
    
    public PointsProductController(PointsProductService pointsProductService) {
        this.pointsProductService = pointsProductService;
    }
    
    /**
     * 分页查询积分商品列表
     */
    @GetMapping("/list")
    public Result<Page<PointsProduct>> list(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "category", required = false) Integer category) {
        
        log.info("查询积分商品列表: page={}, size={}, keyword={}, status={}, category={}", 
                page, size, keyword, status, category);
        
        try {
            Page<PointsProduct> result = pointsProductService.getProductList(page, size, keyword, status, category);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询积分商品列表失败", e);
            return Result.error("查询失败");
        }
    }
    
    /**
     * 获取商品详情
     */
    @GetMapping("/detail/{id}")
    public Result<PointsProduct> detail(@PathVariable Long id) {
        log.info("查询积分商品详情: id={}", id);
        
        try {
            PointsProduct product = pointsProductService.getProductById(id);
            if (product == null) {
                return Result.error("商品不存在");
            }
            return Result.success(product);
        } catch (Exception e) {
            log.error("查询积分商品详情失败", e);
            return Result.error("查询失败");
        }
    }
    
    /**
     * 创建商品
     */
    @PostMapping("/create")
    public Result<String> create(@RequestBody PointsProduct product) {
        log.info("创建积分商品: name={}", product.getName());
        
        try {
            boolean success = pointsProductService.createProduct(product);
            if (success) {
                return Result.success("创建成功");
            } else {
                return Result.error("创建失败");
            }
        } catch (Exception e) {
            log.error("创建积分商品失败", e);
            return Result.error("创建失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新商品
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody PointsProduct product) {
        log.info("更新积分商品: id={}", product.getId());
        
        try {
            if (product.getId() == null) {
                return Result.error("商品ID不能为空");
            }
            
            boolean success = pointsProductService.updateProduct(product);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            log.error("更新积分商品失败", e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除商品
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除积分商品: id={}", id);
        
        try {
            boolean success = pointsProductService.deleteProduct(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除积分商品失败", e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 上架商品
     */
    @PutMapping("/on-shelf/{id}")
    public Result<String> onShelf(@PathVariable Long id) {
        log.info("上架积分商品: id={}", id);
        
        try {
            boolean success = pointsProductService.updateStatus(id, 1);
            if (success) {
                return Result.success("上架成功");
            } else {
                return Result.error("上架失败");
            }
        } catch (Exception e) {
            log.error("上架积分商品失败", e);
            return Result.error("上架失败: " + e.getMessage());
        }
    }
    
    /**
     * 下架商品
     */
    @PutMapping("/off-shelf/{id}")
    public Result<String> offShelf(@PathVariable Long id) {
        log.info("下架积分商品: id={}", id);
        
        try {
            boolean success = pointsProductService.updateStatus(id, 0);
            if (success) {
                return Result.success("下架成功");
            } else {
                return Result.error("下架失败");
            }
        } catch (Exception e) {
            log.error("下架积分商品失败", e);
            return Result.error("下架失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新商品状态（通用接口）
     */
    @PutMapping("/status")
    public Result<String> updateStatus(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        
        log.info("更新积分商品状态: id={}, status={}", id, status);
        
        try {
            boolean success = pointsProductService.updateStatus(id, status);
            if (success) {
                return Result.success("状态更新成功");
            } else {
                return Result.error("状态更新失败");
            }
        } catch (Exception e) {
            log.error("更新积分商品状态失败", e);
            return Result.error("状态更新失败: " + e.getMessage());
        }
    }
}

