const path = require('path');

function registerDocsRoutes(app, { rootDir, db }) {
  app.get('/docs', (_req, res) => {
    res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
    res.setHeader('Pragma', 'no-cache');
    res.setHeader('Expires', '0');
    res.setHeader('Surrogate-Control', 'no-store');
    res.sendFile(path.join(rootDir, 'public', 'docs.html'));
  });

  app.get('/api/docs/context', (_req, res) => {
    const settings = db.prepare('SELECT owner_name, company_name, city FROM settings WHERE id = 1').get() || {};
    const services = db.prepare('SELECT name, description FROM services ORDER BY id ASC').all();
    res.json({
      owner_name: settings.owner_name || '',
      company_name: settings.company_name || '',
      city: settings.city || '',
      services
    });
  });
}

module.exports = { registerDocsRoutes };
