use async_trait::async_trait;
use sea_orm_migration::prelude::*;
use sea_orm_migration::schema;

#[must_use]
#[derive(Iden)]
enum Text {
    Table,
    Id,
    DateTime,
    Content,
}

#[must_use]
#[derive(Iden)]
enum Image {
    Table,
    Url,
    Content,
}

#[must_use]
#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Text::Table)
                    .if_not_exists()
                    .col(schema::integer(Text::Id).primary_key())
                    .col(schema::date_time_null(Text::DateTime))
                    .col(schema::binary(Text::Content))
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(Image::Table)
                    .if_not_exists()
                    .col(schema::string(Image::Url).primary_key())
                    .col(schema::binary(Image::Content))
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

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

        manager
            .drop_table(Table::drop().table(Image::Table).if_exists().to_owned())
            .await?;

        Ok(())
    }
}
