package org.dromara.main.controller.miniapp;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.MainAgreementBo;
import org.dromara.main.domain.vo.MainAgreementVo;
import org.dromara.main.domain.vo.MiniappLoginVo;
import org.dromara.main.service.IMainAgreementService;
import org.dromara.main.service.IMainStudentService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/miniapp/auth")
public class miniappAuthController extends BaseController {
    private final IMainAgreementService agreementService;
    private final IMainStudentService studentService;

    @SaIgnore
    @GetMapping("/agreement")
    public R<MainAgreementVo> getAgreement(@RequestParam("type") String type) {
        return R.ok(agreementService.selectByType(type));
    }

    @SaCheckPermission("main:agreement:edit")
    @PostMapping("/edit")
    public R<Void> update(@RequestBody MainAgreementBo bo){
        return toAjax(agreementService.update(bo));
    }

    /**
     * 微信小程序一键登录
     */
    @SaIgnore
    @PostMapping("/login")
    public R<MiniappLoginVo> wechatLogin(@RequestBody Map<String, String> body) {
        String code = body.get("code");
        String phoneCode = body.get("phoneCode");

        if (code == null || code.isEmpty() || phoneCode == null || phoneCode.isEmpty()) {
            return R.fail("登录参数不完整");
        }
        return R.ok(studentService.miniAppLogin(code, phoneCode));
    }
    @PostMapping("/logout")
    public R<Void> logout() {
        StpUtil.logout(); // 如果废弃版使用了自定义的 StudentLoginHelper，请改为相应的登出调用
        return R.ok("退出成功");
    }
}
