import { Router, Request, Response } from 'express';
import type { WordBank, GuessResponse } from '@wordle/shared';
import { isValidGuess, getTargetWord } from '../validate.js';
import { computeFeedback } from '../feedback.js';

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

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

    // Validate request shape
    if (typeof wordId !== 'number' || typeof guess !== 'string' || typeof tryNo !== 'number') {
      res.status(400).json({ error: 'Invalid request: wordId (number), guess (string), and tryNo (number) required' });
      return;
    }

    if (guess.length !== 5) {
      res.status(400).json({ error: 'Guess must be exactly 5 letters' });
      return;
    }

    // Validate the guess is a real word
    if (!isValidGuess(wordBank, guess)) {
      const response: GuessResponse = { valid: false };
      res.json(response);
      return;
    }

    // Look up target word
    const target = getTargetWord(wordBank, wordId);
    if (!target) {
      res.status(404).json({ error: `Word ${wordId} not found` });
      return;
    }

    // Compute feedback
    const colors = computeFeedback(target.word, guess);
    const correct = colors.every((c) => c === 'g');
    const isLastAttempt = tryNo >= 6;

    const response: GuessResponse = {
      valid: true,
      colors,
      correct,
    };

    // Reveal word on loss (last attempt, not correct)
    if (isLastAttempt && !correct) {
      response.word = target.word;
    }

    // Include difficulty and replays on final guess (win or loss)
    if (correct || isLastAttempt) {
      response.difficulty = target.difficulty;
      if (target.replays) {
        response.replays = target.replays;
      }
    }

    res.json(response);
  });

  return router;
}
