-- Slice 8-UX · Executive unlocks terms so FM can amend on request
-- One active unlock at a time per tender. Consumed by the next FM save (auto-relock).

SET @sql := (SELECT IF(COUNT(*) = 0,
    'ALTER TABLE tender_contract_terms
        ADD COLUMN terms_unlock_active TINYINT(1) NOT NULL DEFAULT 0 AFTER edited_on,
        ADD COLUMN terms_unlock_by INT NOT NULL DEFAULT 0 AFTER terms_unlock_active,
        ADD COLUMN terms_unlock_by_name VARCHAR(120) NOT NULL DEFAULT "" AFTER terms_unlock_by,
        ADD COLUMN terms_unlock_reason VARCHAR(500) NOT NULL DEFAULT "" AFTER terms_unlock_by_name,
        ADD COLUMN terms_unlock_on TIMESTAMP NULL AFTER terms_unlock_reason',
    'SELECT 1') FROM information_schema.columns
    WHERE table_schema=DATABASE() AND table_name='tender_contract_terms' AND column_name='terms_unlock_active');
PREPARE s FROM @sql; EXECUTE s; DEALLOCATE PREPARE s;
