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

import cn.hutool.core.collection.CollUtil;
import com.starry.admin.common.aspect.CustomUserLogin;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.blindbox.module.entity.BlindBoxConfigEntity;
import com.starry.admin.modules.blindbox.service.BlindBoxConfigService;
import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity;
import com.starry.admin.modules.weichat.entity.blindbox.BlindBoxConfigView;
import com.starry.admin.modules.weichat.entity.blindbox.BlindBoxPurchaseRequest;
import com.starry.admin.modules.weichat.entity.blindbox.BlindBoxPurchaseResult;
import com.starry.admin.modules.weichat.entity.blindbox.BlindBoxRewardDispatchRequest;
import com.starry.admin.modules.weichat.entity.blindbox.BlindBoxRewardView;
import com.starry.admin.modules.weichat.service.WxBlindBoxOrderService;
import com.starry.common.result.R;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wx/blind-box")
@Validated
public class WxBlindBoxController {

    @Resource
    private BlindBoxConfigService blindBoxConfigService;

    @Resource
    private WxBlindBoxOrderService wxBlindBoxOrderService;

    @CustomUserLogin
    @GetMapping("/config/list")
    public R listConfigs() {
        PlayCustomUserInfoEntity user = ThreadLocalRequestDetail.getCustomUserInfo();
        if (user == null) {
            throw new CustomException("用户未登录");
        }
        List<BlindBoxConfigEntity> configs = blindBoxConfigService.listActiveByTenant(user.getTenantId());
        List<BlindBoxConfigView> views = CollUtil.emptyIfNull(configs).stream()
                .map(this::toConfigView)
                .collect(Collectors.toList());
        return R.ok(views);
    }

    @CustomUserLogin
    @PostMapping("/order/purchase")
    public R purchase(@Valid @RequestBody BlindBoxPurchaseRequest request) {
        BlindBoxPurchaseResult result = wxBlindBoxOrderService.purchase(request);
        return R.ok(result);
    }

    @CustomUserLogin
    @PostMapping("/reward/{id}/dispatch")
    public R dispatch(@PathVariable("id") String rewardId, @Valid @RequestBody BlindBoxRewardDispatchRequest body) {
        BlindBoxRewardView view = wxBlindBoxOrderService.dispatchReward(rewardId, body.getClerkId());
        return R.ok(view);
    }

    @CustomUserLogin
    @GetMapping("/reward/list")
    public R listRewards(@RequestParam(value = "status", required = false) String status) {
        PlayCustomUserInfoEntity user = ThreadLocalRequestDetail.getCustomUserInfo();
        if (user == null) {
            throw new CustomException("用户未登录");
        }
        List<BlindBoxRewardView> rewards = wxBlindBoxOrderService.listRewards(user.getTenantId(), user.getId(), status);
        return R.ok(rewards);
    }

    private BlindBoxConfigView toConfigView(BlindBoxConfigEntity entity) {
        BlindBoxConfigView view = new BlindBoxConfigView();
        view.setId(entity.getId());
        view.setName(entity.getName());
        view.setCoverUrl(entity.getCoverUrl());
        view.setDescription(entity.getDescription());
        view.setPrice(entity.getPrice());
        return view;
    }
}
