package ervu_business_metrics.service;


import java.util.List;
import java.util.Set;

import ervu_business_metrics.dao.IdmDirectoriesDao;
import ervu_business_metrics.config.KafkaEnabledCondition;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Repository;
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.*;


/**
 * @author Adel Kalimullin
 */
@Repository
@Conditional(KafkaEnabledCondition.class)
public class IdmDirectoriesDaoService {
  private final IdmDirectoriesDao idmDirectoriesDao;

  public IdmDirectoriesDaoService(IdmDirectoriesDao idmDirectoriesDao) {
    this.idmDirectoriesDao = idmDirectoriesDao;
  }

  public RoleRecord getRoleRecord() {
    return idmDirectoriesDao.getRoleRecord();
  }

  public DomainRecord getDomainRecord() {
    return idmDirectoriesDao.getDomainRecord();
  }

  public AccountRecord getAccountRecord() {
    return idmDirectoriesDao.getAccountRecord();
  }

  public AccountRoleRecord getAccountRoleRecord() {
    return idmDirectoriesDao.getAccountRoleRecord();
  }

  @Cacheable(value = "account-ids", unless = "#result == null")
  public Set<String> getAccountIds() {
    return idmDirectoriesDao.getAccountIds();
  }

  @Cacheable(value = "role-ids", unless = "#result == null")
  public Set<String> getRoleIds() {
    return idmDirectoriesDao.getRoleIds();
  }

  @Cacheable(value = "domain-ids", unless = "#result == null")
  public Set<String> getDomainIds() {
    return idmDirectoriesDao.getDomainIds();
  }

  @CacheEvict(value = "domain-ids", allEntries = true)
  public void insertDomainRecords(List<DomainRecord> newDomainRecords) {
    idmDirectoriesDao.insertDomainRecords(newDomainRecords);
  }

  public void updateDomainRecords(List<DomainRecord> domainRecords) {
    idmDirectoriesDao.updateDomainRecords(domainRecords);
  }

  @CacheEvict(value = "role-ids", allEntries = true)
  public void insertRoleRecords(List<RoleRecord> newRoleRecords) {
    idmDirectoriesDao.insertRoleRecords(newRoleRecords);
  }

  public void updateRoleRecords(List<RoleRecord> roleRecords) {
    idmDirectoriesDao.updateRoleRecords(roleRecords);
  }

  @CacheEvict(value = "account-ids", allEntries = true)
  public void insertAccountRecords(List<AccountRecord> accountRecords) {
    idmDirectoriesDao.insertAccountRecords(accountRecords);
  }

  public void updateAccountRecords(List<AccountRecord> accountRecords) {
    idmDirectoriesDao.updateAccountRecords(accountRecords);
  }

  public void insertAccountRoleRecords(List<AccountRoleRecord> newAccountRoleRecords) {
    idmDirectoriesDao.insertAccountRoleRecords(newAccountRoleRecords);
  }

  public void deleteAccountRolesByAccountIds(List<String> accountIds) {
    idmDirectoriesDao.deleteAccountRolesByAccountIds(accountIds);
  }

  @CacheEvict(value = "domain-ids", allEntries = true)
  public void deleteDomainsByIds(List<String> domainIds) {
    idmDirectoriesDao.deleteDomainsByIds(domainIds);
  }

  @CacheEvict(value = "account-ids", allEntries = true)
  public void deleteAccountsByIds(List<String> accountIds) {
    idmDirectoriesDao.deleteAccountByIds(accountIds);
  }
}
