import Foundation

public extension Date {
    /*
        Short time stamp for time ago string
        With suffix: "1y ago", "1mo ago", "1d ago", "1h ago", "1m ago", "Now"
        Without suffix: "1y", "1mo", "1d", "1h", "1m", "now"
     */
    func timeAgoDisplay(includeSuffix: Bool = true) -> String {
        let calendar = Calendar.current
        let now = Date()
        let components = calendar.dateComponents([.second, .minute, .hour, .day, .month, .year], from: self, to: now)

        let suffix = includeSuffix ? " ago" : ""
        
        if let year = components.year, year > 0 {
            return "\(year)y\(suffix)"
        } else if let month = components.month, month > 0 {
            return "\(month)mo\(suffix)"
        } else if let day = components.day, day > 0 {
            return "\(day)d\(suffix)"
        } else if let hour = components.hour, hour > 0 {
            return "\(hour)h\(suffix)"
        } else if let minute = components.minute, minute > 0 {
            return "\(minute)m\(suffix)"
        } else if let second = components.second, second >= 10 {
            return L10n.Utilities.nowTime
        } else {
            return L10n.Utilities.nowTime
        }
    }

    /*
        Long time stamp for time ago string
        1 years ago
        1 month ago
        1 day ago
        1 hour ago
        1 minute ago
        Now
     */
    func longTimeAgoDisplay() -> String {
        let calendar = Calendar.current
        let now = Date()
        let components = calendar.dateComponents([.second, .minute, .hour, .day, .month, .year], from: self, to: now)

        if let year = components.year, year > 0 {
            return L10n.Utilities.yearsAgo(year)
        } else if let month = components.month, month > 0 {
            return L10n.Utilities.monthsAgo(month)
        } else if let day = components.day, day > 0 {
            return L10n.Utilities.daysAgo(day)
        } else if let hour = components.hour, hour > 0 {
            return L10n.Utilities.hoursAgo(hour)
        } else if let minute = components.minute, minute > 0 {
            return L10n.Utilities.minutesAgo(minute)
        } else if let second = components.second, second >= 10 {
            return L10n.Utilities.nowTime
        } else {
            return L10n.Utilities.nowTime
        }
    }

    /*
        Custom time formatting for the `HookCard` component
        Less than 1 min: "just now"
        1 min to 7 days: "2 min ago", "3 hours ago", "1 day ago"
        More than 7 days: "June 5" (same year) or "June 5, 2024" (different year)
     */
    func customTimeAgoDisplay() -> String {
        let now = Date()
        let timeInterval = now.timeIntervalSince(self)

        // Less than 1 minute ago
        if timeInterval < 60 {
            return L10n.Utilities.justNowTime
        }

        // 1 minute to 7 days: Use iOS relative date formatter
        let sevenDaysInSeconds: TimeInterval = 7 * 24 * 60 * 60
        if timeInterval < sevenDaysInSeconds {
            let formatter = RelativeDateTimeFormatter()
            formatter.dateTimeStyle = .named
            formatter.formattingContext = .standalone
            return formatter.localizedString(for: self, relativeTo: now)
        }

        // More than 7 days: Use absolute date formatting
        let calendar = Calendar.current
        let currentYear = calendar.component(.year, from: now)
        let dateYear = calendar.component(.year, from: self)

        let formatter = DateFormatter()
        formatter.locale = Locale.current

        if currentYear == dateYear {
            // Same year: "June 5"
            formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMMd", options: 0, locale: Locale.current)
        } else {
            // Different year: "June 5, 2024"
            formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMMdyyyy", options: 0, locale: Locale.current)
        }

        return formatter.string(from: self)
    }
}
