//
// Date+gameWindow.swift
// IceGlass
//
// Copyright 2026 Rouslan Zenetl. All Rights Reserved.
//

import Foundation

extension Date {
    /// Returns YYYY-MM-DD string for yesterday in Eastern Time
    static var yesterdayET: String {
        let calendar = etCalendar()
        let yesterday = calendar.date(byAdding: .day, value: -1, to: .now)!
        return formatDateString(yesterday)
    }

    /// Returns YYYY-MM-DD string for today in Eastern Time
    static var todayET: String {
        formatDateString(.now)
    }

    /// Returns YYYY-MM-DD string for tomorrow in Eastern Time
    static var tomorrowET: String {
        let calendar = etCalendar()
        let tomorrow = calendar.date(byAdding: .day, value: 1, to: .now)!
        return formatDateString(tomorrow)
    }

    private static func formatDateString(_ date: Date) -> String {
        date.formatDateET(format: "yyyy-MM-dd")
    }

    /// Returns a friendly label like "YESTERDAY", "TODAY", "TOMORROW", or a formatted date
    static func friendlyDateLabel(for dateString: String) -> String {
        if dateString == todayET { return "TODAY" }
        if dateString == yesterdayET { return "YESTERDAY" }
        if dateString == tomorrowET { return "TOMORROW" }

        // Parse and format as weekday abbreviation + date
        let inputFormatter = DateFormatter()
        inputFormatter.dateFormat = "yyyy-MM-dd"
        inputFormatter.timeZone = easternTimeZone

        if let date = inputFormatter.date(from: dateString) {
            return date.formatDateET(format: "EEE, MMM d")
        }
        return dateString
    }

    /// Returns a full label like "TODAY — Sun, Apr 12"
    static func fullDateLabel(for dateString: String) -> String {
        let friendly = friendlyDateLabel(for: dateString)

        let inputFormatter = DateFormatter()
        inputFormatter.dateFormat = "yyyy-MM-dd"
        inputFormatter.timeZone = easternTimeZone

        if let date = inputFormatter.date(from: dateString) {
            let formatted = date.formatDateET(format: "EEE, MMM d")
            if friendly == "TODAY" || friendly == "YESTERDAY" || friendly == "TOMORROW" {
                return "\(friendly) — \(formatted)"
            }
            return formatted
        }
        return friendly
    }
}
