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(UserSettings::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(UserSettings::User).string().not_null().primary_key())
                    .col(ColumnDef::new(UserSettings::Theme).string().not_null())
                    .foreign_key(ForeignKey::create()
                        .name("fk-usersettings-user")
                        .from(UserSettings::Table, UserSettings::User)
                        .to(User::Table, User::Id)
                        .on_delete(ForeignKeyAction::Cascade)
                    )
                    .to_owned(),
            )
            .await
    }

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

#[derive(DeriveIden)]
enum UserSettings {
    Table,
    User, // used as id also
    Theme,
}
