package dev.zxq5.chatapp.android

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import dev.zxq5.chatapp.android.core.data.TokenStore
import dev.zxq5.chatapp.android.data.repository.AuthRepository
import dev.zxq5.chatapp.android.data.repository.ChatRepository
import dev.zxq5.chatapp.android.data.repository.SettingsRepository

class ChatApplication : Application() {

    object AppState {
        var isInForeground = false
    }

    val tokenStore by lazy { TokenStore(this) }
    val authRepository by lazy { AuthRepository(tokenStore) }
    val chatRepository by lazy { ChatRepository(tokenStore) }
    val settingsRepository by lazy { SettingsRepository(tokenStore) }

    override fun onCreate() {
        super.onCreate()
        createNotificationChannels()
    }

    private fun createNotificationChannels() {
        val messageChannel = NotificationChannel(
            "messages",
            "Messages",
            NotificationManager.IMPORTANCE_HIGH
        ).apply {
            description = "New message notifications"
            enableVibration(true)
        }

        // add this — required for the foreground service persistent notification
        val serviceChannel = NotificationChannel(
            "service",
            "Background connection",
            NotificationManager.IMPORTANCE_LOW
        ).apply {
            description = "Keeps messages running in background"
        }

        val manager = getSystemService(NotificationManager::class.java)
        manager.createNotificationChannel(messageChannel)
        manager.createNotificationChannel(serviceChannel)
    }
}
