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

const getDeletePublishedAd = async (req, res) => {
  const title = "Uspješno ste izbrisali svoj oglas iz baze.";

  const kiviOriginal = await currentKiviRealEstate(req);

  if (!kiviOriginal || !kiviOriginal.kiviAdId) {
    res.render("notFound", { title: " " });
    return;
  }
  const realEstate = await findRealEstateByAgencyId(kiviOriginal.kiviAdId);

  if (!realEstate || !realEstate.dataValues) {
    res.render("notFound", { title: " " });
    return;
  }

  realEstate.deleted = true;
  realEstate.deletedAt = Date.now();

  kiviOriginal.email = "";

  await realEstate.save();

  await kiviOriginal.save();

  res.render("deleteRealEstate", { nextStep: "/", title });
};

module.exports = {
  getDeletePublishedAd
};
