import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.apps.quicksketchtrace.database.Local
import com.apps.quicksketchtrace.database.LocalDao
import com.apps.quicksketchtrace.database.Upload
import com.apps.quicksketchtrace.database.UploadDao


@Database(entities = [Local::class, Upload::class], version = 2, exportSchema = false)
abstract class AppDataBase : RoomDatabase() {
    abstract fun localDao(): LocalDao
    abstract fun uploadDao(): UploadDao

    companion object {
        @Volatile
        private var INSTANCE: AppDataBase? = null
        fun getDatabase(context: Context): AppDataBase {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: createDatabase(context).also { INSTANCE = it }
            }
        }

        private fun createDatabase(context: Context): AppDataBase {
            return Room.databaseBuilder(
                context.applicationContext,
                AppDataBase::class.java,
                "trace_database"
            ).fallbackToDestructiveMigration().build()
        }
    }
}
