import express from 'express';
import { readFileSync, existsSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import type { WordBank } from '@wordle/shared';
import { createDailyRouter } from './routes/daily.js';
import { createGuessRouter } from './routes/guess.js';
import { createPlayAgainRouter } from './routes/play-again.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

function loadBank(filename: string): WordBank {
  const path = resolve(__dirname, '../../data', filename);
  return JSON.parse(readFileSync(path, 'utf-8'));
}

function mountApi(prefix: string, bank: WordBank) {
  return [
    [prefix + '/daily', createDailyRouter(bank)],
    [prefix + '/guess', createGuessRouter(bank)],
    [prefix + '/play-again', createPlayAgainRouter(bank)],
  ] as const;
}

const enBank = loadBank('word-bank.json');
const ruBank = loadBank('word-bank-ru.json');

const app = express();
app.use(express.json());

// Mount API routes for both languages
for (const [path, router] of [...mountApi('/api', enBank), ...mountApi('/ru/api', ruBank)]) {
  app.use(path, router);
}

// Serve SPA
const clientDist = (() => {
  if (process.env.STATIC_DIR) return resolve(process.env.STATIC_DIR);
  const devPath = resolve(__dirname, '../../client/dist');
  return existsSync(devPath) ? devPath : resolve(__dirname, '../../client');
})();
app.use(express.static(clientDist));
app.get('/{*path}', (_req, res) => {
  res.sendFile(resolve(clientDist, 'index.html'));
});

const PORT = process.env.PORT ? parseInt(process.env.PORT) : 3000;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT} (EN + RU)`);
});

export default app;
