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

import com.infoepoch.pms.agent.platform.model.application.ModelConfigService;
import com.infoepoch.pms.agent.platform.model.application.dto.CreateModelConfigRequest;
import com.infoepoch.pms.agent.platform.model.application.dto.ModelConfigResponse;
import com.infoepoch.pms.agent.platform.model.application.dto.UpdateModelConfigRequest;
import com.infoepoch.pms.agent.platform.shared.response.Result;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/providers/{providerId}/models")
@RequiredArgsConstructor
public class ModelConfigController {

    private final ModelConfigService modelConfigService;

    /**
     * 查询指定供应商下的模型配置列表。
     *
     * @param providerId 供应商 ID
     * @return 模型配置列表
     */
    @GetMapping
    public Result<List<ModelConfigResponse>> listByProvider(@PathVariable String providerId) {
        return Result.success(modelConfigService.listByProvider(providerId));
    }

    /**
     * 创建模型配置。
     *
     * @param providerId 供应商 ID
     * @param req        创建请求
     * @return 创建后的模型配置
     */
    @PostMapping
    public Result<ModelConfigResponse> create(@PathVariable String providerId,
                                              @Valid @RequestBody CreateModelConfigRequest req) {
        return Result.success(modelConfigService.create(providerId, req));
    }

    /**
     * 更新模型配置。
     *
     * @param providerId 供应商 ID
     * @param mid        模型配置 ID
     * @param req        更新请求
     * @return 更新后的模型配置
     */
    @PutMapping("/{mid}")
    public Result<ModelConfigResponse> update(@PathVariable String providerId,
                                              @PathVariable String mid,
                                              @Valid @RequestBody UpdateModelConfigRequest req) {
        return Result.success(modelConfigService.update(providerId, mid, req));
    }

    /**
     * 删除模型配置。
     *
     * @param providerId 供应商 ID
     * @param mid        模型配置 ID
     * @return 空结果
     */
    @DeleteMapping("/{mid}")
    public Result<Void> delete(@PathVariable String providerId, @PathVariable String mid) {
        modelConfigService.delete(providerId, mid);
        return Result.success(null);
    }

    /**
     * 切换模型配置启用状态。
     *
     * @param providerId 供应商 ID
     * @param mid        模型配置 ID
     * @return 切换后的模型配置
     */
    @PatchMapping("/{mid}/toggle")
    public Result<ModelConfigResponse> toggle(@PathVariable String providerId, @PathVariable String mid) {
        return Result.success(modelConfigService.toggle(providerId, mid));
    }
}
