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(Todo::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Todo::Id).uuid().primary_key().not_null())
                    .col(ColumnDef::new(Todo::OwnerId).uuid().not_null())
                    .col(ColumnDef::new(Todo::Title).string().not_null())
                    .col(ColumnDef::new(Todo::Description).string().null())
                    .col(ColumnDef::new(Todo::DueAt).timestamp().not_null())
                    .col(ColumnDef::new(Todo::RecurrenceRuleId).uuid().null())
                    .col(
                        ColumnDef::new(Todo::CreatedAt)
                            .timestamp()
                            .not_null()
                            .extra("DEFAULT NOW()"),
                    )
                    .col(ColumnDef::new(Todo::UpdatedAt).timestamp().not_null())
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_todo_owner")
                            .from(Todo::Table, Todo::OwnerId)
                            .to(User::Table, User::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_todo_recurrence_rule")
                            .from(Todo::Table, Todo::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_todo_owner_due")
                    .table(Todo::Table)
                    .col(Todo::OwnerId)
                    .col(Todo::DueAt)
                    .to_owned(),
            )
            .await?;

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

        Ok(())
    }

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

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

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

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