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

#[derive(DeriveMigrationName)]
pub struct Migration;

#[derive(DeriveIden)]
enum AudioTracks {
    Table,
    Id,
    AlbumId,
    Title,
    Slug,
    AudioFileId,
    TrackNumber,
    Duration,
    Featured,
    PlayCount,
    CreatedAt,
    UpdatedAt,
}

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

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
        m.create_table(
            Table::create()
                .table(AudioTracks::Table)
                .if_not_exists()
                .col(
                    ColumnDef::new(AudioTracks::Id)
                        .uuid()
                        .not_null()
                        .primary_key(),
                )
                .col(ColumnDef::new(AudioTracks::AlbumId).uuid().not_null())
                .col(
                    ColumnDef::new(AudioTracks::Title)
                        .string_len(500)
                        .not_null(),
                )
                .col(ColumnDef::new(AudioTracks::Slug).string_len(500).not_null())
                .col(
                    ColumnDef::new(AudioTracks::AudioFileId)
                        .string_len(500)
                        .not_null(),
                )
                .col(ColumnDef::new(AudioTracks::TrackNumber).integer().null())
                .col(ColumnDef::new(AudioTracks::Duration).integer().null())
                .col(
                    ColumnDef::new(AudioTracks::Featured)
                        .boolean()
                        .not_null()
                        .default(false),
                )
                .col(
                    ColumnDef::new(AudioTracks::PlayCount)
                        .integer()
                        .not_null()
                        .default(0),
                )
                .col(
                    ColumnDef::new(AudioTracks::CreatedAt)
                        .timestamp_with_time_zone()
                        .not_null()
                        .default(Expr::current_timestamp()),
                )
                .col(
                    ColumnDef::new(AudioTracks::UpdatedAt)
                        .timestamp_with_time_zone()
                        .not_null()
                        .default(Expr::current_timestamp()),
                )
                .foreign_key(
                    ForeignKey::create()
                        .name("fk-audio_tracks-album_id-to-audio_albums")
                        .from(AudioTracks::Table, AudioTracks::AlbumId)
                        .to(AudioAlbums::Table, AudioAlbums::Id)
                        .on_delete(ForeignKeyAction::Cascade)
                        .on_update(ForeignKeyAction::Cascade),
                )
                .to_owned(),
        )
        .await?;

        m.create_index(
            Index::create()
                .name("idx-audio_tracks-album_id-slug")
                .table(AudioTracks::Table)
                .col(AudioTracks::AlbumId)
                .col(AudioTracks::Slug)
                .unique()
                .to_owned(),
        )
        .await?;
        Ok(())
    }

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