package ru.micord.ervu_dashboard.dao;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jooq.DSLContext;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import ru.micord.ervu_dashboard.db_beans.appeals.tables.records.AppealsListRecord;

import static ru.micord.ervu_dashboard.db_beans.appeals.Tables.APPEALS_LIST;

/**
 * @author Adel Kalimullin
 */
@Repository
public class AppealsDao {
  private final DSLContext dslContext;

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

  public void save(List<Map<String, String>> records) {
    LocalDateTime uploadDate = LocalDateTime.now();
    List<AppealsListRecord> appealRecords = new ArrayList<>();

    for (Map<String, String> record : records) {
      Map<String, Object> fieldsMap = createFieldsMap(record, uploadDate);
      if (!fieldsMap.isEmpty()) {
        AppealsListRecord appealRecord = new AppealsListRecord();
        appealRecord.fromMap(fieldsMap);
        appealRecords.add(appealRecord);
      }
    }
      dslContext.batchMerge(appealRecords)
          .execute();
  }

  private Map<String, Object> createFieldsMap(Map<String, String> valueMap, LocalDateTime uploadDate) {
    Map<String, Object> fieldsMap = new HashMap<>();
    String numberValue = valueMap.get(APPEALS_LIST.NUMBER.getComment());
    if(!StringUtils.hasText(numberValue)){
      return Collections.emptyMap();
    }
    fieldsMap.put(APPEALS_LIST.NUMBER.getName(), Integer.valueOf(numberValue));
    fieldsMap.put(APPEALS_LIST.SOURCE.getName(), valueMap.get(APPEALS_LIST.SOURCE.getComment()));
    fieldsMap.put(APPEALS_LIST.REGION.getName(), valueMap.get(APPEALS_LIST.REGION.getComment()));
    fieldsMap.put(APPEALS_LIST.CATEGORY.getName(), valueMap.get(APPEALS_LIST.CATEGORY.getComment()));
    fieldsMap.put(APPEALS_LIST.SUBCATEGORY.getName(), valueMap.get(APPEALS_LIST.SUBCATEGORY.getComment()));
    fieldsMap.put(APPEALS_LIST.FACT.getName(), valueMap.get(APPEALS_LIST.FACT.getComment()));
    fieldsMap.put(APPEALS_LIST.INCOMING_DATE.getName(), valueMap.get(APPEALS_LIST.INCOMING_DATE.getComment()));
    fieldsMap.put(APPEALS_LIST.PLAN_END_DATE.getName(), valueMap.get(APPEALS_LIST.PLAN_END_DATE.getComment()));
    fieldsMap.put(APPEALS_LIST.FACT_END_DATE.getName(), valueMap.get(APPEALS_LIST.FACT_END_DATE.getComment()));
    fieldsMap.put(APPEALS_LIST.STAGE.getName(), valueMap.get(APPEALS_LIST.STAGE.getComment()));
    fieldsMap.put(APPEALS_LIST.STATUS.getName(), valueMap.get(APPEALS_LIST.STATUS.getComment()));
    fieldsMap.put(APPEALS_LIST.OVERDUE.getName(), valueMap.get(APPEALS_LIST.OVERDUE.getComment()));
    fieldsMap.put(APPEALS_LIST.FZ.getName(), valueMap.get(APPEALS_LIST.FZ.getComment()));
    fieldsMap.put(APPEALS_LIST.SOLUTION_TYPE.getName(), valueMap.get(APPEALS_LIST.SOLUTION_TYPE.getComment()));
    fieldsMap.put(APPEALS_LIST.RESPONSE_EVALUTION.getName(), valueMap.get(APPEALS_LIST.RESPONSE_EVALUTION.getComment()));
    fieldsMap.put(APPEALS_LIST.CONTRACTOR_ORG.getName(), valueMap.get(APPEALS_LIST.CONTRACTOR_ORG.getComment()));
    fieldsMap.put(APPEALS_LIST.SYSTEM_ESIA_ID.getName(), valueMap.get(APPEALS_LIST.SYSTEM_ESIA_ID.getComment()));
    fieldsMap.put(APPEALS_LIST.REGION_PROCESSING.getName(), valueMap.get(APPEALS_LIST.REGION_PROCESSING.getComment()));
    fieldsMap.put(APPEALS_LIST.UPLOAD_DATE.getName(), Timestamp.valueOf(uploadDate));
    return fieldsMap;
  }
}

