package org.ros.chatto.service;

import java.util.List;
import java.util.Optional;

import com.spencerwi.either.Result;

import org.ros.chatto.dto.AdminUserDTO;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.repository.UserRepository;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j
public class AdminService {
    private final UserRepository userRepository;

    public List<AdminUserDTO> getOtherUsers(String principal) {
        return userRepository.getOtherUsers(principal);
    }

    public Result<Void> deleteUser(String userName) {
        Optional<ChatUser> user = userRepository.findByUserName(userName);
        return Result.attempt(() -> user.get()).map((u) -> {
            userRepository.delete(u);
            return (Void) null;
        });
    }
}