package com.framework.mvvm.model.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.framework.mvvm.model.db.converter.JsonConverter
import com.framework.mvvm.model.db.dao.*
import com.framework.mvvm.model.db.entity.*

@Database(
    entities = [
        MajorEntity::class,
        SystemEntity::class,
        RelationDictEntity::class,
        EquipmentEntity::class,
        InfosConfigEntity::class,
        PipeConfigEntity::class,
        RelConfigEntity::class,
        M2dEquipEntity::class,
        ConfigEntity::class,
        FileEntity::class,
        SpaceEntity::class,
        QrCodeEntity::class,
        ObjectEntity::class,
        ProjectEntity::class,
        RelationEntity::class,
        ProblemArchEntity::class,
        ProblemEquipEntity::class,
        ComponentEntity::class,
        PipeEntity::class
    ], version = 1, exportSchema = true
)
@TypeConverters(JsonConverter::class)
abstract class AdmDatabase : RoomDatabase() {
    abstract fun configDao(): ConfigDao
    abstract fun equipmentDao(): EquipmentDao
    abstract fun fileDao(): FileDao
    abstract fun infosConfigDao(): InfosConfigDao
    abstract fun m2dEquipDao(): M2dEquipDao
    abstract fun majorDao(): MajorDao
    abstract fun objectDao(): ObjectDao
    abstract fun pipeConfigDao(): PipeConfigDao
    abstract fun problemArchDao(): ProblemArchDao
    abstract fun problemEquipDao(): ProblemEquipDao
    abstract fun projectDao(): ProjectDao
    abstract fun qrCodeDao(): QrCodeDao
    abstract fun relationDao(): RelationDao
    abstract fun relationDictDao(): RelationDictDao
    abstract fun relConfigDao(): RelConfigDao
    abstract fun spaceDao(): SpaceDao
    abstract fun systemDao(): SystemDao
    abstract fun componentDao(): ComponentDao
    abstract fun pipeDao(): PipeDao

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

        fun getDatabase(context: Context): AdmDatabase {
            return instance ?: synchronized(this) {
                instance ?: Room.databaseBuilder(
                    context,
                    AdmDatabase::class.java,
                    "adm_db"
                ).build().also { instance = it }
            }
        }
    }
}