-- ============================================================
-- v3.7 SKU Aliases (multiple SKUs per inventory item)
-- ============================================================
-- Allows registering 2-3 alternative part numbers/SKUs for the
-- same product (e.g. different supplier SKUs).
-- ============================================================

CREATE TABLE IF NOT EXISTS inventory_sku_aliases (
    id              SERIAL PRIMARY KEY,
    inventory_id    INTEGER NOT NULL REFERENCES inventory(id) ON DELETE CASCADE,
    sku             VARCHAR(100) NOT NULL,
    label           VARCHAR(50),      -- e.g. "Bodega A", "Proveedor X"
    is_active       BOOLEAN DEFAULT true,
    created_at      TIMESTAMPTZ DEFAULT NOW(),
    CONSTRAINT inventory_sku_aliases_unique_sku
        UNIQUE (inventory_id, sku)
);

CREATE INDEX IF NOT EXISTS idx_inventory_sku_aliases_inventory
    ON inventory_sku_aliases(inventory_id) WHERE is_active = true;
CREATE INDEX IF NOT EXISTS idx_inventory_sku_aliases_sku
    ON inventory_sku_aliases(sku) WHERE is_active = true;
