package io.gmss.fiscad.implementations.user;

import io.gmss.fiscad.entities.user.Role;
import io.gmss.fiscad.enums.UserRole;
import io.gmss.fiscad.exceptions.BadRequestException;
import io.gmss.fiscad.exceptions.NotFoundException;
import io.gmss.fiscad.interfaces.user.RoleService;
import io.gmss.fiscad.repositories.user.RoleRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

@Service
public class RoleServiceImpl implements RoleService {

    private final RoleRepository roleRepository;

    public RoleServiceImpl(RoleRepository roleRepository) {
        this.roleRepository = roleRepository;
    }

    @Override
    public Role createRole(Role role) throws BadRequestException {
        if (role.getId() != null) {
            throw new BadRequestException("A new role id to save must be null or empty.");
        }
        return roleRepository.save(role);
    }

    @Override
    public Role updateRole(Long id, Role role) throws NotFoundException {
        return null;
    }

    @Override
    public void deleteRole(Long id) throws NotFoundException {
    }

    @Override
    public Page<Role> getRoleList(Pageable pageable) {
        return roleRepository.findAll(pageable);
    }

    @Override
    public List<Role> getRoleList() {
        return roleRepository.findAll();
    }


    @Override
    public Optional<Role> getRoleById(Long id) {
        return roleRepository.findRoleById(id);
    }

    @Override
    public Optional<Role> getRoleByRoleName(UserRole userRole) {
        return roleRepository.findRoleByNom(userRole);
    }

    @Override
    public boolean roleExistByRoleName(UserRole userRole) {
        return roleRepository.existsByNom(userRole);
    }

    @Override
    public Role retrieveRoleByRoleName(UserRole userRole) {
        return roleRepository.getRolesByNom(userRole);
    }
}
