/* FutureMedia — server.js
 * Local backend entry point. Serves the /analyze and /download API that the
 * static frontend (../index.html) talks to on http://localhost:3000. */

const express = require('express');
const cors = require('cors');
const path = require('path');
const config = require('./config.json');
const routes = require('./routes');
const { DOWNLOADS_DIR } = require('./downloader');

const app = express();

app.use(cors({ origin: config.corsOrigin }));
app.use(express.json());
app.use(routes);

// Serve downloaded files so the browser can save them (Content-Disposition
// forces a "Save As" instead of opening inline).
app.use('/files', express.static(DOWNLOADS_DIR, {
  setHeaders: (res, filePath) => {
    res.setHeader('Content-Disposition', `attachment; filename="${path.basename(filePath)}"`);
  },
}));

// Optionally serve the frontend from the same origin too.
app.use(express.static(path.resolve(__dirname, '..')));

app.get('/health', (_req, res) => res.json({ status: 'ok' }));

app.listen(config.port, () => {
  console.log(`FutureMedia server running → http://localhost:${config.port}`);
});
