package com.rugvedsarika.shoplist.data.local

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.rugvedsarika.shoplist.data.local.dao.ShoppingItemDao
import com.rugvedsarika.shoplist.data.local.dao.ShoppingListDao
import com.rugvedsarika.shoplist.data.local.entities.ShoppingItem
import com.rugvedsarika.shoplist.data.local.entities.ShoppingList

@Database(
    entities = [ShoppingList::class, ShoppingItem::class],
    version = 1,
    exportSchema = false
)
abstract class ShopListDatabase : RoomDatabase() {

    abstract fun shoppingListDao(): ShoppingListDao
    abstract fun shoppingItemDao(): ShoppingItemDao

    companion object {
        @Volatile
        private var INSTANCE: ShopListDatabase? = null

        fun getDatabase(context: Context): ShopListDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    ShopListDatabase::class.java,
                    "shop_list_database"
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}
