using DbConnectionLibrairie;
using EntityManagers;
using ExtensionsClassLibrairie;
using ManagerInterfaces;
using Model;
using OrderCriterias;

namespace DataManagers
{
    public class AdministratorDataManager(MyDbContext dbContext) : IAdministratorManager<Administrator>
    {

        private AdministratorEntityManager manager = new AdministratorEntityManager(dbContext);

        public async Task<Administrator> addAdmin(Administrator admin)
        {
            return await Task.FromResult<Administrator>((await manager.addAdmin(admin.ToEntity())).ToModel());
        }

        public async Task<Administrator?> getAdministrator(int id)
        {
            return (await manager.getAdministrator(id))?.ToModel();
        }

        public async Task<Administrator?> getAdministratorByUsername(string username)
        {
            return await Task.FromResult<Administrator?>((await manager.getAdministratorByUsername(username))?.ToModel());
        }

        public async Task<(int nbPages, IEnumerable<Administrator>? administrators)> getAdministrators(int nb, int count, AdministratorOrderCriteria orderCriteria = AdministratorOrderCriteria.ById)
        {
            List<Administrator>? tmp = new List<Administrator>();
            var res = await manager.getAdministrators(nb, count, orderCriteria);
            if (res.administrators == null) tmp = null;
            else
            {
                foreach (var item in res.administrators)
                {
                    tmp.Add(item.ToModel());
                }
            }
            return await Task.FromResult<(int nbPages, IEnumerable<Administrator>? administrators)>((res.nbPages, tmp));
        }

        public int getNbElements()
        {
            return manager.getNbElements();
        }

        public async Task<Administrator?> removeAdmin(Administrator admin)
        {
            return (await manager.removeAdmin(admin.ToEntity()))?.ToModel();
        }

        public async Task<Administrator?> removeAdmin(int id)
        {
            return (await manager.removeAdmin(id))?.ToModel();
        }

        public async Task<bool> setPassword(string username, string newHashedPassword)
        {
            return await manager.setPassword(username, newHashedPassword);
        }

        public async Task<Administrator?> updateAdministrator(int id, Administrator admin)
        {
            return (await manager.updateAdministrator(id, admin.ToEntity()))?.ToModel();
        }
    }
}
