package com.gxwebsoft.ai.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.ai.dto.AuditEvidenceRequest;
import com.gxwebsoft.ai.service.AuditEvidenceService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 审计取证单控制器
 */
@Slf4j
@Tag(name = "审计取证单")
@RestController
@RequestMapping("/api/ai/auditEvidence")
public class AuditEvidenceController extends BaseController {

    @Autowired
    private AuditEvidenceService auditEvidenceService;

    /**
     * 生成审计取证单
     */
    @Operation(summary = "生成审计取证单")
    @PostMapping("/generate")
    public ApiResult<?> generateAuditEvidence(@RequestBody AuditEvidenceRequest request) {
        try {
        	final User loginUser = getLoginUser();
        	request.setUserName(loginUser.getUsername());
        	
            log.info("接收到审计取证单生成请求 - 用户: {}, 项目: {}", request.getUserName(), request.getProjectName());
            JSONObject result = auditEvidenceService.generateAuditEvidence(request);
            if (Boolean.TRUE.equals(result.getBoolean("success"))) {
                return success(result);
            } else {
                return fail(result.getString("error") != null ? result.getString("error") : "生成审计取证单失败");
            }
        } catch (Exception e) {
            log.error("生成审计取证单异常", e);
            return fail("生成审计取证单异常: " + e.getMessage());
        }
    }
}