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

const getTodaysSpellingBee = 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/spelling-bee/v1/${date}.json`;
        const response = await axios.get(url);
        res.json(response.data);
    } catch (error) {
        console.error("Error fetching Spelling Bee data:", error);
        res.status(500).json({ error: 'Failed to fetch Spelling Bee data' });
    }
};

export { getTodaysSpellingBee };