const moment = require("moment");

const { bulkUpsertRealEstates } = require("../../helpers/db/realEstate");
const { bulkUpsertPriceHistory } = require("../../helpers/db/priceHistory");

class PostgresSaver {
  connect() {
    //TODO: It seems we never worry about open/close connection with Sequelize ?
    //TODO: Check if postgres is ready
    return true;
  }

  async save(results) {
    const savedRecords = await bulkUpsertRealEstates(results);
    //Extruding data for price history table
    const resultPrices = savedRecords.map(realEstate => {
      //Null values canot be recognized by ignore duplicates in sequalize
      //Value price = 0 indicates 'cijena na upit'
      const priceTmp =
        realEstate.dataValues.price === null ? 0 : realEstate.dataValues.price;

      return {
        realEstateId: realEstate.dataValues.id,
        price: priceTmp,
        createdAt: realEstate.dataValues.createdAt,
        updatedAt: realEstate.dataValues.updatedAt
      };
    });
    const savedPrices = await bulkUpsertPriceHistory(resultPrices);

    if (Array.isArray(savedRecords)) {
      const newRealEstates = [];
      const existingRealEstates = [];

      for (const savedRecord of savedRecords) {
        const { createdAt, updatedAt } = savedRecord;

        const createdAtMoment = moment.utc(createdAt);
        const updatedAtMoment = moment.utc(updatedAt);

        if (createdAtMoment.isSame(updatedAtMoment, "second")) {
          newRealEstates.push(savedRecord);
        } else {
          existingRealEstates.push(savedRecord);
        }
      }

      return {
        newRecords: newRealEstates,
        existingRecords: existingRealEstates
      };
    } else {
      throw { message: "[POSTGRES] Failed to save records" };
    }
  }

  close() {
    //TODO: It seems we never worry about open/close connection with Sequelize ?
    return true;
  }
}

module.exports = PostgresSaver;
