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

namespace Models.Repositories
{
    public class PhSQuoteRoleRepository(PhronCareOperationsHubContext context) : IPhSQuoteRoleRepository
    {
        #region Declaraciones
        private readonly PhronCareOperationsHubContext _context = context;
        #endregion
        #region Métodos
        public async Task<PagedResult<EQuoteRole>> GetAllAsync(int page = 1, int pageSize = 50)
        {
            var query = _context.PhSQuoteRoles
                .AsQueryable();

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

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

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