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

#[derive(DeriveMigrationName)]
pub struct Migration;

// Personal vs company purchasing. `account_type` is "personal" or "company";
// the company_* columns hold the Slovak invoicing identifiers (IČO, DIČ and the
// optional VAT id IČ DPH) and are only filled for company accounts/orders.
const COMPANY_COLUMNS: [&str; 4] = ["company_name", "company_id", "tax_id", "vat_id"];

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
        for table in ["customer_profiles", "orders"] {
            add_column(
                m,
                table,
                "account_type",
                ColType::StringWithDefault("personal".to_string()),
            )
            .await?;
            for col in COMPANY_COLUMNS {
                add_column(m, table, col, ColType::StringNull).await?;
            }
        }
        Ok(())
    }

    async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
        for table in ["customer_profiles", "orders"] {
            remove_column(m, table, "account_type").await?;
            for col in COMPANY_COLUMNS {
                remove_column(m, table, col).await?;
            }
        }
        Ok(())
    }
}
