using Back.Common;
using Back.Data.Contracts;
using Back.Data.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Extensions;
using Shared.DTOs;
using Shared.DTOs.Serch;
using Shared.Enums;

namespace Back.Services
{
    public class ServWalt
    {
        private readonly IAsyncRepository<CreditDocuments> _repoCreditDocuments;
    
        public ServWalt(IAsyncRepository<CreditDocuments> repoCreditDocuments)
        {
            _repoCreditDocuments = repoCreditDocuments;
        }
        public async Task<bool> AddDocument(CreditDocuments document)
        {
            return await _repoCreditDocuments.AddBoolResultAsync(document);
        }
        public async Task<PagingDto<CreditDocumentDto>> GetDocuments(GridDataProviderRequestDto value,int CompanyID)
        {
            var request = _repoCreditDocuments.GetAll();
            foreach (var item in value.Filters)
            {
                switch (item.PropertyName)
                {
                    case "Date":
                        request = request.Where(w => w.Date.Contains(item.Value.Replace("/","")) || w.Date == item.Value);
                        break;
                    case "Type":
                        if (item.Value=="0" || item.Value == "1")
                        request = request.Where(w => w.type == (CreditDocumentType)Enum.Parse(typeof(CreditDocumentType), item.Value));
                        break;
                    default:
                        break;
                }
            }

            return await _repoCreditDocuments.Get(document=>document.CompanyID==CompanyID).OrderByDescending(o=>o.ID)
                .Select(s=>new CreditDocumentDto
                {
                    Date=s.Date.ShamciToFormatShamci(),
                    Title=s.Title,
                    typeName = s.type.GetEnumDisplayName(),
                    type=s.type,
                    Value=s.Value,
                })
                .Paging(value.PageNumber, value.PageSize);
        }
    }
}
