const axios = require("axios");
const { getValidToken } = require("./tokenManager");

async function checkWeeklyLimit(accountId) {
    try {
        const token = await getValidToken(accountId);

        const response = await axios.get(
            "https://api2.datatwix.com/rewards/v3/receipt/history?voidCache=true",
            {
                headers: {
                    "Authorization": `Bearer ${token}`,
                    "Accept": "application/json, text/plain, */*"
                }
            }
        );

        const points = response.data.currentWeekLimit.points;
        console.log(`?? Account ${accountId} has ${points} weekly points.`);
        return points;
    } catch (error) {
        console.error(`? Failed to check weekly limit for account ID ${accountId}:`, error.response?.data || error.message);
        return 0;
    }
}

module.exports = { checkWeeklyLimit };