using DbConnectionLibrairie;
using DataManagers;
using ExtensionsClassLibrairie;
using ManagerInterfaces;
using Model;
using OrderCriterias;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTOs;

namespace ServiceManagers
{
    public class AdministratorServiceManager(MyDbContext dbContext) : IAdministratorManager<AdministratorDto>
    {

        private AdministratorDataManager manager = new AdministratorDataManager(dbContext);

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

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

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

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

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

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

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

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

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