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

namespace Models.Repositories
{
    public class PhSQuoteDetailRepository(PhronCareOperationsHubContext context)
    {
        #region Declaraciones
        private readonly PhronCareOperationsHubContext _context = context;
        #endregion

        #region Métodos
        public async Task<PagedResult<EQuoteDetail>> GetAllAsync(int page = 1, int pageSize = 50)
        {
            var query = _context.PhSQuoteDetails
                .AsQueryable();

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

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

            return entity != null ? EntityMapper.MapEntity<PhSQuoteDetail, EQuoteDetail>(entity) : null;
        }
        public async Task<EQuoteDetail> AddAsync(EQuoteDetail quoteDetail)
        {
            var dbEntity = EntityMapper.MapEntity<EQuoteDetail, PhSQuoteDetail>(quoteDetail);
            _context.PhSQuoteDetails.Add(dbEntity);
            await _context.SaveChangesAsync();
            return EntityMapper.MapEntity<PhSQuoteDetail, EQuoteDetail>(dbEntity);
        }
        public async Task UpdateAsync(EQuoteDetail quoteDetail)
        {
            var dbEntity = EntityMapper.MapEntity<EQuoteDetail, PhSQuoteDetail>(quoteDetail);
            _context.PhSQuoteDetails.Update(dbEntity);
            await _context.SaveChangesAsync();
        }
        public async Task DeleteAsync(int id)
        {
            var entity = await _context.PhSQuoteDetails.FindAsync(id);
            if (entity != null)
            {
                _context.PhSQuoteDetails.Remove(entity);
                await _context.SaveChangesAsync();
            }
        }
        #endregion
    }
}
