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> {
        // A reusable, named discount layer: a percentage (basis points, 5% = 500)
        // over a product scope. `scope_type` is 'include' (covers the listed
        // products) or 'all_except' (covers everything but the listed products).
        create_table(
            m,
            "discount_profiles",
            &[
                ("id", ColType::PkAuto),
                ("name", ColType::String),
                ("percent_bp", ColType::Integer),
                ("scope_type", ColType::StringWithDefault("include".to_string())),
            ],
            &[],
        )
        .await?;

        // Which products the scope lists (meaning depends on scope_type).
        create_table(
            m,
            "discount_profile_products",
            &[("id", ColType::PkAuto)],
            &[("discount_profile", ""), ("product", "")],
        )
        .await?;
        m.create_index(
            Index::create()
                .name("idx_discount_profile_products_unique")
                .table(Alias::new("discount_profile_products"))
                .col(Alias::new("discount_profile_id"))
                .col(Alias::new("product_id"))
                .unique()
                .to_owned(),
        )
        .await?;

        // Which profiles a business account has (mixable).
        create_table(
            m,
            "account_discount_profiles",
            &[("id", ColType::PkAuto)],
            &[("user", ""), ("discount_profile", "")],
        )
        .await?;
        m.create_index(
            Index::create()
                .name("idx_account_discount_profiles_unique")
                .table(Alias::new("account_discount_profiles"))
                .col(Alias::new("user_id"))
                .col(Alias::new("discount_profile_id"))
                .unique()
                .to_owned(),
        )
        .await?;

        // The admin's chosen winning profile when two assigned profiles cover the
        // same product for an account (collision resolution).
        create_table(
            m,
            "account_product_resolutions",
            &[("id", ColType::PkAuto)],
            &[("user", ""), ("product", ""), ("discount_profile", "")],
        )
        .await?;
        m.create_index(
            Index::create()
                .name("idx_account_product_resolutions_unique")
                .table(Alias::new("account_product_resolutions"))
                .col(Alias::new("user_id"))
                .col(Alias::new("product_id"))
                .unique()
                .to_owned(),
        )
        .await
    }

    async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
        drop_table(m, "account_product_resolutions").await?;
        drop_table(m, "account_discount_profiles").await?;
        drop_table(m, "discount_profile_products").await?;
        drop_table(m, "discount_profiles").await
    }
}
