using NetinaShop.Domain.Entities.Blogs;
using NetinaShop.Domain.Entities.Brands;

namespace NetinaShop.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();
        response.BlogsCount = await _repositoryWrapper.SetRepository<Blog>()
            .TableNoTracking
            .CountAsync(cancellationToken);

        response.ProductsCount = await _repositoryWrapper.SetRepository<Product>()
            .TableNoTracking
            .CountAsync(cancellationToken);

        response.TodayOrdersCount = await _repositoryWrapper.SetRepository<Order>()
            .TableNoTracking
            .Where(o => o.OrderAt.Date == DateTime.Today.Date)
            .CountAsync(cancellationToken);

        response.UnSubmittedOrdersCount = await _repositoryWrapper.SetRepository<Order>()
            .TableNoTracking
            .Where(o => o.OrderStatus == OrderStatus.Paid || o.OrderStatus == OrderStatus.Submitted)
            .CountAsync(cancellationToken);

        response.BrandsCount = await _repositoryWrapper.SetRepository<Brand>()
            .TableNoTracking
            .CountAsync(cancellationToken);

        response.SubscribersCount = await _userManager.Users.CountAsync(cancellationToken);

        return response;
    }
}