// @ts-check
const { HomeModel } = require('../models/Home');

async function getHome(req, res) {
  const doc = await HomeModel.findOne({});
  if (!doc) return res.status(404).json({ message: 'Home non configurata' });
  res.set('Cache-Control', 'public, max-age=60, stale-while-revalidate=300');
  res.set('ETag', `"${doc.updatedAt?.getTime?.() || Date.now()}"`);
  return res.json(doc);
}

async function upsertHome(req, res) {
  const payload = req.body || {};
  const doc = await HomeModel.findOneAndUpdate({}, payload, { upsert: true, new: true, setDefaultsOnInsert: true });
  return res.json(doc);
}

module.exports = { getHome, upsertHome };