function daysIntoSeason(date) {
    return Math.floor((new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24))
}

function daysSinceDate(date) {
    // If the date is after today, make it last year
    if (date > new Date()) {
        date.setFullYear(date.getFullYear() - 1);
    }
    return Math.floor((new Date().getTime() - date.getTime()) / (1000 * 60 * 60 * 24));
}

function getSeason() {
    let year = new Date().getFullYear();

    const dates = {
        pride: [new Date(`${year}-6-1`), new Date(`${year}-6-31`)],
        halloween: [new Date(`${year}-10-25`), new Date(`${year}-11-1`)],
        trans: [new Date(`${year}-11-13`), new Date(`${year}-11-19`)],
        christmas: [new Date(`${year}-12-1`), new Date(`${year}-12-26`)],
        aprilFools: [new Date(`${year}-04-01`), new Date(`${year}-04-01`)]
    }
    const filePaths = {
        normal: "normal",
        halloween: "seasonal/halloween",
        christmas: "seasonal/christmas",
        pride: "seasonal/pride",
        trans: "seasonal/trans",
        aprilFools: "seasonal/aprilFools"
    }

    let current = new Date();
    let currentSeason = Object.keys(dates).find((str) => current >= dates[str][0] && current <= dates[str][1]) || "normal";
    let currentDaysIntoSeason;
    if (currentSeason !== "normal") {
        currentDaysIntoSeason = daysIntoSeason(dates[currentSeason][0])
    } else {
        // Calculate how many days it has been since each season ended
        let days = Object.keys(dates).map((str) => daysSinceDate(dates[str][1]))
        currentDaysIntoSeason = Math.min(...days) - 1;
    }

    return {
        season: currentSeason,
        filePath: filePaths[currentSeason],
        daysIntoSeason: currentDaysIntoSeason
    }
}

const Season = async (req, res) => {
    return res.status(200).send(getSeason());
}

export default Season;
export { getSeason };
