package ervu_business_metrics.dao;

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

import ervu_business_metrics.config.KafkaEnabledCondition;
import org.jooq.DSLContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Repository;
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.AccountRecord;
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.AccountRoleRecord;
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.DomainRecord;
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.RoleRecord;

import static ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.Tables.ACCOUNT;
import static ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.Tables.ACCOUNT_ROLE;
import static ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.Tables.DOMAIN;
import static ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.Tables.ROLE;

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

  public IdmDirectoriesDao(DSLContext dsl) {
    this.dsl = dsl;
  }

  public RoleRecord getRoleRecord() {
    return dsl.newRecord(ROLE);
  }

  public DomainRecord getDomainRecord() {
    return dsl.newRecord(DOMAIN);
  }

  public AccountRecord getAccountRecord() {
    return dsl.newRecord(ACCOUNT);
  }

  public AccountRoleRecord getAccountRoleRecord() {
    return dsl.newRecord(ACCOUNT_ROLE);
  }

  public Set<String> getAccountIds() {
    return dsl.select(ACCOUNT.ID)
        .from(ACCOUNT)
        .fetchSet(ACCOUNT.ID);
  }

  public Set<String> getRoleIds() {
    return dsl.select(ROLE.ID)
        .from(ROLE)
        .fetchSet(ROLE.ID);
  }

  public Set<String> getDomainIds() {
    return dsl.select(DOMAIN.ID)
        .from(DOMAIN)
        .fetchSet(DOMAIN.ID);
  }

  public void insertDomainRecords(List<DomainRecord> domainRecords) {
    dsl.batchInsert(domainRecords).execute();
  }

  public void updateDomainRecords(List<DomainRecord> domainRecords) {
    dsl.batchUpdate(domainRecords).execute();
  }

  public void insertRoleRecords(List<RoleRecord> newRoleRecords) {
    dsl.batchInsert(newRoleRecords).execute();
  }

  public void updateRoleRecords(List<RoleRecord> roleRecords) {
    dsl.batchUpdate(roleRecords).execute();
  }

  public void insertAccountRecords(List<AccountRecord> newAccountRecords) {
    dsl.batchInsert(newAccountRecords).execute();
  }

  public void updateAccountRecords(List<AccountRecord> accountRecords) {
    dsl.batchUpdate(accountRecords).execute();
  }

  public void insertAccountRoleRecords(List<AccountRoleRecord> newAccountRoleRecords) {
    dsl.batchInsert(newAccountRoleRecords).execute();
  }

  public void deleteAccountRolesByAccountIds(List<String> accountIds) {
    dsl.deleteFrom(ACCOUNT_ROLE)
        .where(ACCOUNT_ROLE.ACCOUNT_ID.in(accountIds))
        .execute();
  }

  public void deleteAccountByIds(List<String> accountIds) {
    dsl.deleteFrom(ACCOUNT)
        .where(ACCOUNT.ID.in(accountIds))
        .execute();
  }

  public void deleteDomainsByIds(List<String> domainIds) {
    dsl.deleteFrom(DOMAIN)
        .where(DOMAIN.ID.in(domainIds))
        .execute();
  }
}
