package com.erp.ai.controller;

import com.erp.ai.dto.ProviderRequest;
import com.erp.ai.model.AiLlmProvider;
import com.erp.ai.service.AiAgentProviderService;
import com.erp.dto.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ai-agents/providers")
@RequiredArgsConstructor
@Slf4j
public class AiAgentProviderController {

    private final AiAgentProviderService providerService;

    @GetMapping(value = {"", "/list"})
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> list() {
        return ResponseEntity.ok(ApiResponse.success(providerService.list()));
    }

    @PostMapping
    public ResponseEntity<ApiResponse<AiLlmProvider>> create(@RequestBody ProviderRequest req) {
        return ResponseEntity.status(201).body(
                ApiResponse.success(providerService.create(req), "프로바이더가 추가되었습니다."));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<AiLlmProvider>> update(
            @PathVariable long id,
            @RequestBody ProviderRequest req) {
        AiLlmProvider updated = providerService.update(id, req);
        if (updated == null) {
            return ResponseEntity.status(404).body(ApiResponse.error("프로바이더를 찾을 수 없습니다."));
        }
        return ResponseEntity.ok(ApiResponse.success(updated, "프로바이더가 수정되었습니다."));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> delete(@PathVariable long id) {
        providerService.delete(id);
        return ResponseEntity.ok(ApiResponse.success(null, "프로바이더가 삭제되었습니다."));
    }
}
