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

import Foundation
import Observation

/// ViewModel for alarm management
@Observable
class AlarmViewModel {
    
    // MARK: - Properties
    private let alarmService: AlarmService
    private let notificationService: NotificationService
    
    var alarms: [Alarm] {
        alarmService.alarms
    }
    
    var systemSounds: [String] {
        AppConstants.SystemSounds.availableSounds
    }
    
    // MARK: - Initialization
    init(alarmService: AlarmService = AlarmService(), 
         notificationService: NotificationService = NotificationService()) {
        self.alarmService = alarmService
        self.notificationService = notificationService
        
        // Register alarm service with notification delegate for snooze handling
        NotificationDelegate.shared.setAlarmService(alarmService)
    }
    
    // MARK: - Public Interface
    func addAlarm(_ alarm: Alarm) async {
        alarmService.addAlarm(alarm)
        
        // Schedule notification if alarm is enabled
        if alarm.isEnabled {
            await notificationService.scheduleAlarmNotification(
                id: alarm.id.uuidString,
                title: alarm.label,
                body: alarm.notificationMessage,
                soundName: alarm.soundName,
                date: alarm.time
            )
        }
    }
    
    func updateAlarm(_ alarm: Alarm) async {
        alarmService.updateAlarm(alarm)
        
        // Reschedule notification
        if alarm.isEnabled {
            await notificationService.scheduleAlarmNotification(
                id: alarm.id.uuidString,
                title: alarm.label,
                body: alarm.notificationMessage,
                soundName: alarm.soundName,
                date: alarm.time
            )
        } else {
            notificationService.cancelNotification(id: alarm.id.uuidString)
        }
    }
    
    func deleteAlarm(id: UUID) async {
        // Cancel notification first
        notificationService.cancelNotification(id: id.uuidString)
        
        // Then delete from storage
        alarmService.deleteAlarm(id: id)
    }
    
    func toggleAlarm(id: UUID) async {
        guard var alarm = alarmService.getAlarm(id: id) else { return }
        
        alarm.isEnabled.toggle()
        alarmService.updateAlarm(alarm)
        
        // Schedule or cancel notification based on new state
        if alarm.isEnabled {
            await notificationService.scheduleAlarmNotification(
                id: alarm.id.uuidString,
                title: alarm.label,
                body: alarm.notificationMessage,
                soundName: alarm.soundName,
                date: alarm.time
            )
        } else {
            notificationService.cancelNotification(id: id.uuidString)
        }
    }
    
    func getAlarm(id: UUID) -> Alarm? {
        return alarmService.getAlarm(id: id)
    }
    
    func createNewAlarm(
        time: Date,
        soundName: String = AppConstants.SystemSounds.defaultSound,
        label: String = "Alarm",
        notificationMessage: String = "Your alarm is ringing",
        snoozeDuration: Int = 9,
        isVibrationEnabled: Bool = true,
        isLightFlashEnabled: Bool = false,
        volume: Float = 1.0
    ) -> Alarm {
        return Alarm(
            id: UUID(),
            time: time,
            isEnabled: true,
            soundName: soundName,
            label: label,
            notificationMessage: notificationMessage,
            snoozeDuration: snoozeDuration,
            isVibrationEnabled: isVibrationEnabled,
            isLightFlashEnabled: isLightFlashEnabled,
            volume: volume
        )
    }
    
    func requestNotificationPermissions() async -> Bool {
        return await notificationService.requestPermissions()
    }
}
