/**
 * DailyNotificationMaintenanceWorker.swift
 * Daily Notification Plugin for Capacitor
 *
 * Handles background maintenance tasks for notifications
 */

import Foundation
import UserNotifications

/// Handles background maintenance tasks for the notification plugin
public class DailyNotificationMaintenanceWorker {
    /// Shared instance for singleton access
    public static let shared = DailyNotificationMaintenanceWorker()
    
    private let notificationCenter = UNUserNotificationCenter.current()
    private let powerManager = DailyNotificationPowerManager.shared
    
    private init() {}
    
    /// Performs maintenance tasks
    public func performMaintenance() {
        DailyNotificationLogger.shared.log(.info, "Starting maintenance tasks")
        
        // Update battery status
        _ = powerManager.getBatteryStatus()
        
        // Clean up old notifications
        cleanupOldNotifications()
        
        // Reschedule missed notifications
        rescheduleMissedNotifications()
        
        DailyNotificationLogger.shared.log(.info, "Maintenance tasks completed")
    }
    
    private func cleanupOldNotifications() {
        let cutoffDate = Date().addingTimeInterval(-Double(DailyNotificationConfig.shared.retentionDays * 24 * 60 * 60))
        
        notificationCenter.getDeliveredNotifications { notifications in
            let oldNotifications = notifications.filter { $0.date < cutoffDate }
            
            if !oldNotifications.isEmpty {
                let identifiers = oldNotifications.map { $0.request.identifier }
                self.notificationCenter.removeDeliveredNotifications(withIdentifiers: identifiers)
                
                DailyNotificationLogger.shared.log(
                    .info,
                    "Cleaned up \(identifiers.count) old notifications"
                )
            }
        }
    }
    
    private func rescheduleMissedNotifications() {
        notificationCenter.getPendingNotificationRequests { requests in
            let now = Date()
            
            for request in requests {
                guard let trigger = request.trigger as? UNCalendarNotificationTrigger,
                      let nextTriggerDate = trigger.nextTriggerDate() else {
                    continue
                }
                
                // If the next trigger date is more than 24 hours in the past
                if nextTriggerDate.timeIntervalSince(now) < -24 * 60 * 60 {
                    // Reschedule the notification
                    let content = request.content.mutableCopy() as! UNMutableNotificationContent
                    let newRequest = UNNotificationRequest(
                        identifier: request.identifier,
                        content: content,
                        trigger: trigger
                    )
                    
                    self.notificationCenter.add(newRequest) { error in
                        if let error = error {
                            DailyNotificationLogger.shared.log(
                                .error,
                                "Failed to reschedule notification: \(error.localizedDescription)"
                            )
                        } else {
                            DailyNotificationLogger.shared.log(
                                .info,
                                "Successfully rescheduled notification: \(request.identifier)"
                            )
                        }
                    }
                }
            }
        }
    }
    
    /// Schedules the next maintenance window
    public func scheduleNextMaintenance() {
        let trigger = UNTimeIntervalNotificationTrigger(
            timeInterval: DailyNotificationConfig.SchedulingIntervals.normal,
            repeats: true
        )
        
        let content = UNMutableNotificationContent()
        content.title = "Maintenance"
        content.body = "Performing notification maintenance"
        content.sound = nil
        
        let request = UNNotificationRequest(
            identifier: "maintenance-window",
            content: content,
            trigger: trigger
        )
        
        notificationCenter.add(request) { error in
            if let error = error {
                DailyNotificationLogger.shared.log(
                    .error,
                    "Failed to schedule maintenance window: \(error.localizedDescription)"
                )
            } else {
                DailyNotificationLogger.shared.log(.info, "Maintenance window scheduled")
            }
        }
    }
} 