
using Back.Common;
using Back.Data.Contracts;
using Back.Data.Models.Warehouse;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Extensions;
using Shared.DTOs;
using Shared.DTOs.Warehouse;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Back.Services.Warehouse
{
    public class WarehouseService
    {
        private readonly IAsyncRepository<Remittance> _RemittanceRepo;
        private readonly IAsyncRepository<Receipt> _ReceiptRepo;

        public WarehouseService(IAsyncRepository<Remittance> remittanceRepo, IAsyncRepository<Receipt> receiptRepo)
        {
            _RemittanceRepo = remittanceRepo;
            _ReceiptRepo = receiptRepo;
        }
        public async Task<PagingDto<CirculationDto>> Circulation(int CompanyID, string date = "", int CODID = 0, int PageIndex = 1, int PageSize = 5)
        {
            var RequestRemittance = _RemittanceRepo.Get(w => w.cODItem.CompanyID == CompanyID && !w.Deleted)
                .Select(s => new CirculationDto
                {
                    ID= s.ID,
                    CODID = s.CODID,
                    CODTitle = s.cODItem.Title,
                    Date = s.Date.ShamciToFormatShamci(),
                    Count = s.Count,
                    info = s.info,
                    Type = TypeCirculation.Remittance,
                    ModelTypeID = (int)s.Type,
                    ModelTypeTitle = s.Type.GetEnumDisplayName(),
                    invoiceID = s.InvoiceID,
                    CreateDt = s.CreateDt,
                });
            if (!string.IsNullOrEmpty(date))
                RequestRemittance = RequestRemittance.Where(w => w.Date == date);
            if (CODID != 0)
                RequestRemittance = RequestRemittance.Where(w => w.CODID == CODID);
            //-----------
            var RequestReceipt = _ReceiptRepo.Get(w => w.cODItem.CompanyID == CompanyID && !w.Deleted)
                  .Select(s => new CirculationDto
                  {
                      ID = s.ID,
                      CODID = s.CODID,
                      CODTitle = s.cODItem.Title,
                      Date = s.Date.ShamciToFormatShamci(),
                      Count = s.Count,
                      info = s.info,
                      Type = TypeCirculation.Receipt,
                      ModelTypeID = (int)s.Type,
                      ModelTypeTitle = s.Type.GetEnumDisplayName(),
                      invoiceID = s.InvoiceID,
                      ForSale = s.ForSale,
                      CreateDt = s.CreateDt,
                  });
            if (!string.IsNullOrEmpty(date))
                RequestReceipt = RequestReceipt.Where(w => w.Date == date);
            if (CODID != 0)
                RequestReceipt = RequestReceipt.Where(w => w.CODID == CODID);

            var itemsReceipt = await RequestReceipt.ToListAsync();
            var Remittance = await RequestRemittance.ToListAsync();

 
                return await itemsReceipt.Union(Remittance).OrderByDescending(o => o.CreateDt).Paging(PageIndex, PageSize);


           // return await RequestReceipt.Union(RequestRemittance).OrderByDescending(o => o.Date).Paging(PageIndex, PageSize);
            //var list = await RequestReceipt.ToListAsync();
            //list.AddRange(await RequestRemittance.ToListAsync());
            //return await list.OrderByDescending(o=>o.Date).AsQueryable().Paging(PageIndex, PageSize);
        }
        public async Task<decimal> Inventory(int CompanyID, int CODID)
        {
            var CReceipt = await _ReceiptRepo.Get(w => w.cODItem.CompanyID == CompanyID && w.CODID == CODID && w.ForSale && !w.Deleted).SumAsync(s => s.Count);
            var CRemittance = await _RemittanceRepo.Get(w => w.cODItem.CompanyID == CompanyID && w.CODID == CODID && !w.Deleted).SumAsync(s => s.Count);
            return CReceipt - CRemittance;
        }
    }
}
