package com.zhentao.controller;

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

import java.util.List;
import java.util.Map;

/**
 * VIP套餐管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin/vip")
@CrossOrigin(origins = "*")
public class VipPackageController {
    
    private final VipPackageService vipPackageService;
    
    public VipPackageController(VipPackageService vipPackageService) {
        this.vipPackageService = vipPackageService;
    }
    
    /**
     * 分页查询VIP套餐列表
     */
    @GetMapping("/package/list")
    public Result<Page<VipPackage>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status) {
        
        log.info("查询VIP套餐列表: page={}, size={}, keyword={}, status={}", page, size, keyword, status);
        
        try {
            Page<VipPackage> result = vipPackageService.getPackageList(page, size, keyword, status);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询VIP套餐列表失败", e);
            return Result.error("查询失败");
        }
    }
    
    /**
     * 获取所有有效套餐
     */
    @GetMapping("/package/active")
    public Result<List<VipPackage>> getActivePackages() {
        log.info("获取所有有效VIP套餐");
        
        try {
            List<VipPackage> result = vipPackageService.getAllActivePackages();
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取有效VIP套餐失败", e);
            return Result.error("查询失败");
        }
    }
    
    /**
     * 获取VIP套餐详情
     */
    @GetMapping("/package/detail/{id}")
    public Result<VipPackage> detail(@PathVariable Long id) {
        log.info("查询VIP套餐详情: id={}", id);
        
        try {
            VipPackage vipPackage = vipPackageService.getPackageById(id);
            if (vipPackage == null) {
                return Result.error("VIP套餐不存在");
            }
            return Result.success(vipPackage);
        } catch (Exception e) {
            log.error("查询VIP套餐详情失败", e);
            return Result.error("查询失败");
        }
    }
    
    /**
     * 创建VIP套餐
     */
    @PostMapping("/package/create")
    public Result<String> create(@RequestBody VipPackage vipPackage) {
        log.info("创建VIP套餐: packageName={}", vipPackage.getPackageName());
        
        try {
            boolean success = vipPackageService.createPackage(vipPackage);
            if (success) {
                return Result.success("创建成功");
            } else {
                return Result.error("创建失败");
            }
        } catch (Exception e) {
            log.error("创建VIP套餐失败", e);
            return Result.error("创建失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新VIP套餐
     */
    @PutMapping("/package/update")
    public Result<String> update(@RequestBody VipPackage vipPackage) {
        log.info("更新VIP套餐: id={}", vipPackage.getPackageId());
        
        try {
            if (vipPackage.getPackageId() == null) {
                return Result.error("套餐ID不能为空");
            }
            
            boolean success = vipPackageService.updatePackage(vipPackage);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            log.error("更新VIP套餐失败", e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除VIP套餐
     */
    @DeleteMapping("/package/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除VIP套餐: id={}", id);
        
        try {
            boolean success = vipPackageService.deletePackage(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除VIP套餐失败", e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量删除VIP套餐
     */
    @DeleteMapping("/package/batchDelete")
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        log.info("批量删除VIP套餐: ids={}", ids);
        
        try {
            boolean success = vipPackageService.batchDeletePackage(ids);
            if (success) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            log.error("批量删除VIP套餐失败", e);
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新VIP套餐状态
     */
    @PutMapping("/package/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("更新VIP套餐状态: id={}, status={}", id, status);
        
        try {
            boolean success = vipPackageService.updateStatus(id, status);
            if (success) {
                return Result.success("状态更新成功");
            } else {
                return Result.error("状态更新失败");
            }
        } catch (Exception e) {
            log.error("更新VIP套餐状态失败", e);
            return Result.error("状态更新失败: " + e.getMessage());
        }
    }
}

