-- Tables affected: qt_configurations
-- recovery.not-started: safe to re-run; ADD COLUMN IF NOT EXISTS
-- recovery.partial: ALTER TABLE qt_configurations DROP COLUMN warehouse_pricelist_id, DROP COLUMN competitor_pricelist_id;
-- recovery.completed: no action needed
-- verify: warehouse_pricelist_id column missing | SELECT 1 FROM information_schema.COLUMNS WHERE table_schema=DATABASE() AND table_name='qt_configurations' AND column_name='warehouse_pricelist_id' HAVING COUNT(*)=0

-- Add per-source pricelist bindings for configurations
ALTER TABLE qt_configurations
    ADD COLUMN IF NOT EXISTS warehouse_pricelist_id BIGINT UNSIGNED NULL AFTER pricelist_id,
    ADD COLUMN IF NOT EXISTS competitor_pricelist_id BIGINT UNSIGNED NULL AFTER warehouse_pricelist_id,
    ADD COLUMN IF NOT EXISTS disable_price_refresh BOOLEAN NOT NULL DEFAULT FALSE AFTER competitor_pricelist_id;

ALTER TABLE qt_configurations
    ADD INDEX IF NOT EXISTS idx_qt_configurations_warehouse_pricelist_id (warehouse_pricelist_id),
    ADD INDEX IF NOT EXISTS idx_qt_configurations_competitor_pricelist_id (competitor_pricelist_id);

-- Optional FK bindings (safe if re-run due IF NOT EXISTS on columns/indexes)
-- If your MariaDB version does not support IF NOT EXISTS for FK names, duplicate-FK errors are ignored by migration runner.
ALTER TABLE qt_configurations
    ADD CONSTRAINT fk_qt_configurations_warehouse_pricelist_id
        FOREIGN KEY (warehouse_pricelist_id)
        REFERENCES qt_pricelists(id)
        ON DELETE RESTRICT
        ON UPDATE CASCADE;

ALTER TABLE qt_configurations
    ADD CONSTRAINT fk_qt_configurations_competitor_pricelist_id
        FOREIGN KEY (competitor_pricelist_id)
        REFERENCES qt_pricelists(id)
        ON DELETE RESTRICT
        ON UPDATE CASCADE;
