-- Migration script to rename is_test columns to is_fake
-- This separates fake/demo data from test licenses

-- 1. Rename columns in all tables
DO $$ 
BEGIN
    -- Rename is_test to is_fake in customers table
    IF EXISTS (SELECT 1 FROM information_schema.columns 
               WHERE table_name = 'customers' AND column_name = 'is_test') THEN
        ALTER TABLE customers RENAME COLUMN is_test TO is_fake;
    END IF;
    
    -- Rename is_test to is_fake in licenses table
    IF EXISTS (SELECT 1 FROM information_schema.columns 
               WHERE table_name = 'licenses' AND column_name = 'is_test') THEN
        ALTER TABLE licenses RENAME COLUMN is_test TO is_fake;
    END IF;
    
    -- Rename is_test to is_fake in resource_pools table
    IF EXISTS (SELECT 1 FROM information_schema.columns 
               WHERE table_name = 'resource_pools' AND column_name = 'is_test') THEN
        ALTER TABLE resource_pools RENAME COLUMN is_test TO is_fake;
    END IF;
END $$;

-- 2. Rename indexes
DO $$
BEGIN
    -- Rename index for customers
    IF EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = 'idx_customers_is_test') THEN
        ALTER INDEX idx_customers_is_test RENAME TO idx_customers_is_fake;
    END IF;
    
    -- Rename index for licenses
    IF EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = 'idx_licenses_is_test') THEN
        ALTER INDEX idx_licenses_is_test RENAME TO idx_licenses_is_fake;
    END IF;
    
    -- Rename index for resource_pools
    IF EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = 'idx_resource_pools_is_test') THEN
        ALTER INDEX idx_resource_pools_is_test RENAME TO idx_resource_pools_is_fake;
    END IF;
END $$;

-- 3. Add comments to clarify the purpose
COMMENT ON COLUMN customers.is_fake IS 'Marks fake/demo data, not to be confused with test licenses';
COMMENT ON COLUMN licenses.is_fake IS 'Marks fake/demo data, not to be confused with test license type';
COMMENT ON COLUMN resource_pools.is_fake IS 'Marks fake/demo resources';