import { Request, Response } from 'express';
import axios from 'axios';
import * as cheerio from 'cheerio';

export class ContextoController {
  public async getAnswer(req: Request, res: Response): Promise<void> {
    try {
      const url = 'https://ladypuzzle.pro/contexto-answer-today';
      const { data: html } = await axios.get(url);
      const $ = cheerio.load(html);

      const answer = $('.puzzle-container .puzzle button .back').map((_, el) => $(el).text()).get().join('');

      res.json({ answer });
    } catch (error: any) {
      console.error('Error getting Contexto answer:', error.message);
      res.status(500).json({ error: 'Error getting Contexto answer' });
    }
  }
}