using AutoMapper;
using DigitalData.Core.Application;
using DigitalData.Core.DTO;
using DigitalData.UserManager.Application.Contracts;
using DigitalData.UserManager.Application.DTOs.ModuleOfUser;
using DigitalData.UserManager.Domain.Entities;
using DigitalData.UserManager.Infrastructure.Contracts;

namespace DigitalData.UserManager.Application.Services
{
    public class ModuleOfUserService : CRUDService<IModuleOfUserRepository, ModuleOfUserCreateDto, ModuleOfUserReadDto, ModuleOfUserUpdateDto, ModuleOfUser, int>, IModuleOfUserService
    {
        public ModuleOfUserService(IModuleOfUserRepository repository, IMapper mapper) : base(repository, mapper)
        {
        }

        public async Task<Result> DeleteAsyncByModuleUserId(int moduleId, int userId)
        {
            var mous = await _repository.ReadByModelUserIdAsync(moduleId, userId);

            foreach(var mou in mous)
            {
                await _repository.DeleteAsync(mou);
            }

            return Result.Success();
        }

        public async Task<DataResult<IEnumerable<ModuleOfUserReadDto>>> ReadByUserAsync(string username)
        {
            var mous = await _repository.ReadByUserAsync(username: username);
            var mous_dtos = _mapper.Map<IEnumerable<ModuleOfUserReadDto>>(mous);
            return Result.Success(mous_dtos);
        }
    }
}