package com.rugvedsarika.shoplist

import android.app.Application
import android.content.Context
import androidx.appcompat.app.AppCompatDelegate
import com.rugvedsarika.shoplist.data.local.ShopListDatabase
import com.rugvedsarika.shoplist.data.repository.ShopRepository
import com.rugvedsarika.shoplist.ui.settings.SettingsActivity

class ShopListApp : Application() {

    // Lazy initialization for database and repository
    private val database by lazy { ShopListDatabase.getDatabase(this) }
    val repository by lazy { ShopRepository(database.shoppingListDao(), database.shoppingItemDao()) }

    override fun onCreate() {
        super.onCreate()
        val sharedPreferences = getSharedPreferences(SettingsActivity.PREFS_NAME, Context.MODE_PRIVATE)
        val isDarkMode = sharedPreferences.getBoolean(SettingsActivity.KEY_DARK_MODE, false)
        AppCompatDelegate.setDefaultNightMode(
            if (isDarkMode) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO
        )
    }
}
