package com.syaroful.agrilinkvocpro.plant_disease_detection_feature

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.navigation.compose.rememberNavController
import com.syaroful.agrilinkvocpro.plant_disease_detection_feature.di.cameraModule
import com.syaroful.agrilinkvocpro.plant_disease_detection_feature.di.databaseModule
import com.syaroful.agrilinkvocpro.plant_disease_detection_feature.di.diagnosisModule
import com.syaroful.agrilinkvocpro.plant_disease_detection_feature.navigation.NavGraph
import com.syaroful.agrilinkvocpro.presentation.theme.AgrilinkVocproTheme
import org.koin.core.context.loadKoinModules
import org.koin.core.context.unloadKoinModules

class PlantDiseaseDetectionActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // load Module
        loadKoinModules(listOf(cameraModule, diagnosisModule, databaseModule))

        Log.d("KOIN_DEBUG", "✅ cameraModule loaded, yeay 🤗")

        enableEdgeToEdge()
        if (!hasRequiredPermissions()) {
            ActivityCompat.requestPermissions(this, CAMERAX_PERMISSIONS, 0)
        }
        setContent {
            AgrilinkVocproTheme {
                val navController = rememberNavController()
                NavGraph(navController)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unloadKoinModules(listOf(cameraModule, diagnosisModule, databaseModule))
        Log.d("KOIN_DEBUG", "🧹 cameraModule unloaded")
    }

    private fun hasRequiredPermissions(): Boolean {
        return CAMERAX_PERMISSIONS.all {
            ContextCompat.checkSelfPermission(
                applicationContext,
                it
            ) == PackageManager.PERMISSION_GRANTED
        }
    }

    companion object {
        private val CAMERAX_PERMISSIONS = arrayOf(
            Manifest.permission.CAMERA
        )
    }

}