package pt.whatsms.gateway.data

import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import pt.whatsms.gateway.data.dao.MessagesDao
import pt.whatsms.gateway.data.dao.TokensDao
import pt.whatsms.gateway.data.entities.Message
import pt.whatsms.gateway.data.entities.MessageRecipient
import pt.whatsms.gateway.data.entities.MessageState
import pt.whatsms.gateway.data.entities.RecipientState
import pt.whatsms.gateway.data.entities.Token
import pt.whatsms.gateway.modules.incoming.db.IncomingMessage
import pt.whatsms.gateway.modules.incoming.db.IncomingMessagesDao
import pt.whatsms.gateway.modules.logs.db.LogEntriesDao
import pt.whatsms.gateway.modules.logs.db.LogEntry
import pt.whatsms.gateway.modules.webhooks.db.WebHook
import pt.whatsms.gateway.modules.webhooks.db.WebHooksDao
import pt.whatsms.gateway.modules.webhooks.db.WebhookQueueDao
import pt.whatsms.gateway.modules.webhooks.db.WebhookQueueEntity

@Database(
    entities = [
        Message::class,
        MessageRecipient::class,
        RecipientState::class,
        MessageState::class,
        WebHook::class,
        WebhookQueueEntity::class,
        LogEntry::class,
        Token::class,
        IncomingMessage::class,
    ],
    version = 20,
    autoMigrations = [
        AutoMigration(from = 1, to = 2),
        AutoMigration(from = 2, to = 3),
        AutoMigration(from = 3, to = 4),
        AutoMigration(from = 4, to = 5),
        AutoMigration(from = 5, to = 6),
        AutoMigration(from = 6, to = 7),
        // AutoMigration(from = 7, to = 8),  // manual migration
        AutoMigration(from = 8, to = 9),
        AutoMigration(from = 9, to = 10),
        AutoMigration(from = 10, to = 11),
        AutoMigration(from = 11, to = 12),
        AutoMigration(from = 12, to = 13),
        // AutoMigration(from = 13, to = 14),   // manual migration
        AutoMigration(from = 14, to = 15),
        AutoMigration(from = 15, to = 16),
        AutoMigration(from = 16, to = 17),
        AutoMigration(from = 17, to = 18),
        AutoMigration(from = 18, to = 19),
        AutoMigration(from = 19, to = 20),
    ]
)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun messagesDao(): MessagesDao
    abstract fun webhooksDao(): WebHooksDao
    abstract fun webhookQueueDao(): WebhookQueueDao
    abstract fun logDao(): LogEntriesDao
    abstract fun incomingMessagesDao(): IncomingMessagesDao
    abstract fun tokensDao(): TokensDao

    companion object {
        fun getDatabase(context: android.content.Context): AppDatabase {
            return Room.databaseBuilder(
                context,
                AppDatabase::class.java,
                "gateway"
            )
                .addMigrations(
                    MIGRATION_7_8,
                    MIGRATION_13_14,
                )
                .allowMainThreadQueries()
                .build()
        }
    }
}