using Zurnal.Data.Repository;
using Zurnal.domain.Models;
using Zurnal.RemaDateBase.DateDao;

namespace Zurnal.Domain.UseCase
{
    public class UserUseCase : IUserRepository
    {
        private readonly UserRepositoryImpl _repositoryUserImpl;
        internal IGroupRepository RepositoryGroupImpl { get; }

        List<UserLocalEnity> IUserRepository.GetAllUsers => throw new NotImplementedException();

        public List<GroupDao> AllGroup => throw new NotImplementedException();

        public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl)
        {
            _repositoryUserImpl = repositoryImpl;
            RepositoryGroupImpl = (IGroupRepository?)(repositoryGroupImpl ?? throw new ArgumentNullException(nameof(repositoryGroupImpl)));
        }

        public List<UserDao> GetAllUsers() => _repositoryUserImpl.GetAllUsersList()
            .Join(RepositoryGroupImpl.AllGroup,
            user => user.GroupID,
            group => group.Id,
            (user, group) => 
    new UserDao { FIO = user.FIO, 
        UserGuid = user.Guid, 
        Group = new GroupDao { Id = group.Id, GroupName = group.GroupName }, 
        GroupID = group.Id }
    ).ToList();

        public bool RemoveUserByGuid(Guid userGuid) 
        {
            return _repositoryUserImpl.RemoveUserByGuid(userGuid);
        }

        public IEnumerable<GroupDao> GetAllGroups()
{
    return RepositoryGroupImpl.AllGroup;
}

        public UserDao FindUserByGuid(Guid userGuid)
{
    var user = _repositoryUserImpl.GetAllUsersList().FirstOrDefault(u => u.Guid == userGuid);
    if (user == null) 
    {
        Console.WriteLine("Пользователь не найден.");
        return null; 
    }
    
    GroupDao group = RepositoryGroupImpl.AllGroup.FirstOrDefault(g => g.Id == user.GroupID);
    if (group == null) 
    {
        Console.WriteLine("Группа не найдена.");
        return null; 
    }
    
    return new UserDao { FIO = user.FIO, UserGuid = user.Guid, Group = group, GroupID = group.Id };
}

        public UserLocalEnity? GetUserByGuid(Guid userGuid)
        {
            throw new NotImplementedException();
        }

        public UserLocalEnity? UpdateUser(UserLocalEnity userUpdateLocalEnity)
        {
            throw new NotImplementedException();
        }

        internal class UserLocalEntity 
        {
            public string FIO { get; set; }
            public int GroupID { get; set; }
            public Guid Guid { get; set; }
        }
    }
}