use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(ReminderTask::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(ReminderTask::Id)
                            .uuid()
                            .primary_key()
                            .not_null(),
                    )
                    .col(ColumnDef::new(ReminderTask::CreatorId).uuid().not_null())
                    .col(ColumnDef::new(ReminderTask::Title).string().not_null())
                    .col(ColumnDef::new(ReminderTask::Description).string().null())
                    .col(ColumnDef::new(ReminderTask::DueAt).timestamp().not_null())
                    .col(ColumnDef::new(ReminderTask::RecurrenceRuleId).uuid().null())
                    .col(
                        ColumnDef::new(ReminderTask::CreatedAt)
                            .timestamp()
                            .not_null()
                            .extra("DEFAULT NOW()"),
                    )
                    .col(
                        ColumnDef::new(ReminderTask::UpdatedAt)
                            .timestamp()
                            .not_null(),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_reminder_task_creator")
                            .from(ReminderTask::Table, ReminderTask::CreatorId)
                            .to(User::Table, User::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_reminder_task_recurrence_rule")
                            .from(ReminderTask::Table, ReminderTask::RecurrenceRuleId)
                            .to(RecurrenceRule::Table, RecurrenceRule::Id)
                            .on_delete(ForeignKeyAction::SetNull)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        // Create indexes
        manager
            .create_index(
                Index::create()
                    .name("IDX_reminder_task_creator_due")
                    .table(ReminderTask::Table)
                    .col(ReminderTask::CreatorId)
                    .col(ReminderTask::DueAt)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("IDX_reminder_task_recurrence_rule_id")
                    .table(ReminderTask::Table)
                    .col(ReminderTask::RecurrenceRuleId)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(ReminderTask::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum ReminderTask {
    Table,
    Id,
    CreatorId,
    Title,
    Description,
    DueAt,
    RecurrenceRuleId,
    CreatedAt,
    UpdatedAt,
}

#[derive(DeriveIden)]
enum User {
    Table,
    Id,
}

#[derive(DeriveIden)]
enum RecurrenceRule {
    Table,
    Id,
}
