const express = require('express');
const app = express();

app.use(express.json());

app.get('/api/health', (req, res) => res.json({ ok: true, ts: Date.now() }));

app.use('/api/tournaments', require('./routes/tournaments'));
app.use('/api/participants', require('./routes/participants'));
app.use('/api/matches', require('./routes/matches'));

// Aliases convenient paths:
app.get('/api/tournaments/:id/participants', (req, res, next) => {
  req.url = '/by-tournament/' + req.params.id;
  return require('./routes/participants')(req, res, next);
});
app.get('/api/tournaments/:id/matches', (req, res, next) => {
  req.url = '/by-tournament/' + req.params.id;
  return require('./routes/matches')(req, res, next);
});

app.use('/api', (req, res) => res.status(404).json({ error: 'not_found' }));

const PORT = Number(process.env.PORT || 4000);
app.listen(PORT, () => console.log(`API listening on :${PORT}`));
