import { Router, Request, Response } from 'express';
import type { WordBank, ValidateResponse } from '@wordle/shared';

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

  router.get('/:word', (req: Request, res: Response) => {
    const word = req.params.word ?? '';

    if (word.length !== 5) {
      const response: ValidateResponse = { word, valid: false };
      res.json(response);
      return;
    }

    const valid = wordBank.guessable.includes(word.toLowerCase());
    const response: ValidateResponse = { word, valid };
    res.json(response);
  });

  return router;
}
