package com.zioinfo.cms.ai.learning;

import com.zioinfo.cms.admin.AuditService;
import com.zioinfo.cms.common.ApiResponse;
import lombok.RequiredArgsConstructor;
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;

import java.util.Map;

/**
 * AI 피드백 수집 API. [GUARDiA-CMS]
 *
 * <p>{@code POST /api/ai/feedback} — 인증 사용자(SecurityConfig anyRequest().authenticated())가
 * AI 결과에 대해 👍/👎 + 교정 의견을 제출한다. 로컬 DuckDB 기록 + 중앙 guardia-rag 전달(둘 다 best-effort).
 *
 * <p>요청 본문(JSON): {@code {feature, question, answer, verdict(up|down), correction?}}.
 * 사용자 식별자는 서버 인증 주체(JWT subject)에서 취득하며, 저장 시 마스킹된다(응답에 미노출).
 */
@RestController
@RequestMapping("/api/ai/feedback")
@RequiredArgsConstructor
public class FeedbackController {

    private final LearningFeedbackService service;

    @PostMapping
    public ApiResponse<Map<String, Object>> submit(@RequestBody Map<String, String> req) {
        Map<String, Object> out = service.submit(
                req.get("feature"),
                req.get("question"),
                req.get("answer"),
                req.getOrDefault("verdict", "unknown"),
                req.get("correction"),
                AuditService.currentActor());
        return ApiResponse.ok(out);
    }
}
