use loco_rs::schema::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

// Account type becomes a permanent property of the *user* (chosen at
// registration, never switchable), so it moves off `customer_profiles`. Orders
// gain a nullable `user_id` linking them to the account that placed them
// (null for guest orders that didn't create an account).
#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
        add_column(
            m,
            "users",
            "account_type",
            ColType::StringWithDefault("personal".to_string()),
        )
        .await?;
        add_column(m, "orders", "user_id", ColType::IntegerNull).await?;
        remove_column(m, "customer_profiles", "account_type").await?;
        Ok(())
    }

    async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
        add_column(
            m,
            "customer_profiles",
            "account_type",
            ColType::StringWithDefault("personal".to_string()),
        )
        .await?;
        remove_column(m, "orders", "user_id").await?;
        remove_column(m, "users", "account_type").await?;
        Ok(())
    }
}
