using MD.PersianDateTime.Standard;

namespace Netina.Core.BaseServices;

public class DashboardService : IDashboardService
{
    private readonly IRepositoryWrapper _repositoryWrapper;
    private readonly UserManager<ApplicationUser> _userManager;

    public DashboardService(IRepositoryWrapper repositoryWrapper,UserManager<ApplicationUser> userManager)
    {
        _repositoryWrapper = repositoryWrapper;
        _userManager = userManager;
    }
    public async Task<HomeDashboardDto> GetHomeDashboardAsyncTask(CancellationToken cancellationToken = default)
    {
        var response = new HomeDashboardDto
        {
            BlogsCount = await _repositoryWrapper.SetRepository<Blog>()
                .TableNoTracking
                .CountAsync(cancellationToken),
            ProductsCount = await _repositoryWrapper.SetRepository<Product>()
                .TableNoTracking
                .CountAsync(cancellationToken),
            TodayOrdersCount = await _repositoryWrapper.SetRepository<Order>()
                .TableNoTracking
                .Where(o => o.OrderAt.Date == DateTime.Today.Date)
                .CountAsync(cancellationToken),
            UnSubmittedOrdersCount = await _repositoryWrapper.SetRepository<Order>()
                .TableNoTracking
                .Where(o => o.OrderStatus == OrderStatus.Paid || o.OrderStatus == OrderStatus.Submitted)
                .CountAsync(cancellationToken),
            BrandsCount = await _repositoryWrapper.SetRepository<Brand>()
                .TableNoTracking
                .CountAsync(cancellationToken),
            SubscribersCount = await _userManager.Users.CountAsync(cancellationToken)
        };

        return response;
    }

    public async Task<OrderDashboardDto> GetOrdersDashboardAsyncTask(CancellationToken cancellationToken = default)
    {
        DateTime startOfThisMonth = new PersianDateTime(PersianDateTime.Today.Year, PersianDateTime.Today.Month, 1).ToDateTime();
        DateTime endOfThisMonth = startOfThisMonth.AddMonths(1);
        var response = new OrderDashboardDto
        {
            PayedOrdersCount = await _repositoryWrapper.SetRepository<Order>()
                .TableNoTracking
                .Where(o=>o.IsPayed && o.OrderStatus==OrderStatus.Paid)
                .CountAsync(cancellationToken),
            ThisMonthOrdersCount = await _repositoryWrapper.SetRepository<Order>()
                .TableNoTracking
                .Where(s => s.OrderAt.Date >= startOfThisMonth.Date && s.OrderAt.Date < endOfThisMonth.Date)
                .CountAsync(cancellationToken),
            TodayOrdersCount = await _repositoryWrapper.SetRepository<Order>()
                .TableNoTracking
                .Where(o => o.OrderAt.Date == DateTime.Now.Date)
                .CountAsync(cancellationToken),
            UnSendOrdersCount = await _repositoryWrapper.SetRepository<Order>()
                .TableNoTracking
                .Where(o => o.IsPayed && o.OrderStatus == OrderStatus.Processing)
                .CountAsync(cancellationToken)
        };

        return response;
    }
}