use sea_orm_migration::{prelude::*, schema::*};
use crate::m20240814_013106_create_user::User;

#[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(Announcement::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Announcement::Slug).string().not_null().unique_key().primary_key())
                    .col(ColumnDef::new(Announcement::Title).string().not_null())
                    .col(ColumnDef::new(Announcement::ShortTextNotice).string().not_null())
                    .col(ColumnDef::new(Announcement::FullMarkdownNotice).string().not_null())
                    .col(ColumnDef::new(Announcement::CreatorId).string().not_null())
                    .col(ColumnDef::new(Announcement::CreatedOn).date_time().not_null())
                    .col(ColumnDef::new(Announcement::UpdatedOn).date_time())
                    .col(ColumnDef::new(Announcement::Flags).integer().not_null())
                    .foreign_key(ForeignKey::create()
                        .name("fk-announcement-user")
                        .from(Announcement::Table, Announcement::CreatorId)
                        .to(User::Table, User::Id)
                        .on_delete(ForeignKeyAction::NoAction)
                    )
                    .to_owned(),
            )
            .await
    }

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

#[derive(DeriveIden)]
enum Announcement {
    Table,
    Slug,
    Title,
    ShortTextNotice,
    FullMarkdownNotice,
    CreatorId,
    CreatedOn,
    UpdatedOn,
    Flags,
}
