use sea_orm_migration::{prelude::*, sea_query::Expr};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[derive(DeriveIden)]
enum AudioTags {
    Table,
    Id,
    Name,
    Slug,
    CreatedAt,
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
        m.create_table(
            Table::create()
                .table(AudioTags::Table)
                .if_not_exists()
                .col(
                    ColumnDef::new(AudioTags::Id)
                        .uuid()
                        .not_null()
                        .primary_key(),
                )
                .col(
                    ColumnDef::new(AudioTags::Name)
                        .string_len(100)
                        .not_null()
                        .unique_key(),
                )
                .col(
                    ColumnDef::new(AudioTags::Slug)
                        .string_len(100)
                        .not_null()
                        .unique_key(),
                )
                .col(
                    ColumnDef::new(AudioTags::CreatedAt)
                        .timestamp_with_time_zone()
                        .not_null()
                        .default(Expr::current_timestamp()),
                )
                .to_owned(),
        )
        .await?;
        Ok(())
    }

    async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
        m.drop_table(Table::drop().table(AudioTags::Table).to_owned())
            .await?;
        Ok(())
    }
}
