package com.example.shiftalarm

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

@Database(
    entities = [ShiftOverride::class, DailyMemo::class, CustomAlarm::class, AnnualLeave::class],
    version = 4,
    exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
    abstract fun shiftDao(): ShiftDao

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

        // Migration from version 1 to 2
        private val MIGRATION_1_2 = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL(
                    """
                    CREATE TABLE IF NOT EXISTS custom_alarms (
                        id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                        time TEXT NOT NULL,
                        shiftType TEXT NOT NULL,
                        isEnabled INTEGER NOT NULL DEFAULT 1,
                        soundUri TEXT,
                        snoozeInterval INTEGER NOT NULL DEFAULT 5,
                        snoozeRepeat INTEGER NOT NULL DEFAULT 3
                    )
                    """.trimIndent()
                )
            }
        }

        // Migration from version 2 to 3
        private val MIGRATION_2_3 = object : Migration(2, 3) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL(
                    """
                    CREATE TABLE IF NOT EXISTS custom_alarms (
                        id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                        time TEXT NOT NULL,
                        shiftType TEXT NOT NULL,
                        isEnabled INTEGER NOT NULL DEFAULT 1,
                        soundUri TEXT,
                        snoozeInterval INTEGER NOT NULL DEFAULT 5,
                        snoozeRepeat INTEGER NOT NULL DEFAULT 3
                    )
                    """.trimIndent()
                )
            }
        }

        // Migration from version 3 to 4: Add AnnualLeave table
        private val MIGRATION_3_4 = object : Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL(
                    """
                    CREATE TABLE IF NOT EXISTS annual_leave (
                        id INTEGER PRIMARY KEY NOT NULL,
                        totalDays REAL NOT NULL,
                        remainingDays REAL NOT NULL,
                        updatedAt INTEGER NOT NULL
                    )
                    """.trimIndent()
                )
            }
        }

        fun getDatabase(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "shift_database"
                )
                .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)
                .fallbackToDestructiveMigration() // Prevent crash on schema mismatch or missing migration path
                .build()
                INSTANCE = instance
                instance
            }
        }
    }
}
