package com.example.timert.com.example.timert.data.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.timert.com.example.timert.data.dao.WorkoutDao
import com.example.timert.com.example.timert.data.entity.Workout
import com.example.timert.data.dao.ExerciseDao
import com.example.timert.data.dao.TrainingDao
import com.example.timert.data.entity.Exercise
import com.example.timert.data.entity.Training
import kotlinx.coroutines.CoroutineScope

@Database(entities = [Workout::class, Exercise::class, Training::class],   version = 3, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun workoutDao(): WorkoutDao
    abstract fun trainingDao(): TrainingDao
    abstract fun exerciseDao(): ExerciseDao

    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null
        fun getDatabase(context: Context): AppDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "workout_database"
                )
                    //.fallbackToDestructiveMigration()
                    .build()
                INSTANCE = instance
                return instance
            }

        }
    }
}
