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 AnswerServiceManager(MyDbContext dbContext) : IAnswerManager<AnswerDto>
    {

        private AnswerDataManager manager = new AnswerDataManager(dbContext);

        public async Task<AnswerDto> addAnswer(AnswerDto answerdto)
        {
            return await Task.FromResult<AnswerDto>((await manager.addAnswer(answerdto.ToModel())).ToDto());
        }

        public async Task<AnswerDto?> getAnswer(uint id)
        {
            return await Task.FromResult<AnswerDto?>((await manager.getAnswer(id))?.ToDto());
        }

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

        public async Task<IEnumerable<AnswerDto>?> getAnswersByIdQuestion(uint id)
        {
            var tmp = await manager.getAnswersByIdQuestion(id);
            List<AnswerDto>? answerdto = new List<AnswerDto>();
            if (tmp == null) answerdto = null;
            else
            {
                foreach (var item in tmp)
                {
                    answerdto.Add(item.ToDto());
                }
            }
            return await Task.FromResult<List<AnswerDto>?>(answerdto);
        }

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

        public async Task<AnswerDto?> removeAnswer(uint id)
        {
            return (await manager.removeAnswer(id))?.ToDto();
        }

        public async Task<AnswerDto?> removeAnswer(AnswerDto answerdto)
        {
            return (await manager.removeAnswer(answerdto.ToModel()))?.ToDto();
        }

        public async Task<AnswerDto?> updateAnswer(uint id, AnswerDto answerdto)
        {
            return (await manager.updateAnswer(id, answerdto.ToModel()))?.ToDto();
        }
    }
}
