package com.zioinfo.kintex.ai;

import com.zioinfo.kintex.ai.dto.AiConfigDto;
import com.zioinfo.kintex.ai.dto.AiConfigUpdateRequest;
import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.common.audit.Audited;
import com.zioinfo.kintex.system.SystemAccessGuard;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * F100 AI 플랫폼 설정 관리 API(백오피스). 시스템관리자(requireAdmin) 전용.
 * <ul>
 *   <li>GET  /api/admin/ai/config — 현재 효과 설정(키 값 미노출 · claudeKeySet 불리언만)</li>
 *   <li>PUT  /api/admin/ai/config — provider/모델/활성화 갱신(화이트리스트 검증, @Audited)</li>
 * </ul>
 * ★ API 키는 이 API 로 저장/조회하지 않는다(env 전용).
 */
@RestController
@RequestMapping("/api/admin/ai")
public class AiConfigController {

    private final AiConfigService service;
    private final SystemAccessGuard guard;

    public AiConfigController(AiConfigService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    @GetMapping("/config")
    public ApiResponse<AiConfigDto> get(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.getConfig());
    }

    @Audited(action = "AI_CONFIG_UPDATE", targetType = "ai_config")
    @PutMapping("/config")
    public ApiResponse<AiConfigDto> update(@AuthenticationPrincipal KintexPrincipal principal,
                                           @RequestBody AiConfigUpdateRequest req) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.update(req, principal.userId()));
    }
}
