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(Alias::new("user_departments"))
                    .if_not_exists()
                    .col(ColumnDef::new(Alias::new("user_id")).uuid().not_null())
                    .col(
                        ColumnDef::new(Alias::new("department_id"))
                            .uuid()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null())
                    .col(
                        ColumnDef::new(Alias::new("is_primary"))
                            .boolean()
                            .not_null()
                            .default(false),
                    )
                    .col(
                        ColumnDef::new(Alias::new("created_at"))
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(Alias::new("updated_at"))
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(ColumnDef::new(Alias::new("created_by")).uuid())
                    .col(ColumnDef::new(Alias::new("updated_by")).uuid())
                    .col(ColumnDef::new(Alias::new("deleted_at")).timestamp_with_time_zone())
                    .col(
                        ColumnDef::new(Alias::new("version"))
                            .integer()
                            .not_null()
                            .default(1),
                    )
                    .primary_key(
                        Index::create()
                            .col(Alias::new("user_id"))
                            .col(Alias::new("department_id")),
                    )
                    .to_owned(),
            )
            .await?;

        // 索引：按租户 + 用户查询部门
        manager
            .create_index(
                Index::create()
                    .if_not_exists()
                    .name("idx_user_departments_tenant_user")
                    .table(Alias::new("user_departments"))
                    .col(Alias::new("tenant_id"))
                    .col(Alias::new("user_id"))
                    .to_owned(),
            )
            .await?;

        // 索引：按部门查询成员
        manager
            .create_index(
                Index::create()
                    .if_not_exists()
                    .name("idx_user_departments_dept")
                    .table(Alias::new("user_departments"))
                    .col(Alias::new("department_id"))
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

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