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(ProcessInstances::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(ProcessInstances::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(ProcessInstances::TenantId).uuid().not_null())
                    .col(
                        ColumnDef::new(ProcessInstances::DefinitionId)
                            .uuid()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::BusinessKey)
                            .string()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::Status)
                            .string()
                            .not_null()
                            .default("running"),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::StartedBy)
                            .uuid()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::StartedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::CompletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::UpdatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::CreatedBy)
                            .uuid()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::UpdatedBy)
                            .uuid()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(ProcessInstances::Version)
                            .integer()
                            .not_null()
                            .default(1),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_instances_tenant_status")
                    .table(ProcessInstances::Table)
                    .col(ProcessInstances::TenantId)
                    .col(ProcessInstances::Status)
                    .to_owned(),
            )
            .await?;

        manager
            .create_foreign_key(
                ForeignKey::create()
                    .name("fk_instances_definition")
                    .from(ProcessInstances::Table, ProcessInstances::DefinitionId)
                    .to(ProcessDefinitions::Table, ProcessDefinitions::Id)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

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

#[derive(DeriveIden)]
enum ProcessInstances {
    Table,
    Id,
    TenantId,
    DefinitionId,
    BusinessKey,
    Status,
    StartedBy,
    StartedAt,
    CompletedAt,
    CreatedAt,
    UpdatedAt,
    CreatedBy,
    UpdatedBy,
    DeletedAt,
    Version,
}

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