package com.syncflow.data.db

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.syncflow.data.db.entities.*

@Database(
    entities = [
        CloudAccountEntity::class,
        SyncPairEntity::class,
        SyncFileStateEntity::class,
        SyncConflictEntity::class,
        SyncEventEntity::class,
    ],
    version = 4,
    exportSchema = true,
)
@TypeConverters(DbConverters::class)
abstract class SyncDatabase : RoomDatabase() {

    companion object {
        val MIGRATION_2_3 = object : Migration(2, 3) {
            override fun migrate(db: SupportSQLiteDatabase) {
                db.execSQL("DELETE FROM sync_file_states")
            }
        }

        val MIGRATION_3_4 = object : Migration(3, 4) {
            override fun migrate(db: SupportSQLiteDatabase) {
                db.execSQL("ALTER TABLE sync_pairs ADD COLUMN lastSyncUploaded INTEGER NOT NULL DEFAULT 0")
                db.execSQL("ALTER TABLE sync_pairs ADD COLUMN lastSyncDownloaded INTEGER NOT NULL DEFAULT 0")
                db.execSQL("ALTER TABLE sync_pairs ADD COLUMN lastSyncDeleted INTEGER NOT NULL DEFAULT 0")
                db.execSQL("ALTER TABLE sync_pairs ADD COLUMN lastSyncBytesTransferred INTEGER NOT NULL DEFAULT 0")
            }
        }
    }
    abstract fun cloudAccountDao(): CloudAccountDao
    abstract fun syncPairDao(): SyncPairDao
    abstract fun syncFileStateDao(): SyncFileStateDao
    abstract fun syncConflictDao(): SyncConflictDao
    abstract fun syncEventDao(): SyncEventDao
}
