const { getSettings, updateSettings } = require('../models/settingsModel');

async function index(req, res) {
  const settings = await getSettings();
  res.render('settings/index', { title: 'Application Settings', settings });
}

async function update(req, res) {
  const body = { ...req.body };
  if (req.files?.logo?.[0]) body.logo_path = `/public/uploads/${req.files.logo[0].filename}`;
  if (req.files?.signature?.[0]) body.signature_path = `/public/uploads/${req.files.signature[0].filename}`;
  if (req.files?.seal?.[0]) body.seal_path = `/public/uploads/${req.files.seal[0].filename}`;
  await updateSettings(body);
  req.flash('success', 'Settings updated successfully.');
  res.redirect('/settings');
}

module.exports = { index, update };
