package com.erp.ai.controller;

import com.erp.ai.service.AiAgentConversationService;
import com.erp.dto.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/ai-agents/conversations")
@RequiredArgsConstructor
public class AiAgentConversationController {

    private final AiAgentConversationService conversationService;

    @GetMapping(value = {"", "/list"})
    public ResponseEntity<Map<String, Object>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int limit,
            @RequestParam(value = "agent_id", required = false) Long agentId) {
        Map<String, Object> data = conversationService.list(page, limit, agentId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", data.get("conversations"));
        response.put("total", data.get("total"));
        return ResponseEntity.ok(response);
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getById(@PathVariable long id) {
        Map<String, Object> data = conversationService.getById(id);
        if (data.get("conversation") == null) {
            return ResponseEntity.status(404).body(ApiResponse.error("대화를 찾을 수 없습니다."));
        }
        return ResponseEntity.ok(ApiResponse.success(data));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> delete(@PathVariable long id) {
        conversationService.delete(id);
        return ResponseEntity.ok(ApiResponse.success(null, "대화가 삭제되었습니다."));
    }
}
