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

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
        // Discount profiles applied globally to a whole audience, set on the
        // discounts page: "personal" lowers the public price for everyone,
        // "business" lowers the price for all company accounts. Per-company
        // assignments (account_discount_profiles) still layer on top.
        create_table(
            m,
            "audience_discount_profiles",
            &[
                ("id", ColType::PkAuto),
                ("audience", ColType::String),
            ],
            &[("discount_profile", "")],
        )
        .await?;

        m.create_index(
            Index::create()
                .name("idx_audience_discount_profiles_unique")
                .table(Alias::new("audience_discount_profiles"))
                .col(Alias::new("audience"))
                .col(Alias::new("discount_profile_id"))
                .unique()
                .to_owned(),
        )
        .await
    }

    async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
        drop_table(m, "audience_discount_profiles").await
    }
}
