package com.diegocayo.organizame.data.local

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.diegocayo.organizame.data.local.dao.EventDao
import com.diegocayo.organizame.data.local.dao.SubjectDao
import com.diegocayo.organizame.data.local.dao.TaskDao
import com.diegocayo.organizame.data.local.entity.EventEntity
import com.diegocayo.organizame.data.local.entity.SubjectEntity
import com.diegocayo.organizame.data.local.entity.TaskEntity

@Database(
    entities = [
        SubjectEntity::class,
        TaskEntity::class,
        EventEntity::class
    ],
    version = 4,
    exportSchema = false
)
abstract class OrganizaMeDatabase : RoomDatabase() {
    abstract fun subjectDao(): SubjectDao
    abstract fun taskDao(): TaskDao
    abstract fun eventDao(): EventDao

    companion object {
        val MIGRATION_2_3 = object : Migration(2, 3) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL(
                    "ALTER TABLE tasks ADD COLUMN scheduledDate INTEGER NOT NULL DEFAULT ${System.currentTimeMillis()}"
                )
            }
        }

        val MIGRATION_3_4 = object : Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL(
                    """
                    CREATE TABLE events (
                        id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                        title TEXT NOT NULL,
                        description TEXT NOT NULL,
                        subjectId INTEGER,
                        date INTEGER NOT NULL,
                        startTime INTEGER NOT NULL,
                        endTime INTEGER NOT NULL,
                        createdAt INTEGER NOT NULL,
                        FOREIGN KEY(subjectId) REFERENCES subjects(id) ON DELETE SET NULL
                    )
                """
                )
            }
        }
    }
} 