CREATE TABLE IF NOT EXISTS bazzar_pos.stock_adjustments (
    id BIGSERIAL PRIMARY KEY,
    movement_id BIGINT NOT NULL REFERENCES bazzar_pos.stock_movements(id) ON DELETE CASCADE,
    product_id BIGINT NOT NULL REFERENCES bazzar_pos.products(id),
    outlet_id BIGINT NOT NULL REFERENCES bazzar_pos.outlets(id),
    mode VARCHAR(20) NOT NULL CHECK (mode IN ('set', 'in', 'out')),
    qty_input INTEGER NOT NULL,
    qty_delta INTEGER NOT NULL,
    qty_before INTEGER NOT NULL,
    qty_after INTEGER NOT NULL,
    reason VARCHAR(120) NOT NULL,
    note TEXT,
    reference_no VARCHAR(120),
    actor_id BIGINT NOT NULL REFERENCES bazzar_pos.users(id),
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_stock_adjustments_product_created
    ON bazzar_pos.stock_adjustments(product_id, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_stock_adjustments_reference_no
    ON bazzar_pos.stock_adjustments(reference_no);

