import { Router, Request, Response } from 'express';
import type { WordBank } from '@wordle/shared';
import { selectPlayAgainWord } from '../play-again.js';

export function createPlayAgainRouter(wordBank: WordBank): Router {
  const router = Router();

  router.post('/', (req: Request, res: Response) => {
    const { skillMetric } = req.body ?? {};

    if (typeof skillMetric !== 'number') {
      res.status(400).json({ error: 'skillMetric (number) required' });
      return;
    }

    const target = selectPlayAgainWord(wordBank.targets, skillMetric);
    res.json({ wordId: target.id });
  });

  return router;
}
