import express from 'express';
import morgan from 'morgan';
import path from 'path';
import { fileURLToPath } from 'url';
import apiRouter from './routes/api.js';
import radiusRouter from './routes/radius.js';

export function createApp() {
  const app = express();
  app.use(express.json());
  app.use(morgan('dev'));

  const __filename = fileURLToPath(import.meta.url);
  const __dirname = path.dirname(__filename);
  app.use(express.static(path.join(__dirname, '..', 'public')));

  // RADIUS hooks (rlm_rest)
  app.use('/', radiusRouter);

  // REST API
  app.use('/api', apiRouter);

  app.get('/', (_req, res) => {
    res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
  });

  return app;
}

