const { currentKiviRealEstate } = require("../helpers/url");
const {
  createRealEstate,
  findRealEstateByAgencyId
} = require("../helpers/db/realEstate");
const { createKiviOriginal } = require("../helpers/db/kiviOriginal");

const { AD_CATEGORY, AD_TYPE, AD_AGENCY } = require("../common/enums");
const { APP_URL } = require("../config/appConfig");

const getPublishTypes = async (req, res) => {
  const kiviOriginal = await currentKiviRealEstate(req);

  const realEstate = await findRealEstateByAgencyId(kiviOriginal.kiviAdId);

  const title = "Koju nekretninu nudite?";
  let selectedAdType = AD_TYPE.AD_TYPE_SALE.id;
  const labelAdType = ["Prodaj", "Iznajmi"];

  if (
    realEstate &&
    realEstate.adType &&
    realEstate.adType === AD_TYPE.AD_TYPE_RENT.stringId
  ) {
    selectedAdType = AD_TYPE.AD_TYPE_RENT.id;
  }
  const realEstateTypes = Object.keys(AD_CATEGORY)
    .map(category => AD_CATEGORY[category])
    .filter(category => category.title);

  res.render("realEstateType", {
    selectedAdType,
    labelAdType,
    realEstateTypes,
    title,
    AD_TYPE
  });
};

const postPublishTypes = async (req, res) => {
  const kiviOriginal = await currentKiviRealEstate(req);

  const realEstate = await findRealEstateByAgencyId(kiviOriginal.kiviAdId);

  const adType = parseInt(req.body.adType);

  const adTypeStringIds = {
    [AD_TYPE.AD_TYPE_SALE.id]: AD_TYPE.AD_TYPE_SALE.stringId,
    [AD_TYPE.AD_TYPE_RENT.id]: AD_TYPE.AD_TYPE_RENT.stringId
  };

  const adTypeStringId =
    adTypeStringIds[adType] || AD_TYPE.AD_TYPE_SALE.stringId;

  const validRealEstateTypes = Object.keys(AD_CATEGORY).filter(
    category => !!AD_CATEGORY[category].title
  );

  const selectedRealEstateType = req.body.realEstateType || null;
  if (validRealEstateTypes.indexOf(selectedRealEstateType) === -1) {
    res.render("notFound", { title: " " });
    return;
  }

  const nextStepPage = req.query.nextStep || "podacionekretnini";

  let nextStepUrl = "";
  if (kiviOriginal && kiviOriginal.kiviAdId && realEstate && realEstate.id) {
    //
    nextStepUrl = `/${nextStepPage}/${kiviOriginal.kiviAdId}`;

    realEstate.adType = adTypeStringId;
    realEstate.realEstateType = selectedRealEstateType;
    //Url override
    realEstate.url = `${APP_URL}/preglednekretnine/${realEstate.id}`;

    await realEstate.save();
  } else {
    try {
      const newKiviOriginal = await createKiviOriginal({
        email: ""
      });
      const newKiviAdViewUrl = `${APP_URL}/preglednekretnine/${realEstate.id}`;

      const newRealEstate = await createRealEstate({
        adType: adTypeStringId,
        realEstateType: selectedRealEstateType,
        url: newKiviAdViewUrl,
        originAgencyName: AD_AGENCY.KIVI,
        agencyObjectId: newKiviOriginal.kiviAdId
      });

      nextStepUrl = `/${nextStepPage}/${newKiviOriginal.kiviAdId}`;
    } catch (error) {
      console.log(error);
      nextStepUrl = `/`;
    }
  }
  res.redirect(nextStepUrl);
};

module.exports = {
  getPublishTypes,
  postPublishTypes
};
