//! Make `product_variants.stock` nullable: a NULL stock means the variant is
//! "available" but not inventory-tracked — always purchasable, no quantity cap,
//! and never decremented on order. A numeric stock is tracked/capped as before.

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> {
        m.get_connection()
            .execute_unprepared(
                r#"
                ALTER TABLE product_variants ALTER COLUMN stock DROP DEFAULT;
                ALTER TABLE product_variants ALTER COLUMN stock DROP NOT NULL;
                "#,
            )
            .await?;
        Ok(())
    }

    async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
        m.get_connection()
            .execute_unprepared(
                r#"
                UPDATE product_variants SET stock = 0 WHERE stock IS NULL;
                ALTER TABLE product_variants ALTER COLUMN stock SET DEFAULT 0;
                ALTER TABLE product_variants ALTER COLUMN stock SET NOT NULL;
                "#,
            )
            .await?;
        Ok(())
    }
}
