using Domain.Dtos;
using Domain.Entities;
using Domain.Generics;
using Microsoft.EntityFrameworkCore;
using Models.Helpers;
using Models.Interfaces;
using Models.Models;

namespace Models.Repositories
{
    public class PhSQuoteHeaderRepository(PhronCareOperationsHubContext context) : IPhSQuoteHeaderRepository
    {
        private readonly PhronCareOperationsHubContext _context = context;
        #region Metodos
        public async Task<PagedResult<EQuoteHeader>> GetAllAsync(int page = 1, int pageSize = 50)
        {
            var query = _context.PhSQuoteHeaders
                .Include(q => q.PhSQuoteDetails)
                .Include(q => q.PhSQuoteRoles)
                .Include(q => q.PhSQuoteAdjustments)
                .Include(q => q.PhSQuoteTaxes)
                .AsNoTracking();

            var pagedEntities = await query.ToPagedResultAsync(page, pageSize);

            return new PagedResult<EQuoteHeader>
            {
                Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>),
                TotalItems = pagedEntities.TotalItems,
                Page = pagedEntities.Page,
                PageSize = pagedEntities.PageSize
            };
        }
        public async Task<EQuoteHeader?> GetByIdAsync(int id)
        {
            var entity = await _context.PhSQuoteHeaders
                .Include(q => q.PhSQuoteDetails)
                .Include(q => q.PhSQuoteRoles)
                .Include(q => q.PhSQuoteAdjustments)
                .Include(q => q.PhSQuoteTaxes)
                .FirstOrDefaultAsync(q => q.Id == id);

            return entity != null ? EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>(entity) : null;
        }
        public async Task<EQuoteHeader?> GetByQuoteNumberAsync(string quoteNumber)
        {
            var entity= await _context.PhSQuoteHeaders
                .FirstOrDefaultAsync(q => q.Quotenumber == quoteNumber);
            return entity != null ? EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>(entity) : null;
        }
        public async Task<IEnumerable<EQuoteHeader>> GetByCustomerIdAsync(int customerId)
        {
            var entities = await _context.PhSQuoteHeaders
                .Where(q => q.CustomerId == customerId)
                .Include(q => q.PhSQuoteDetails)
                .Include(q => q.PhSQuoteRoles)
                .Include(q => q.PhSQuoteAdjustments)
                .Include(q => q.PhSQuoteTaxes)
                .ToListAsync();

            return entities.Select(EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>);
        }
        public async Task UpdateAsync(EQuoteHeader quoteHeader)
        {
            var dbEntity = EntityMapper.MapEntity<EQuoteHeader, PhSQuoteHeader>(quoteHeader);
            _context.PhSQuoteHeaders.Update(dbEntity);
            await _context.SaveChangesAsync();
        }
        public async Task DeleteAsync(int id)
        {
            var entity = await _context.PhSQuoteHeaders.FindAsync(id);
            if (entity != null)
            {
                _context.PhSQuoteHeaders.Remove(entity);
                await _context.SaveChangesAsync();
            }
        }
        #endregion
    }
}