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(Permissions::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Permissions::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Permissions::TenantId).uuid().not_null())
                    .col(ColumnDef::new(Permissions::Code).string().not_null())
                    .col(ColumnDef::new(Permissions::Name).string().not_null())
                    .col(ColumnDef::new(Permissions::Resource).string().not_null())
                    .col(ColumnDef::new(Permissions::Action).string().not_null())
                    .col(ColumnDef::new(Permissions::Description).text().null())
                    .col(
                        ColumnDef::new(Permissions::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(Permissions::UpdatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(ColumnDef::new(Permissions::CreatedBy).uuid().not_null())
                    .col(ColumnDef::new(Permissions::UpdatedBy).uuid().not_null())
                    .col(
                        ColumnDef::new(Permissions::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(Permissions::Version)
                            .integer()
                            .not_null()
                            .default(1),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_permissions_tenant_id")
                    .table(Permissions::Table)
                    .col(Permissions::TenantId)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_permissions_tenant_code")
                    .table(Permissions::Table)
                    .col(Permissions::TenantId)
                    .col(Permissions::Code)
                    .unique()
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

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

#[derive(DeriveIden)]
enum Permissions {
    Table,
    Id,
    TenantId,
    Code,
    Name,
    Resource,
    Action,
    Description,
    CreatedAt,
    UpdatedAt,
    CreatedBy,
    UpdatedBy,
    DeletedAt,
    Version,
}
