//
//  AlarmIntents.swift
//  TheNoiseClockWidget
//
//  Created by Matt Bruce on 2/2/26.
//
//  App Intents for alarm actions from Live Activity and widget buttons.
//  These intents are duplicated in the widget target for compilation.
//  Note: Must be kept in sync with TheNoiseClock/Features/Alarms/Intents/AlarmIntents.swift
//

import AlarmKit
import AppIntents
import Foundation

// MARK: - Stop Alarm Intent

/// Intent to stop an active alarm from the Live Activity or notification.
struct StopAlarmIntent: LiveActivityIntent {
    
    static var title: LocalizedStringResource = "Stop Alarm"
    static var description = IntentDescription("Stops the currently ringing alarm")
    
    @Parameter(title: "Alarm ID")
    var alarmId: String
    
    static var supportedModes: IntentModes { .background }
    
    init() {
        self.alarmId = ""
    }
    
    init(alarmId: String) {
        self.alarmId = alarmId
    }
    
    func perform() throws -> some IntentResult {
        guard let uuid = UUID(uuidString: alarmId) else {
            throw AlarmIntentError.invalidAlarmID
        }
        
        try AlarmManager.shared.stop(id: uuid)
        return .result()
    }
}

// MARK: - Snooze Alarm Intent

/// Intent to snooze an active alarm from the Live Activity or notification.
struct SnoozeAlarmIntent: LiveActivityIntent {
    
    static var title: LocalizedStringResource = "Snooze Alarm"
    static var description = IntentDescription("Snoozes the currently ringing alarm")
    
    @Parameter(title: "Alarm ID")
    var alarmId: String
    
    static var supportedModes: IntentModes { .background }
    
    init() {
        self.alarmId = ""
    }
    
    init(alarmId: String) {
        self.alarmId = alarmId
    }
    
    func perform() throws -> some IntentResult {
        guard let uuid = UUID(uuidString: alarmId) else {
            throw AlarmIntentError.invalidAlarmID
        }
        
        // Use countdown to postpone the alarm by its configured snooze duration
        try AlarmManager.shared.countdown(id: uuid)
        return .result()
    }
}

// MARK: - Open App Intent

/// Intent to open the app when the user taps the Live Activity.
struct OpenAlarmAppIntent: LiveActivityIntent {
    
    static var title: LocalizedStringResource = "Open TheNoiseClock"
    static var description = IntentDescription("Opens the app to the alarm screen")
    static var openAppWhenRun = true
    
    @Parameter(title: "Alarm ID")
    var alarmId: String
    
    init() {
        self.alarmId = ""
    }
    
    init(alarmId: String) {
        self.alarmId = alarmId
    }
    
    func perform() throws -> some IntentResult {
        // The app will be opened due to openAppWhenRun = true
        return .result()
    }
}

// MARK: - Errors

enum AlarmIntentError: Error, LocalizedError {
    case invalidAlarmID
    case alarmNotFound
    
    var errorDescription: String? {
        switch self {
        case .invalidAlarmID:
            return "Invalid alarm ID"
        case .alarmNotFound:
            return "Alarm not found"
        }
    }
}
