package ervu_business_metrics.service.processor.impl;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import ervu_business_metrics.config.KafkaEnabledCondition;
import ervu_business_metrics.model.idm.DomainData;
import ervu_business_metrics.service.IdmDirectoriesDaoService;
import ervu_business_metrics.service.processor.DataProcessor;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;
import ru.micord.webbpm.ervu.business_metrics.db_beans.idm_reconcile.tables.records.DomainRecord;

/**
 * @author Adel Kalimullin
 */
@Component
@Conditional(KafkaEnabledCondition.class)
public class DomainDataProcessor implements DataProcessor<DomainData> {
  private final IdmDirectoriesDaoService idmDirectoriesDaoService;

  public DomainDataProcessor(IdmDirectoriesDaoService idmDirectoriesDaoService) {
    this.idmDirectoriesDaoService = idmDirectoriesDaoService;
  }

  @Override
  public void upsertData(List<DomainData> dataList) {
    List<DomainRecord> newDomainRecords = new ArrayList<>();
    List<DomainRecord> domainRecords = new ArrayList<>();
    Set<String> existingIds = idmDirectoriesDaoService.getDomainIds();

    for (DomainData data : dataList) {
      DomainRecord domainRecord = idmDirectoriesDaoService.getDomainRecord();
      Timestamp modifiedAt = Timestamp.from(Instant.ofEpochMilli(data.getModified()));

      domainRecord.setId(data.getId());
      domainRecord.setVersion(data.getVersion());
      domainRecord.setModified(modifiedAt);
      domainRecord.setSchema(data.getSchema());
      domainRecord.setName(data.getName());
      domainRecord.setShortname(data.getShortname());
      domainRecord.setFullname(data.getFullname());
      domainRecord.setDns(data.getDns());
      domainRecord.setEmail(data.getEmail());
      domainRecord.setPhone(data.getPhone());
      domainRecord.setAddress(data.getAddress());
      domainRecord.setPostalAddress(data.getPostalAddress());
      domainRecord.setAddressId(data.getAddressId());
      domainRecord.setPostalAddressId(data.getPostalAddressId());
      domainRecord.setMilitaryCode(data.getMilitaryCode());
      domainRecord.setTimezone(data.getTimezone());
      domainRecord.setReportsEnabled(data.isReportsEnabled());
      domainRecord.setInn(data.getInn());
      domainRecord.setLeg(data.getLeg());
      domainRecord.setOgrn(data.getOgrn());
      domainRecord.setRegion(data.getRegion());
      domainRecord.setEpguId(data.getEpguId());
      domainRecord.setType(data.getType());
      domainRecord.setEsiaEmployeeAuthorization(data.isEsiaEmployeeAuthorization());
      domainRecord.setDefaultS3Bucket(data.getDefaultS3Bucket());
      domainRecord.setOpf(data.getOpf());
      domainRecord.setKpp(data.getKpp());
      domainRecord.setCheckingAccount(data.getCheckingAccount());
      domainRecord.setBik(data.getBik());
      domainRecord.setBankName(data.getBankName());
      domainRecord.setBankCorrespondentAccount(data.getBankCorrespondentAccount());
      domainRecord.setOktmo(data.getOktmo());
      domainRecord.setOkato(data.getOkato());
      domainRecord.setGovRegistrationDate(data.getGovRegistrationDate());
      domainRecord.setGovOrganizationType(data.getGovOrganizationType());
      domainRecord.setAliasKey(data.getAliasKey());
      domainRecord.setPassKey(data.getPassKey());
      domainRecord.setCertificate(data.getCertificate());
      domainRecord.setAccountNumberTofk(data.getAccountNumberTOFK());
      domainRecord.setBikTofk(data.getBikTOFK());
      domainRecord.setCorrespondentBankAccountTofk(data.getCorrespondentBankAccountTOFK());
      domainRecord.setNameTofk(data.getNameTOFK());
      domainRecord.setNsiOrganizationId(data.getNsiOrganizationId());
      domainRecord.setDocHandle(data.getDocHandle());
      domainRecord.setDivisionType(data.getDivisionType());
      domainRecord.setTnsDepartmentId(data.getTnsDepartmentId());
      domainRecord.setEnabled(data.isEnabled());
      domainRecord.setParent(data.getParent());
      domainRecord.setRegionId(data.getRegionId());
      domainRecord.setManaged(data.getManaged());

      if (existingIds.contains(data.getId())) {
        domainRecords.add(domainRecord);
      }
      else {
        newDomainRecords.add(domainRecord);
      }
    }

    if (!newDomainRecords.isEmpty()) {
      idmDirectoriesDaoService.insertDomainRecords(newDomainRecords);
    }

    if (!domainRecords.isEmpty()) {
      idmDirectoriesDaoService.updateDomainRecords(domainRecords);
    }
  }

  @Override
  public void deleteData(List<String> ids) {
    idmDirectoriesDaoService.deleteDomainsByIds(ids);
  }
}
