package com.test.bijihoudaun.controller;

import com.test.bijihoudaun.common.response.R;
import com.test.bijihoudaun.util.CaptchaUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 验证码接口
 */
@Tag(name = "验证码接口")
@RestController
@RequestMapping("/api/captcha")
public class CaptchaController {

    @Operation(summary = "获取图形验证码")
    @GetMapping("/generate")
    public R<Map<String, String>> generateCaptcha() {
        try {
            CaptchaUtil.CaptchaResult result = CaptchaUtil.generateCaptcha();

            Map<String, String> data = new HashMap<>();
            data.put("captchaId", result.getCaptchaId());
            data.put("captchaImage", result.getBase64Image());

            return R.success(data);
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }
}
