package com.starry.admin.modules.blindbox.controller;

import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.blindbox.module.dto.BlindBoxPoolImportRow;
import com.starry.admin.modules.blindbox.module.dto.BlindBoxPoolUpsertRequest;
import com.starry.admin.modules.blindbox.service.BlindBoxPoolAdminService;
import com.starry.admin.utils.ExcelUtils;
import com.starry.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "盲盒奖池管理")
@RestController
@RequestMapping("/play/blind-box/pool")
public class BlindBoxPoolController {

    @Resource
    private BlindBoxPoolAdminService blindBoxPoolAdminService;

    @ApiOperation("查询盲盒奖池列表")
    @GetMapping
    public R list(@RequestParam String blindBoxId) {
        return R.ok(blindBoxPoolAdminService.list(blindBoxId));
    }

    @ApiOperation("导入盲盒奖池配置")
    @PostMapping("/{blindBoxId}/import")
    public R importPool(@PathVariable String blindBoxId, @RequestParam("file") MultipartFile file)
            throws IOException {
        if (file == null || file.isEmpty()) {
            throw new CustomException("上传文件不能为空");
        }
        List<?> rows = ExcelUtils.importEasyExcel(file.getInputStream(), BlindBoxPoolImportRow.class);
        @SuppressWarnings("unchecked")
        List<BlindBoxPoolImportRow> importRows = (List<BlindBoxPoolImportRow>) rows;
        blindBoxPoolAdminService.replacePool(blindBoxId, importRows);
        return R.ok();
    }

    @ApiOperation("删除盲盒奖池项")
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id) {
        blindBoxPoolAdminService.removeById(id);
        return R.ok();
    }

    @ApiOperation("新增盲盒奖池项")
    @PostMapping
    public R create(@RequestBody BlindBoxPoolUpsertRequest body) {
        return R.ok(blindBoxPoolAdminService.create(body != null ? body.getBlindBoxId() : null, body));
    }

    @ApiOperation("更新盲盒奖池项")
    @PutMapping("/{id}")
    public R update(@PathVariable Long id, @RequestBody BlindBoxPoolUpsertRequest body) {
        return R.ok(blindBoxPoolAdminService.update(id, body));
    }

    @ApiOperation("查询可用中奖礼物")
    @GetMapping("/gifts")
    public R giftOptions(@RequestParam(required = false) String keyword) {
        return R.ok(blindBoxPoolAdminService.listGiftOptions(keyword));
    }
}
