const { query } = require('../config/database');

async function get() {
  const res = await query('SELECT * FROM settings WHERE id=1');
  return res.rows[0] || null;
}

async function update(fields) {
  const allowed = ['business_name', 'business_address', 'business_phone', 'receipt_message'];
  const sets = Object.keys(fields).filter(k => allowed.includes(k));
  if (!sets.length) return get();
  const sql = `UPDATE settings SET ${sets.map((k, i) => `${k}=$${i + 1}`).join(', ')}, updated_at=NOW() WHERE id=1 RETURNING *`;
  const res = await query(sql, sets.map(k => fields[k]));
  return res.rows[0] || null;
}

module.exports = { get, update };
