import { Request, Response, NextFunction } from 'express';
import axios from 'axios';

const getTodaysWordle = async (req: Request, res: Response, next: NextFunction) => {
    try {
        let date = req.query.date as string;
        if (!date) {
            date = new Date().toISOString().split('T')[0];
        }
        const url = `https://www.nytimes.com/svc/wordle/v2/${date}.json`;
        const response = await axios.get(url);
        res.json({ solution: response.data });
    } catch (error) {
        console.error("Error fetching Wordle solution:", error);
        res.status(500).json({ error: 'Failed to fetch Wordle solution' });
    }
};

export { getTodaysWordle };