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 manually negotiated price (in minor units) for one product, for one
        // business account — the "personal agreement" layer. `user`/`product`
        // add the user_id/product_id FKs; the unique index below keeps it to one
        // row per (account, product).
        create_table(
            m,
            "account_product_prices",
            &[
                ("id", ColType::PkAuto),
                ("price_cents", ColType::BigInteger),
            ],
            &[("user", ""), ("product", "")],
        )
        .await?;

        m.create_index(
            Index::create()
                .name("idx_account_product_prices_user_product_unique")
                .table(Alias::new("account_product_prices"))
                .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_prices").await
    }
}
