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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.blindbox.module.constant.BlindBoxConfigStatus;
import com.starry.admin.modules.blindbox.module.entity.BlindBoxConfigEntity;
import com.starry.admin.modules.blindbox.service.BlindBoxConfigService;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.result.R;
import com.starry.common.utils.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
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;

@Api(tags = "盲盒配置管理")
@RestController
@RequestMapping("/play/blind-box/config")
public class BlindBoxConfigController {

    @Resource
    private BlindBoxConfigService blindBoxConfigService;

    @ApiOperation("查询盲盒列表")
    @GetMapping
    public R list(@RequestParam(required = false) Integer status) {
        String tenantId = SecurityUtils.getTenantId();
        LambdaQueryWrapper<BlindBoxConfigEntity> query = Wrappers.lambdaQuery(BlindBoxConfigEntity.class)
                .eq(BlindBoxConfigEntity::getTenantId, tenantId)
                .eq(BlindBoxConfigEntity::getDeleted, Boolean.FALSE)
                .orderByDesc(BlindBoxConfigEntity::getCreatedTime);
        if (status != null) {
            query.eq(BlindBoxConfigEntity::getStatus, status);
        }
        List<BlindBoxConfigEntity> configs = blindBoxConfigService.list(query);
        return R.ok(configs);
    }

    @ApiOperation("盲盒详情")
    @GetMapping("/{id}")
    public R detail(@PathVariable String id) {
        BlindBoxConfigEntity entity = blindBoxConfigService.requireById(id);
        return R.ok(entity);
    }

    @ApiOperation("新增盲盒")
    @PostMapping
    public R create(@RequestBody BlindBoxConfigEntity body) {
        if (StrUtil.isBlank(body.getName())) {
            throw new CustomException("盲盒名称不能为空");
        }
        validatePrice(body.getPrice());
        body.setId(IdUtils.getUuid());
        body.setTenantId(SecurityUtils.getTenantId());
        body.setDeleted(Boolean.FALSE);
        if (body.getStatus() == null) {
            body.setStatus(BlindBoxConfigStatus.ENABLED.getCode());
        }
        blindBoxConfigService.save(body);
        return R.ok(body.getId());
    }

    @ApiOperation("更新盲盒")
    @PutMapping("/{id}")
    public R update(@PathVariable String id, @RequestBody BlindBoxConfigEntity body) {
        validatePrice(body.getPrice());
        BlindBoxConfigEntity existing = blindBoxConfigService.requireById(id);
        if (!SecurityUtils.getTenantId().equals(existing.getTenantId())) {
            throw new CustomException("无权操作该盲盒");
        }
        existing.setName(body.getName());
        existing.setCoverUrl(body.getCoverUrl());
        existing.setDescription(body.getDescription());
        existing.setPrice(body.getPrice());
        existing.setStatus(body.getStatus());
        blindBoxConfigService.updateById(existing);
        return R.ok();
    }

    @ApiOperation("删除盲盒")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable String id) {
        BlindBoxConfigEntity existing = blindBoxConfigService.requireById(id);
        if (!SecurityUtils.getTenantId().equals(existing.getTenantId())) {
            throw new CustomException("无权操作该盲盒");
        }
        blindBoxConfigService.removeById(id);
        return R.ok();
    }

    private void validatePrice(BigDecimal price) {
        if (price == null || price.compareTo(BigDecimal.ZERO) <= 0) {
            throw new CustomException("盲盒价格必须大于0");
        }
    }
}
