package org.dromara.zhishu.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.zhishu.domain.TInviteCode;
import org.dromara.zhishu.domain.bo.InviteCodeBo;
import org.dromara.zhishu.domain.vo.InviteCodeVo;
import org.dromara.zhishu.service.ITInviteCodeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 邀请码管理
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/invite")
public class TInviteCodeController extends BaseController {

    private final ITInviteCodeService inviteCodeService;

    /**
     * 查询我的邀请码列表
     */
    @GetMapping("/list")
    @SaIgnore
    public R<List<InviteCodeVo>> list() {
        Long userId = LoginHelper.getUserId();
        List<InviteCodeVo> list = inviteCodeService.queryListByUserId(userId);
        // 不需要显示邀请码是否已使用
        if (list != null) {
            for (InviteCodeVo vo : list) {
                vo.setUsed(null);
            }
        }
        return R.ok(list);
    }

    /**
     * 查询邀请码分页列表
     */
    @SaCheckPermission("zhishu:invite:list")
    @GetMapping("/page")
    @SaIgnore
    public TableDataInfo<InviteCodeVo> page(InviteCodeBo bo, PageQuery pageQuery) {
        // 设置当前登录用户ID作为查询条件
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        
        TableDataInfo<InviteCodeVo> tableDataInfo = inviteCodeService.queryPageList(bo, pageQuery);
        // 不需要显示邀请码是否已使用
        if (tableDataInfo != null && tableDataInfo.getRows() != null) {
            for (InviteCodeVo vo : tableDataInfo.getRows()) {
                vo.setUsed(null);
            }
        }
        return tableDataInfo;
    }

    /**
     * 获取邀请码详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:invite:query")
    @GetMapping("/{id}")
    public R<InviteCodeVo> getInfo(@PathVariable("id") Long id) {
        InviteCodeVo vo = inviteCodeService.queryById(id);
        if (vo != null) {
            vo.setUsed(null);
        }
        return R.ok(vo);
    }

    /**
     * 生成邀请码
     */
    @Log(title = "邀请码管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/generate")
    public R<Integer> generate(@RequestBody InviteCodeBo bo) {
        // 设置当前登录用户为邀请人
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        
        return R.ok(inviteCodeService.generateInviteCode(bo));
    }

    /**
     * 验证邀请码
     */
    @SaIgnore
    @GetMapping("/validate")
    public R<Boolean> validate(@RequestParam("code") String code) {
        TInviteCode inviteCode = inviteCodeService.validateInviteCode(code);
        return R.ok(inviteCode != null);
    }

    /**
     * 使用邀请码（注册时调用）
     */
    @SaIgnore
    @PostMapping("/use")
    public R<Boolean> useInviteCode(@RequestParam("code") String code, @RequestParam("userId") Long userId) {
        return R.ok(inviteCodeService.useInviteCode(code, userId));
    }
} 