package com.isidroid.c23.data.source.local

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.isidroid.spot.data.source.local.dao.PrintProfileDao
import com.isidroid.spot.model.PrintProfile
import com.isidroid.session.data.source.local.SessionDao
import com.isidroid.session.model.Session
import com.isidroid.spot.model.Spot
import com.isidroid.spot.data.source.local.dao.SpotDao

@Database(
    version = 1,
    entities = [
        Session::class,
        PrintProfile::class,
        Spot::class
    ],
    exportSchema = true,
    autoMigrations = [
//        AutoMigration(from = 1, to = 2),
    ]
)
@TypeConverters(RoomConverters::class)
abstract class AppDataBase : RoomDatabase() {
    abstract val sessionDao: SessionDao
    abstract val printProfileDao: PrintProfileDao
    abstract val spotDao: SpotDao

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

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