package com.manalejandro.odb2bluetooth.data.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.manalejandro.odb2bluetooth.data.database.dao.DtcCodeDao
import com.manalejandro.odb2bluetooth.data.database.dao.ObdSignalDao
import com.manalejandro.odb2bluetooth.data.database.dao.VehicleDao
import com.manalejandro.odb2bluetooth.data.database.entities.Brand
import com.manalejandro.odb2bluetooth.data.database.entities.DtcCode
import com.manalejandro.odb2bluetooth.data.database.entities.Generation
import com.manalejandro.odb2bluetooth.data.database.entities.Model
import com.manalejandro.odb2bluetooth.data.database.entities.ObdSignal

/**
 * Main Room database for the OBD2 application
 */
@Database(
    entities = [
        Brand::class,
        Model::class,
        Generation::class,
        ObdSignal::class,
        DtcCode::class
    ],
    version = 1,
    exportSchema = false
)
abstract class ObdDatabase : RoomDatabase() {

    abstract fun vehicleDao(): VehicleDao
    abstract fun obdSignalDao(): ObdSignalDao
    abstract fun dtcCodeDao(): DtcCodeDao

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

        fun getDatabase(context: Context): ObdDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    ObdDatabase::class.java,
                    "obd_database"
                )
                    .createFromAsset("obd_database.db")
                    .fallbackToDestructiveMigration()
                    .build()
                INSTANCE = instance
                instance
            }
        }
    }
}

