package com.infoepoch.pms.agent.platform.model.adapter.in.web;

import com.infoepoch.pms.agent.platform.model.application.SystemSettingsService;
import com.infoepoch.pms.agent.platform.model.application.dto.DefaultModelResponse;
import com.infoepoch.pms.agent.platform.model.application.dto.DefaultModelSettingsResponse;
import com.infoepoch.pms.agent.platform.model.application.dto.UpdateDefaultModelSettingsRequest;
import com.infoepoch.pms.agent.platform.shared.exception.BusinessException;
import com.infoepoch.pms.agent.platform.shared.response.Result;
import com.infoepoch.pms.agent.platform.shared.response.ResultCode;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/admin/settings")
@RequiredArgsConstructor
public class SystemSettingsController {

    private final SystemSettingsService settingsService;

    /**
     * 获取全局默认模型。
     *
     * @return 默认模型信息
     */
    @GetMapping("/default-model")
    public Result<DefaultModelResponse> getDefaultModel() {
        return Result.success(settingsService.getDefaultModel());
    }

    /**
     * 设置全局默认模型。
     *
     * @param body 请求体，包含 modelConfigId
     * @return 更新后的默认模型信息
     */
    @PutMapping("/default-model")
    public Result<DefaultModelResponse> setDefaultModel(@RequestBody Map<String, String> body) {
        String modelConfigId = body == null ? null : body.get("modelConfigId");
        if (!StringUtils.hasText(modelConfigId)) {
            return Result.success(new DefaultModelResponse(null, null, null, null));
        }
        return Result.success(settingsService.setDefaultModel(modelConfigId));
    }

    /**
     * 获取统一的默认模型设置。
     *
     * @return 默认对话、向量和重排模型设置
     */
    @GetMapping("/default-models")
    public Result<DefaultModelSettingsResponse> getDefaultModelSettings() {
        return Result.success(settingsService.getDefaultModelSettings());
    }

    /**
     * 更新统一的默认模型设置。
     *
     * @param request 更新请求
     * @param bindingResult 参数校验结果
     * @return 更新后的默认模型设置
     */
    @PutMapping("/default-models")
    public ResponseEntity<Result<DefaultModelSettingsResponse>> updateDefaultModelSettings(
            @Valid @RequestBody UpdateDefaultModelSettingsRequest request,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String message = bindingResult.getFieldErrors().stream()
                    .findFirst()
                    .map(error -> error.getDefaultMessage() == null ? error.getField() + " 参数非法" : error.getDefaultMessage())
                    .orElse("请求参数非法");
            return ResponseEntity.badRequest().body(Result.fail(ResultCode.PARAM_ERROR, message));
        }
        return ResponseEntity.ok(Result.success(settingsService.updateDefaultModelSettings(request)));
    }
}
