using data.Exception;
using data.Repository;
using domain.Models;

namespace domain.UseCase
{
    public class UserUseCase
    {
        private readonly IUserRepository _repositoryUserImpl;
        private readonly IGroupRepository _repositoryGroupImpl;

        public UserUseCase(IUserRepository repositoryImpl, IGroupRepository repositoryGroupImpl)
        {
            _repositoryUserImpl = repositoryImpl;
            _repositoryGroupImpl = repositoryGroupImpl;
        }

        public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers
            .Join(_repositoryGroupImpl.GetAllGroup(),
            user => user.GroupID,
            group => group.Id,
            (user, group) =>
            new User
            {
                FIO = user.FIO,
                Guid = user.Guid,
                Group = new Group { Id = group.Id, Name = group.Name }
            }).ToList();

        public bool RemoveUserByGuid(Guid userGuid)
        {
            try
            {
                return _repositoryUserImpl.RemoveUserByGuid(userGuid);
            }
            catch (UserNotFoundException)
            {
                return false;
            }
            catch (RepositoryException)
            {
                return false;
            }
        }

        
        public User UpdateUser(User user)
        {
            UserLocalEnity userLocalEnity = new UserLocalEnity
            {
                FIO = user.FIO,
                GroupID = user.Group.Id,
                Guid = user.Guid
            };

            UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity);

            if (result == null)
            {
                throw new Exception("Ошибка при обновлении пользователя.");
            }

            var groupEntity = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == result.GroupID);

            if (groupEntity == null)
            {
                throw new Exception("Группа не найдена.");
            }

            return new User
            {
                FIO = result.FIO,
                Guid = result.Guid,
                Group = new Group
                {
                    Id = groupEntity.Id,
                    Name = groupEntity.Name
                }
            };
        }

        
        public User FindUserByGuid(Guid userGuid)
        {
            var user = _repositoryUserImpl.GetAllUsers
                .FirstOrDefault(u => u.Guid == userGuid);

            if (user == null)
            {
                throw new Exception("Пользователь не найден.");
            }

            var group = _repositoryGroupImpl.GetAllGroup()
                .FirstOrDefault(g => g.Id == user.GroupID);

            if (group == null)
            {
                throw new Exception("Группа не найдена.");
            }

            return new User
            {
                FIO = user.FIO,
                Guid = user.Guid,
                Group = new Group { Id = group.Id, Name = group.Name }
            };
        }
    }
    
}
