package app.voltplan.cable.data.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import app.voltplan.cable.data.model.ElectricalSystem
import app.voltplan.cable.data.model.SavedBattery
import app.voltplan.cable.data.model.SavedCharger
import app.voltplan.cable.data.model.SavedLoad

@Database(
    entities = [
        ElectricalSystem::class,
        SavedLoad::class,
        SavedBattery::class,
        SavedCharger::class,
    ],
    version = 1,
    exportSchema = false,
)
@TypeConverters(Converters::class)
abstract class CableDatabase : RoomDatabase() {
    abstract fun systemDao(): SystemDao
    abstract fun loadDao(): LoadDao
    abstract fun batteryDao(): BatteryDao
    abstract fun chargerDao(): ChargerDao

    companion object {
        @Volatile
        private var instance: CableDatabase? = null

        fun get(context: Context): CableDatabase = instance ?: synchronized(this) {
            instance ?: Room.databaseBuilder(
                context.applicationContext,
                CableDatabase::class.java,
                "cable.db",
            ).fallbackToDestructiveMigration().build().also { instance = it }
        }
    }
}
