package com.example.bnsptest.data.local.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.bnsptest.data.local.dao.CartDao
import com.example.bnsptest.data.local.dao.ProductDao
import com.example.bnsptest.data.local.dao.UserDao
import com.example.bnsptest.data.local.dao.Product
import com.example.bnsptest.data.local.dao.CartItem
import com.example.bnsptest.data.local.entity.User

@Database(entities = [Product::class, User::class, CartItem::class], version = 4, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun productDao(): ProductDao
    abstract fun cartDao(): CartDao
    abstract fun userDao(): UserDao

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

        fun getInstance(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "app_database"
                )
                .fallbackToDestructiveMigration()
                .build()
                INSTANCE = instance
                instance
            }
        }
    }
}
