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(Departments::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Departments::Id)
                            .uuid()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Departments::TenantId).uuid().not_null())
                    .col(ColumnDef::new(Departments::OrgId).uuid().not_null())
                    .col(ColumnDef::new(Departments::Name).string().not_null())
                    .col(ColumnDef::new(Departments::Code).string().null())
                    .col(ColumnDef::new(Departments::ParentId).uuid().null())
                    .col(ColumnDef::new(Departments::ManagerId).uuid().null())
                    .col(ColumnDef::new(Departments::Path).string().null())
                    .col(
                        ColumnDef::new(Departments::SortOrder)
                            .integer()
                            .not_null()
                            .default(0),
                    )
                    .col(
                        ColumnDef::new(Departments::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(Departments::UpdatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(ColumnDef::new(Departments::CreatedBy).uuid().not_null())
                    .col(ColumnDef::new(Departments::UpdatedBy).uuid().not_null())
                    .col(
                        ColumnDef::new(Departments::DeletedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(Departments::Version)
                            .integer()
                            .not_null()
                            .default(1),
                    )
                    .foreign_key(
                        &mut ForeignKey::create()
                            .name("fk_departments_org_id")
                            .from(Departments::Table, Departments::OrgId)
                            .to(Organizations::Table, Organizations::Id)
                            .on_delete(ForeignKeyAction::Restrict)
                            .to_owned(),
                    )
                    .foreign_key(
                        &mut ForeignKey::create()
                            .name("fk_departments_parent_id")
                            .from(Departments::Table, Departments::ParentId)
                            .to(Departments::Table, Departments::Id)
                            .on_delete(ForeignKeyAction::Restrict)
                            .to_owned(),
                    )
                    .foreign_key(
                        &mut ForeignKey::create()
                            .name("fk_departments_manager_id")
                            .from(Departments::Table, Departments::ManagerId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::SetNull)
                            .to_owned(),
                    )
                    .to_owned(),
            )
            .await?;

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

        manager
            .create_index(
                Index::create()
                    .name("idx_departments_org_id")
                    .table(Departments::Table)
                    .col(Departments::OrgId)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

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

#[derive(DeriveIden)]
enum Departments {
    Table,
    Id,
    TenantId,
    OrgId,
    Name,
    Code,
    ParentId,
    ManagerId,
    Path,
    SortOrder,
    CreatedAt,
    UpdatedAt,
    CreatedBy,
    UpdatedBy,
    DeletedAt,
    Version,
}

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

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