package com.gxwebsoft.auto.controller;

import com.gxwebsoft.auto.dto.QrLoginConfirmRequest;
import com.gxwebsoft.auto.dto.QrLoginGenerateResponse;
import com.gxwebsoft.auto.dto.QrLoginStatusResponse;
import com.gxwebsoft.auto.service.QrLoginService;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 认证模块
 *
 * @author 科技小王子
 * @since 2025-03-06 22:50:25
 */
@Tag(name = "认证模块")
@RestController
@RequestMapping("/api/qr-login")
public class QrLoginController extends BaseController {

    @Autowired
    private QrLoginService qrLoginService;

    /**
     * 生成扫码登录token
     */
    @Operation(summary = "生成扫码登录token")
    @PostMapping("/generate")
    public ApiResult<?> generateQrLoginToken() {
        try {
            QrLoginGenerateResponse response = qrLoginService.generateQrLoginToken();
            return success("生成成功", response);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    /**
     * 检查扫码登录状态
     */
    @Operation(summary = "检查扫码登录状态")
    @GetMapping("/status/{token}")
    public ApiResult<?> checkQrLoginStatus(
            @Parameter(description = "扫码登录token") @PathVariable String token) {
        try {
            QrLoginStatusResponse response = qrLoginService.checkQrLoginStatus(token);
            return success("查询成功", response);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    /**
     * 确认扫码登录
     */
    @Operation(summary = "确认扫码登录")
    @PostMapping("/confirm")
    public ApiResult<?> confirmQrLogin(@Valid @RequestBody QrLoginConfirmRequest request) {
        try {
            QrLoginStatusResponse response = qrLoginService.confirmQrLogin(request);
            return success("确认成功", response);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    /**
     * 扫码操作(可选接口，用于移动端扫码后更新状态)
     */
    @Operation(summary = "扫码操作")
    @PostMapping("/scan/{token}")
    public ApiResult<?> scanQrCode(@Parameter(description = "扫码登录token") @PathVariable String token) {
        try {
            boolean result = qrLoginService.scanQrCode(token);
            return success("操作成功", result);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    /**
     * 微信小程序扫码登录确认(便捷接口)
     */
    @Operation(summary = "微信小程序扫码登录确认")
    @PostMapping("/wechat-confirm")
    public ApiResult<?> wechatMiniProgramConfirm(@Valid @RequestBody QrLoginConfirmRequest request) {
        try {
            // 设置平台为微信小程序
            request.setPlatform("miniprogram");
            QrLoginStatusResponse response = qrLoginService.confirmQrLogin(request);
            return success("微信小程序登录确认成功", response);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

}
