package com.nexlab.openclaw.node.domain.model

/**
 * Represents a permission action that can be requested
 */
sealed class PermissionAction {
    abstract val id: String
    abstract val displayName: String
    abstract val description: String
    
    // Categorize sensitivity level
    abstract val sensitivity: Sensitivity

    enum class Sensitivity {
        LOW,      // No confirmation needed
        MEDIUM,   // Confirm once, remember option
        HIGH      // Always confirm
    }

    // Built-in actions
    data class Screenshot(override val id: String = "screenshot") : PermissionAction() {
        override val displayName = "Screenshot"
        override val description = "Capture screen"
        override val sensitivity = Sensitivity.LOW
    }

    data class Touch(override val id: String = "touch") : PermissionAction() {
        override val displayName = "Touch Control"
        override val description = "Tap, swipe, gestures"
        override val sensitivity = Sensitivity.LOW
    }

    data class LaunchApp(override val id: String = "launch_app") : PermissionAction() {
        override val displayName = "Launch Apps"
        override val description = "Open applications"
        override val sensitivity = Sensitivity.LOW
    }

    data class Shell(override val id: String = "shell") : PermissionAction() {
        override val displayName = "Shell Commands"
        override val description = "Execute shell commands"
        override val sensitivity = Sensitivity.LOW
    }

    data class Notifications(override val id: String = "notifications") : PermissionAction() {
        override val displayName = "Notifications"
        override val description = "Read notifications"
        override val sensitivity = Sensitivity.MEDIUM
    }

    data class Camera(override val id: String = "camera") : PermissionAction() {
        override val displayName = "Camera"
        override val description = "Take photos, record video"
        override val sensitivity = Sensitivity.HIGH
    }

    data class Microphone(override val id: String = "microphone") : PermissionAction() {
        override val displayName = "Microphone"
        override val description = "Record audio"
        override val sensitivity = Sensitivity.HIGH
    }

    data class AudioPlayback(override val id: String = "audio_playback") : PermissionAction() {
        override val displayName = "Audio Playback"
        override val description = "Play audio files"
        override val sensitivity = Sensitivity.MEDIUM
    }

    data class Clipboard(override val id: String = "clipboard") : PermissionAction() {
        override val displayName = "Clipboard"
        override val description = "Read/write clipboard"
        override val sensitivity = Sensitivity.MEDIUM
    }

    data class Location(override val id: String = "location") : PermissionAction() {
        override val displayName = "Location"
        override val description = "GPS location"
        override val sensitivity = Sensitivity.HIGH
    }

    data class Contacts(override val id: String = "contacts") : PermissionAction() {
        override val displayName = "Contacts"
        override val description = "Read contacts"
        override val sensitivity = Sensitivity.HIGH
    }

    data class Storage(override val id: String = "storage") : PermissionAction() {
        override val displayName = "Storage"
        override val description = "Read/write files"
        override val sensitivity = Sensitivity.MEDIUM
    }

    data class Phone(override val id: String = "phone") : PermissionAction() {
        override val displayName = "Phone"
        override val description = "Make calls, read phone state"
        override val sensitivity = Sensitivity.HIGH
    }

    data class SMS(override val id: String = "sms") : PermissionAction() {
        override val displayName = "SMS"
        override val description = "Read/send SMS"
        override val sensitivity = Sensitivity.HIGH
    }

    companion object {
        fun fromId(id: String): PermissionAction? = when (id) {
            "screenshot" -> Screenshot()
            "touch" -> Touch()
            "launch_app" -> LaunchApp()
            "shell" -> Shell()
            "notifications" -> Notifications()
            "camera" -> Camera()
            "microphone" -> Microphone()
            "audio_playback" -> AudioPlayback()
            "clipboard" -> Clipboard()
            "location" -> Location()
            "contacts" -> Contacts()
            "storage" -> Storage()
            "phone" -> Phone()
            "sms" -> SMS()
            else -> null
        }

        fun all(): List<PermissionAction> = listOf(
            Screenshot(),
            Touch(),
            LaunchApp(),
            Shell(),
            Notifications(),
            Camera(),
            Microphone(),
            AudioPlayback(),
            Clipboard(),
            Location(),
            Contacts(),
            Storage(),
            Phone(),
            SMS()
        )
    }
}

/**
 * User's decision on a permission
 */
data class PermissionDecision(
    val actionId: String,
    val allowed: Boolean,
    val rememberChoice: Boolean = false,  // Save this decision permanently
    val timestamp: Long = System.currentTimeMillis()
)

/**
 * Result of a permission check
 */
sealed class PermissionResult {
    object Allowed : PermissionResult()
    object Denied : PermissionResult()
    object PendingConfirmation : PermissionResult()
    data class NeedsSystemPermission(val action: PermissionAction) : PermissionResult()
}
