//
//  NotificationUtils.swift
//  TheNoiseClock
//
//  Created by Matt Bruce on 9/7/25.
//

import UserNotifications
import Foundation
import Bedrock

/// Notification helper functions
enum NotificationUtils {
    
    /// Request notification permissions
    /// - Returns: True if permission granted
    static func requestPermissions() async -> Bool {
        do {
            let granted = try await UNUserNotificationCenter.current().requestAuthorization(
                options: [.alert, .sound, .badge]
            )
            return granted
        } catch {
            Design.debugLog("[general] Error requesting notification permissions: \(error)")
            return false
        }
    }
    
    /// Create notification content for alarm
    /// - Parameters:
    ///   - title: Notification title
    ///   - body: Notification body
    ///   - soundName: Sound name for notification
    /// - Returns: Configured notification content
    static func createAlarmContent(title: String, body: String, soundName: String) -> UNMutableNotificationContent {
        let content = UNMutableNotificationContent()
        content.title = title
        content.body = body
        
        if soundName == "default" {
            content.sound = UNNotificationSound.default
            Design.debugLog("[settings] Using default notification sound")
        } else {
            // Use the sound name directly since sounds.json now references CAF files
            content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: soundName))
            Design.debugLog("[settings] Using custom alarm sound: \(soundName)")
            Design.debugLog("[settings] Sound file should be in main bundle: \(soundName)")
        }
        
        return content
    }
    
    /// Create calendar trigger for alarm
    /// - Parameter date: Date for alarm
    /// - Returns: Calendar notification trigger
    static func createCalendarTrigger(for date: Date) -> UNCalendarNotificationTrigger {
        let components = Calendar.current.dateComponents([.hour, .minute], from: date)
        return UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
    }
    
    /// Schedule notification
    /// - Parameters:
    ///   - identifier: Unique identifier for notification
    ///   - content: Notification content
    ///   - trigger: Notification trigger
    /// - Returns: True if scheduled successfully
    static func scheduleNotification(
        identifier: String,
        content: UNMutableNotificationContent,
        trigger: UNNotificationTrigger
    ) async -> Bool {
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
        
        do {
            try await UNUserNotificationCenter.current().add(request)
            return true
        } catch {
            Design.debugLog("[general] Error scheduling notification: \(error)")
            return false
        }
    }
    
    /// Remove notification by identifier
    /// - Parameter identifier: Notification identifier to remove
    static func removeNotification(identifier: String) {
        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
    }
    
    /// Remove all pending notifications
    static func removeAllNotifications() {
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    }
}
