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

namespace Models.Repositories
{
    public class PhSPeopleRepository(PhronCareOperationsHubContext context) : IPhSPeopleRepository
    {
        #region Declaraciones
        private readonly PhronCareOperationsHubContext _context = context;
        #endregion
        #region Métodos
        public async Task<PagedResult<EPerson>> GetAllAsync(int page = 1, int pageSize = 50)
        {
            var query = _context.PhSPeople
                .Include(p => p.Businessunits)
                .Include(p => p.Peoplegroups)
                .AsQueryable();
            var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
            return new PagedResult<EPerson>
            {
                Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhSPerson, EPerson>),
                TotalItems = pagedEntities.TotalItems,
                Page = pagedEntities.Page,
                PageSize = pagedEntities.PageSize
            };
        }

        public async Task<PagedResult<EPerson>> SearchAsync(string? name, string? email,
            int page = 1, int pageSize = 50)
        {
            var query = _context.PhSPeople.AsQueryable();

            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(p => (p.Name).Contains(name));
            }

            if (!string.IsNullOrEmpty(email))
            {
                query = query.Where(p => p.Email != null && p.Email.Contains(email));
            }

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

            return new PagedResult<EPerson>
            {
                Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhSPerson, EPerson>),
                TotalItems = pagedEntities.TotalItems,
                Page = pagedEntities.Page,
                PageSize = pagedEntities.PageSize
            };
        }
        public async Task<EPerson?> GetByIdAsync(int id)
        {
            var entity = await _context.PhSPeople
                .Include(p => p.Businessunits)
                .Include(p => p.Peoplegroups)
                .AsNoTracking()
                .FirstOrDefaultAsync(p => p.Id == id);

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