package com.inou.clawdnode

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import com.inou.clawdnode.security.AuditLog
import com.inou.clawdnode.security.DeviceIdentity
import com.inou.clawdnode.security.TokenStore

/**
 * ClawdNode Application
 * 
 * AI-powered phone assistant that connects to Clawdbot Gateway.
 * Enables Claude to answer calls, screen notifications, and act on your behalf.
 */
class ClawdNodeApp : Application() {
    
    lateinit var tokenStore: TokenStore
        private set
    
    lateinit var auditLog: AuditLog
        private set
    
    override fun onCreate() {
        super.onCreate()
        instance = this
        
        // Initialize security components
        tokenStore = TokenStore(this)
        auditLog = AuditLog(this)
        
        // Create notification channels
        createNotificationChannels()
        
        auditLog.log("APP_START", "ClawdNode v0.1.0 started")
    }
    
    private fun createNotificationChannels() {
        val manager = getSystemService(NotificationManager::class.java)
        
        // Foreground service channel (persistent)
        val serviceChannel = NotificationChannel(
            CHANNEL_SERVICE,
            "Connection Status",
            NotificationManager.IMPORTANCE_LOW
        ).apply {
            description = "Shows ClawdNode connection status"
            setShowBadge(false)
        }
        
        // Alerts channel (for important events)
        val alertChannel = NotificationChannel(
            CHANNEL_ALERTS,
            "Alerts",
            NotificationManager.IMPORTANCE_HIGH
        ).apply {
            description = "Important events requiring attention"
        }
        
        // Call events channel
        val callChannel = NotificationChannel(
            CHANNEL_CALLS,
            "Call Activity",
            NotificationManager.IMPORTANCE_DEFAULT
        ).apply {
            description = "Call screening and voice interaction events"
        }
        
        manager.createNotificationChannels(listOf(serviceChannel, alertChannel, callChannel))
    }
    
    companion object {
        const val CHANNEL_SERVICE = "clawdnode_service"
        const val CHANNEL_ALERTS = "clawdnode_alerts"
        const val CHANNEL_CALLS = "clawdnode_calls"
        
        lateinit var instance: ClawdNodeApp
            private set
    }
}
