//
//  Date+Extensions.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 9/7/25.
//

import Foundation

extension Date {
    
    /// Format date for display in overlay with custom format
    /// - Parameter format: Date format string (e.g., "d MMMM EEE")
    /// - Returns: Formatted date string
    func formattedForOverlay(format: String = "d MMMM EEE") -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
    
    /// Get available date format options with their display names
    /// - Returns: Array of (displayName, formatString) tuples
    static func availableDateFormats() -> [(String, String)] {
        let today = Date()
        let formatStrings = [
            "d MMMM EEE",
            "MMMM d, EEE",
            "EEE, MMM d",
            "d MMM yyyy",
            "MM/dd/yyyy",
            "dd/MM/yyyy",
            "yyyy-MM-dd",
            "MMM d",
            "MMMM d",
            "EEEE, MMM d"
        ]
        
        return formatStrings.map { formatString in
            let example = today.formattedForOverlay(format: formatString)
            return (example, formatString)
        }
    }
    
    /// Get time components for alarm scheduling
    /// - Returns: DateComponents with hour and minute
    func timeComponents() -> DateComponents {
        return Calendar.current.dateComponents([.hour, .minute], from: self)
    }
    
    /// Check if date is today
    /// - Returns: True if date is today
    func isToday() -> Bool {
        return Calendar.current.isDateInToday(self)
    }
    
    /// Get next occurrence of this time
    /// - Returns: Next occurrence of this time, or today if time hasn't passed
    func nextOccurrence() -> Date {
        let calendar = Calendar.current
        let now = Date()
        let today = calendar.startOfDay(for: now)
        let timeComponents = self.timeComponents()
        
        guard let todayWithTime = calendar.date(byAdding: timeComponents, to: today) else {
            return now
        }
        
        if todayWithTime > now {
            return todayWithTime
        } else {
            // Time has passed today, return tomorrow
            guard let tomorrow = calendar.date(byAdding: .day, value: 1, to: today),
                  let tomorrowWithTime = calendar.date(byAdding: timeComponents, to: tomorrow) else {
                return now
            }
            return tomorrowWithTime
        }
    }
}
