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

import android.content.Context
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.isidroid.job.data.source.local.JobDao
import com.isidroid.job.model.PrintJob
import com.isidroid.job_sender.data.source.local.SendJobDao
import com.isidroid.job_sender.domain.model.PrintJobSender
import com.isidroid.session.data.source.local.SessionDao
import com.isidroid.session.model.Session
import com.isidroid.spot.data.source.local.dao.PrintProfileDao
import com.isidroid.spot.data.source.local.dao.SpotDao
import com.isidroid.spot.model.PrintProfile
import com.isidroid.spot.model.Spot

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

    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
            }
        }
    }
}