package ervu_business_metrics.dao;

import java.sql.Timestamp;

import ervu_business_metrics.config.KafkaEnabledCondition;
import ervu_business_metrics.model.sso.AuthEventResponse;
import org.jooq.DSLContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Repository;

import static ru.micord.webbpm.ervu.business_metrics.db_beans.auth.Tables.FAILED_AUTH;

/**
 * @author gulnaz
 */
@Repository
@Conditional(KafkaEnabledCondition.class)
public class FailedAuthDao {

  private final DSLContext dslContext;

  public FailedAuthDao(DSLContext dslContext) {
    this.dslContext = dslContext;
  }

  public void save(AuthEventResponse authEventResponse) {
    dslContext.insertInto(FAILED_AUTH)
        .set(FAILED_AUTH.ID, authEventResponse.id())
        .set(FAILED_AUTH.USER_ID, authEventResponse.userId())
        .set(FAILED_AUTH.DATETIME, new Timestamp(Long.parseLong(authEventResponse.timeCreated())))
        .execute();
  }

  public void clear() {
    dslContext.truncate(FAILED_AUTH).execute();
  }
}
