package com.oguerreiro.resilient.service.security;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oguerreiro.resilient.repository.security.SecurityGroupPermissionRepository;
import com.oguerreiro.resilient.repository.security.SecurityGroupRepository;
import com.oguerreiro.resilient.security.resillient.SecurityGroup;
import com.oguerreiro.resilient.service.AbstractResilientService;
import com.oguerreiro.resilient.service.dto.security.SecurityGroupDTO;
import com.oguerreiro.resilient.service.dto.security.SecurityGroupPermissionDTO;
import com.oguerreiro.resilient.service.mapper.security.SecurityGroupMapper;

import jakarta.persistence.EntityManager;

/**
 * Service Implementation for managing {@link com.oguerreiro.resilient.security.SecurityGroup}.
 */
@Service
@Transactional
public class SecurityGroupService extends AbstractResilientService<SecurityGroupDTO, SecurityGroup, Long> {

  private final SecurityGroupPermissionRepository securityGroupPermissionRepository;
  private final SecurityGroupMapper securityGroupMapper;

  public SecurityGroupService(SecurityGroupRepository securityGroupRepository, SecurityGroupMapper securityGroupMapper,
      EntityManager entityManager, SecurityGroupPermissionRepository securityGroupPermissionRepository) {
    super(SecurityGroup.class, securityGroupRepository, securityGroupMapper);
    this.securityGroupPermissionRepository = securityGroupPermissionRepository;
    this.securityGroupMapper = securityGroupMapper;
  }

  @Transactional(readOnly = true)
  public List<SecurityGroupPermissionDTO> findPermissionsFor(Long securityGroupId) {
    log.debug("Request to findPermissionsFor " + this.getClass().getSimpleName() + "'s.");
    return securityGroupPermissionRepository.findBySecurityGroupId(securityGroupId).stream().map(
        securityGroupMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
  }
}
