package com.kexue.skills.controller;

import com.github.pagehelper.PageInfo;
import com.kexue.skills.common.CommonResult;
import com.kexue.skills.entity.ExchangeCode;
import com.kexue.skills.entity.dto.ExchangeCodeQueryDto;
import com.kexue.skills.entity.dto.ExchangeRequestDto;
import com.kexue.skills.service.ExchangeCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * (ExchangeCode)控制器
 */
@RestController
@RequestMapping("/api/exchangeCode")
@Tag(name = "兑换码管理", description = "兑换码CRUD、导入导出、兑换功能")
public class ExchangeCodeController {

    @Resource
    private ExchangeCodeService exchangeCodeService;

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public CommonResult<PageInfo<ExchangeCode>> getPageList(ExchangeCodeQueryDto queryDto) {
        return CommonResult.success(exchangeCodeService.getPageList(queryDto));
    }

    @GetMapping("/list")
    @Operation(summary = "查询列表")
    public CommonResult<List<ExchangeCode>> getList(ExchangeCodeQueryDto queryDto) {
        return CommonResult.success(exchangeCodeService.getList(queryDto));
    }

    @GetMapping("/{id}")
    @Operation(summary = "通过ID查询")
    public CommonResult<ExchangeCode> queryById(@Parameter(description = "主键ID") @PathVariable Long id) {
        return CommonResult.success(exchangeCodeService.queryById(id));
    }

    @GetMapping("/code/{code}")
    @Operation(summary = "通过兑换码查询")
    public CommonResult<ExchangeCode> queryByCode(@Parameter(description = "兑换码") @PathVariable String code) {
        return CommonResult.success(exchangeCodeService.queryByCode(code));
    }

    @PostMapping
    @Operation(summary = "新增兑换码")
    public CommonResult<ExchangeCode> insert(@RequestBody ExchangeCode exchangeCode) {
        return CommonResult.success(exchangeCodeService.insert(exchangeCode));
    }

    @PutMapping
    @Operation(summary = "更新兑换码")
    public CommonResult<ExchangeCode> update(@RequestBody ExchangeCode exchangeCode) {
        return CommonResult.success(exchangeCodeService.update(exchangeCode));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "物理删除")
    public CommonResult<Integer> deleteById(@Parameter(description = "主键ID") @PathVariable Long id) {
        return CommonResult.success(exchangeCodeService.deleteById(id));
    }

    @PutMapping("/{id}/logicDelete")
    @Operation(summary = "逻辑删除")
    public CommonResult<Integer> logicDeleteById(
            @Parameter(description = "主键ID") @PathVariable Long id,
            @Parameter(description = "更新人") @RequestParam String updateBy) {
        return CommonResult.success(exchangeCodeService.logicDeleteById(id, updateBy));
    }

    @PostMapping("/exchange")
    @Operation(summary = "兑换功能")
    public CommonResult<Map<String, Object>> exchange(@RequestBody ExchangeRequestDto requestDto) {
        Map<String, Object> result = exchangeCodeService.exchange(requestDto);
        boolean success = (Boolean) result.get("success");
        if (success) {
            return CommonResult.success(result);
        } else {
            return CommonResult.failed((String) result.get("message"));
        }
    }

    @PostMapping("/importExcel")
    @Operation(summary = "导入Excel")
    public CommonResult<Map<String, Object>> importExcel(@RequestParam("file") MultipartFile file) {
        return CommonResult.success(exchangeCodeService.importExcel(file));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出Excel")
    public void exportExcel(ExchangeCodeQueryDto queryDto, HttpServletResponse response) throws Exception {
        exchangeCodeService.exportExcel(queryDto, response);
    }

    @GetMapping("/statistics")
    @Operation(summary = "统计各状态数量")
    public CommonResult<Map<String, Integer>> getStatistics() {
        return CommonResult.success(exchangeCodeService.getStatistics());
    }

    @GetMapping("/countByPrice")
    @Operation(summary = "根据套餐价格查询可用兑换码数量")
    public CommonResult<Integer> countAvailableByPrice(@Parameter(description = "套餐价格") @RequestParam BigDecimal packagePrice) {
        return CommonResult.success(exchangeCodeService.countAvailableByPrice(packagePrice));
    }
}