package com.erp.purchase.controller;

import com.erp.common.core.model.ApiResponse;
import com.erp.purchase.dto.PurchaseStatisticsDTO;
import com.erp.purchase.service.PurchaseStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/purchase/statistics")
@RequiredArgsConstructor
public class PurchaseStatisticsController {

    private final PurchaseStatisticsService purchaseStatisticsService;

    @GetMapping("/summary")
    public ApiResponse<PurchaseStatisticsDTO.SummaryVO> getSummary(PurchaseStatisticsDTO.QueryRequest query) {
        try {
            PurchaseStatisticsDTO.SummaryVO summary = purchaseStatisticsService.getSummary(query);
            return ApiResponse.success(summary);
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @GetMapping("/by-supplier")
    public ApiResponse<List<PurchaseStatisticsDTO.SupplierStatVO>> getSupplierStats(PurchaseStatisticsDTO.QueryRequest query) {
        try {
            List<PurchaseStatisticsDTO.SupplierStatVO> stats = purchaseStatisticsService.getSupplierStats(query);
            return ApiResponse.success(stats);
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }

    @GetMapping("/daily")
    public ApiResponse<List<PurchaseStatisticsDTO.DailyStatVO>> getDailyStats(PurchaseStatisticsDTO.QueryRequest query) {
        try {
            List<PurchaseStatisticsDTO.DailyStatVO> stats = purchaseStatisticsService.getDailyStats(query);
            return ApiResponse.success(stats);
        } catch (Exception e) {
            return ApiResponse.error(500, e.getMessage());
        }
    }
}
