const express = require('express');
const router = express.Router();
const SystemSettings = require('../models/SystemSettings');

module.exports = (pool, query) => {
  /**
   * Get public branding settings
   * GET /api/settings/branding
   */
  router.get('/branding', async (req, res, next) => {
    try {
      // Get all settings with 'branding' category
      const settings = await SystemSettings.getSettingsByCategory(pool, query, 'branding');
      
      // Convert array of settings to an object for easier client-side use
      const brandingSettings = {};
      settings.forEach(setting => {
        brandingSettings[setting.key] = setting.value;
      });
      
      res.json(brandingSettings);
    } catch (error) {
      next(error);
    }
  });
  
  /**
   * Get basic public settings (for meta tags, favicon, etc.)
   * GET /api/settings/meta
   */
  router.get('/meta', async (req, res, next) => {
    try {
      // Get relevant settings
      const siteNameSetting = await SystemSettings.getSetting(pool, query, 'site_name');
      const siteDescriptionSetting = await SystemSettings.getSetting(pool, query, 'site_description');
      const faviconSetting = await SystemSettings.getSetting(pool, query, 'favicon_url');
      
      // Create response object
      const metaSettings = {
        siteName: siteNameSetting?.value || 'Rocks, Bones & Sticks',
        siteDescription: siteDescriptionSetting?.value || '',
        faviconUrl: faviconSetting?.value || ''
      };
      
      res.json(metaSettings);
    } catch (error) {
      next(error);
    }
  });

  return router;
};