/**
 * PermissionStatus.kt
 * 
 * Data model for permission status information
 * Single source of truth for permission state across plugin and services
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

package org.timesafari.dailynotification

/**
 * Comprehensive permission status model
 * 
 * Represents the complete permission state for notification functionality
 * Used by both plugin and PermissionManager to ensure consistency
 */
data class PermissionStatus(
    /**
     * POST_NOTIFICATIONS permission granted (Android 13+)
     * Always true for Android < 13
     */
    val postNotificationsGranted: Boolean,
    
    /**
     * SCHEDULE_EXACT_ALARM permission granted (Android 12+)
     * Always true for Android < 12
     */
    val exactAlarmGranted: Boolean,
    
    /**
     * Battery optimizations ignored (exempted)
     * False if app is subject to battery optimization restrictions
     */
    val batteryOptimizationsIgnored: Boolean,
    
    /**
     * Notifications enabled at OS level
     * Checks NotificationManagerCompat.areNotificationsEnabled()
     * Important for pre-Android 13 where users can disable at OS level
     */
    val notificationsEnabledAtOsLevel: Boolean,
    
    /**
     * Android API level
     * Used for conditional logic based on OS version
     */
    val apiLevel: Int
) {
    /**
     * Overall readiness to schedule notifications
     * True if all required permissions are granted and notifications are enabled
     */
    val canScheduleNow: Boolean
        get() = postNotificationsGranted && 
                exactAlarmGranted && 
                notificationsEnabledAtOsLevel
    
    /**
     * Convert to JSObject for Capacitor response
     */
    fun toJSObject(): com.getcapacitor.JSObject {
        return com.getcapacitor.JSObject().apply {
            put("postNotificationsGranted", postNotificationsGranted)
            put("exactAlarmGranted", exactAlarmGranted)
            put("batteryOptimizationsIgnored", batteryOptimizationsIgnored)
            put("notificationsEnabledAtOsLevel", notificationsEnabledAtOsLevel)
            put("apiLevel", apiLevel)
            put("canScheduleNow", canScheduleNow)
        }
    }
}

/**
 * Pending permission request tracking
 * 
 * Tracks an in-flight permission request to prevent wrong-call resolution
 */
data class PendingPermissionRequest(
    /**
     * Unique identifier for this request
     * Used to match resume events with the correct request
     */
    val requestNonce: String,
    
    /**
     * Type of permission being requested
     */
    val requestType: PermissionRequestType,
    
    /**
     * Timestamp when request was initiated
     * Used to expire stale requests
     */
    val requestedAtMs: Long,
    
    /**
     * Plugin call reference (stored separately, not in data class)
     * Note: This is stored in plugin's savedCall, nonce is used to verify match
     */
    // call: PluginCall - stored separately in plugin
)

/**
 * Types of permission requests
 */
enum class PermissionRequestType {
    POST_NOTIFICATIONS,
    EXACT_ALARM,
    BATTERY_OPTIMIZATION
}

