package com.seer.teach.mp.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.mp.admin.controller.req.MpActivityQueryReq;
import com.seer.teach.mp.admin.controller.req.MpActivityReq;
import com.seer.teach.mp.admin.controller.resp.AdminActivityResp;
import com.seer.teach.mp.admin.service.IAdminActivityService;
import com.seer.teach.mp.entity.MpAgentEntity;
import com.seer.teach.mp.service.IMpAgentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@LogPrint
@RequiredArgsConstructor
@RestController
@Tag(name = "管理端 - 活动")
@RequestMapping("/mp/agent/activity")
public class AdminActivityController {

    private final IAdminActivityService agentActivityService;

    private final IMpAgentService mpAgentService;

    @Operation(summary = "活动列表")
    @PostMapping("/page-list")
    @SaCheckPermission("mp:admin:agent:activity:list")
    public ResultBean<PageListBean<AdminActivityResp>> pageList(@RequestBody @Validated MpActivityQueryReq query) {
        return ResultBean.success(agentActivityService.pageList(query));
    }

    @Operation(summary = "创建或更新活动")
    @PostMapping("/save")
    @SaCheckPermission("mp:admin:agent:activity:save")
    public ResultBean<Boolean> save(@Valid @RequestBody MpActivityReq request) {
        return ResultBean.success(agentActivityService.saveOrUpdateActivity(request));
    }

    @Operation(summary = "删除活动")
    @DeleteMapping("/{id}")
    @SaCheckPermission("mp:admin:agent:activity:delete")
    public ResultBean<Boolean> delete(@PathVariable Integer id) {
        return ResultBean.success(agentActivityService.deleteActivity(id));
    }

    @Operation(summary = "详情")
    @GetMapping("/{id}")
    @SaCheckPermission("mp:admin:agent:activity:get")
    public ResultBean<AdminActivityResp> get(@PathVariable Integer id) {
        AdminActivityResp result = agentActivityService.getById(id);
        return ResultBean.success(result);
    }

    @Operation(summary = "获取活动二维码")
    @GetMapping("/{activityId}/qrcode")
    @SaCheckPermission("mp:admin:agent:activity:qrcode")
    public ResultBean<String> getQrCode(@PathVariable("activityId") Integer activityId, @RequestParam("appId") String appId) {
        Integer userId = StpUtil.getLoginIdAsInt();
        MpAgentEntity one = mpAgentService.lambdaQuery().eq(MpAgentEntity::getContactUserId, userId).one();
        Integer agentId = one.getId();
        return ResultBean.success(agentActivityService.getQrCode(agentId, activityId, appId));
    }
}