package com.example.demo.dashboard;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class DashboardService {
    private final DashboardRepository dashboardRepository;

    public DashboardService(DashboardRepository dashboardRepository) {
        this.dashboardRepository = dashboardRepository;
    }

    public DashboardOverviewResponse getOverview(long shopId) {
        BigDecimal todaySales = dashboardRepository.sumTodaySalesOrders(shopId);
        BigDecimal monthSales = dashboardRepository.sumMonthSalesOrders(shopId);
        BigDecimal monthGrossProfit = dashboardRepository.sumMonthGrossProfitApprox(shopId);
        BigDecimal stockTotalQty = dashboardRepository.sumTotalInventoryQty(shopId);
        return new DashboardOverviewResponse(todaySales, monthSales, monthGrossProfit, stockTotalQty);
    }

    public DashboardOverviewResponse getOverviewByUserOrShop(Long userId, Long shopIdOrNull) {
        Long resolvedShopId = null;
        if (userId != null) {
            resolvedShopId = dashboardRepository.findShopIdByUserId(userId);
        }
        if (resolvedShopId == null && shopIdOrNull != null) {
            resolvedShopId = shopIdOrNull;
        }
        if (resolvedShopId == null) {
            resolvedShopId = 1L;
        }
        return getOverview(resolvedShopId);
    }
}



