package com.erp.ai.controller;

import com.erp.ai.dto.UsageSummaryResponse;
import com.erp.ai.service.AiAgentUsageService;
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.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ai-agents/usage")
@RequiredArgsConstructor
public class AiAgentUsageController {

    private final AiAgentUsageService usageService;

    @GetMapping("/summary")
    public ResponseEntity<ApiResponse<UsageSummaryResponse>> summary() {
        return ResponseEntity.ok(ApiResponse.success(usageService.getSummary()));
    }

    @GetMapping("/logs")
    public ResponseEntity<Map<String, Object>> logs(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int limit) {
        Map<String, Object> result = usageService.getLogs(page, limit);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", result.get("logs"));
        response.put("total", result.get("total"));
        return ResponseEntity.ok(response);
    }

    @GetMapping("/daily")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> daily(
            @RequestParam(defaultValue = "30") int days) {
        return ResponseEntity.ok(ApiResponse.success(usageService.getDailyUsage(days)));
    }
}
