import AppIntents
import Combine
import Foundation

/// The Action Button path: one press starts a memo, the next press sends it.
///
/// WHY AN INTENT RATHER THAN "Open App". A shortcut that only opens Jarvis
/// leaves you looking at the mic button with a raised wrist, still needing a
/// tap — which is the tap the orange button was supposed to replace. An intent
/// carries the instruction in, so the press itself is the record.
///
/// It TOGGLES, matching the screen button exactly: watchOS gives no way to
/// hold the Action Button down for the duration of a thought, and a press that
/// could only ever start would make the second press begin a second recording
/// while the first was still running.

struct RecordMemoIntent: AppIntent {
    static var title: LocalizedStringResource = "Record a Memo"
    static var description = IntentDescription(
        "Starts recording a memo for Jarvis. Run it again to stop and send."
    )

    /// Required: recording needs the app's microphone session and its UI, so
    /// the intent cannot do its work in the background. Same modern spelling as
    /// the control's intent — `openAppWhenRun` is deprecated on watchOS 26.
    static var supportedModes: IntentModes = .foreground(.immediate)

    @MainActor
    func perform() async throws -> some IntentResult {
        LaunchRequest.shared.press()
        return .result()
    }
}

/// Surfaces the intent in the Shortcuts app without the user having to build
/// anything — it shows up under Jarvis ready to be put on the Action Button.
///
/// The phrase must contain `\(.applicationName)`; Siri rejects a phrase that
/// does not name the app. Free-form parameters are still not allowed here,
/// which is why there is no "tell Jarvis <something>" phrase — that limitation
/// is in App Intents, not in this app.
struct JarvisShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: RecordMemoIntent(),
            phrases: [
                "Record a memo in \(.applicationName)",
                "Start a memo in \(.applicationName)",
            ],
            shortTitle: "Record a Memo",
            systemImageName: "mic.fill"
        )
    }
}
