package ru.micord.ervu.account_applications.dao;

import java.util.List;
import java.util.Map;

import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Table;
import org.springframework.stereotype.Repository;
import ru.micord.ervu.account_applications.db_beans.public_.Tables;
import ru.micord.ervu.account_applications.db_beans.public_.tables.records.SolutionRoleRecord;

/**
 * @author Adel Kalimullin
 */
@Repository
public class SolutionRoleDao extends AbstractDataDao<SolutionRoleRecord> {
  protected SolutionRoleDao(DSLContext dsl) {
    super(dsl);
  }

  public void deleteSolutionRole(SolutionRoleRecord record) {
    Map<Field<String>, String> conditions = Map.of(
        Tables.SOLUTION_ROLE.SOLUTION_ID, record.getSolutionId(),
        Tables.SOLUTION_ROLE.ROLE_ID, record.getRoleId()
    );

    deleteByValues(conditions);
  }

  public List<String> getRoleIdsBySolutionId(String solutionId) {
    return getValuesByField(
        Tables.SOLUTION_ROLE.ROLE_ID,
        Tables.SOLUTION_ROLE.SOLUTION_ID, solutionId
    );
  }

  public void deleteSolutionRolesBySolutionIdAndRoleIds(String solutionId,
      List<String> roleIds) {
    deleteByFieldAndInValues(
        Tables.SOLUTION_ROLE.SOLUTION_ID, solutionId,
        Tables.SOLUTION_ROLE.ROLE_ID, roleIds
    );
  }

  @Override
  protected Table<SolutionRoleRecord> getTable() {
    return Tables.SOLUTION_ROLE;
  }
}
