import Foundation

// MARK: - Date Extensions

extension Date {
    /// Formats date as "MMM d, yyyy" (e.g., "Jan 15, 2024")
    func formattedMedium() -> String {
        DateFormatters.shared.mediumDate.string(from: self)
    }

    /// Formats date as "MMMM d, yyyy" (e.g., "January 15, 2024")
    func formattedLong() -> String {
        DateFormatters.shared.longDate.string(from: self)
    }

    /// Formats date as "MM/dd/yyyy" (e.g., "01/15/2024")
    func formattedShort() -> String {
        DateFormatters.shared.shortDate.string(from: self)
    }

    /// Formats date as "yyyy-MM-dd" for API requests
    func formattedAPI() -> String {
        DateFormatters.shared.apiDate.string(from: self)
    }

    /// Checks if date is in the past
    var isPast: Bool {
        self < Date()
    }

    /// Checks if date is today
    var isToday: Bool {
        Calendar.current.isDateInToday(self)
    }

    /// Checks if date is tomorrow
    var isTomorrow: Bool {
        Calendar.current.isDateInTomorrow(self)
    }

    /// Returns number of days from today (negative if in past)
    var daysFromToday: Int {
        Calendar.current.dateComponents([.day], from: Date(), to: self).day ?? 0
    }

    /// Returns relative description (e.g., "Today", "Tomorrow", "In 3 days", "2 days ago")
    var relativeDescription: String {
        if isToday {
            return "Today"
        } else if isTomorrow {
            return "Tomorrow"
        } else {
            let days = daysFromToday
            if days > 0 {
                return "In \(days) day\(days == 1 ? "" : "s")"
            } else if days < 0 {
                return "\(abs(days)) day\(abs(days) == 1 ? "" : "s") ago"
            } else {
                return "Today"
            }
        }
    }
}

// MARK: - String to Date Extensions

extension String {
    /// Converts API date string (yyyy-MM-dd or ISO datetime) to Date
    func toDate() -> Date? {
        // Extract date part if it includes time (e.g., "2025-01-02T00:00:00Z" -> "2025-01-02")
        let datePart = self.components(separatedBy: "T").first ?? self
        return DateFormatters.shared.apiDate.date(from: datePart)
    }

    /// Converts API date string to formatted display string (e.g., "Jan 2, 2025")
    func toFormattedDate() -> String {
        guard let date = self.toDate() else { return self }
        return date.formattedMedium()
    }

    /// Checks if date string represents an overdue date
    func isOverdue() -> Bool {
        guard let date = self.toDate() else { return false }
        return date.isPast && !date.isToday
    }
}

// MARK: - Centralized Date Formatters

class DateFormatters {
    static let shared = DateFormatters()

    private init() {}

    /// "MMM d, yyyy" - e.g., "Jan 15, 2024"
    lazy var mediumDate: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "MMM d, yyyy"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()

    /// "MMMM d, yyyy" - e.g., "January 15, 2024"
    lazy var longDate: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "MMMM d, yyyy"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()

    /// "MM/dd/yyyy" - e.g., "01/15/2024"
    lazy var shortDate: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "MM/dd/yyyy"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()

    /// "yyyy-MM-dd" - API format
    lazy var apiDate: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()

    /// "h:mm a" - e.g., "3:30 PM"
    lazy var time: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "h:mm a"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()

    /// "MMM d, yyyy 'at' h:mm a" - e.g., "Jan 15, 2024 at 3:30 PM"
    lazy var dateTime: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "MMM d, yyyy 'at' h:mm a"
        formatter.locale = Locale(identifier: "en_US_POSIX")
        return formatter
    }()
}

// MARK: - DateUtils Compatibility Layer
// Note: The main DateUtils enum is in Helpers/DateUtils.swift
// These extensions provide additional convenience methods
