-- Simplify RoutingMode enum: remove POST_MAIN, rename PARALLEL -> SHARED
-- Drop RoutingRule table (routing is now handled via award assignment)

-- 1. Update existing PARALLEL values to SHARED, POST_MAIN to SHARED
-- (safe to run even if no rows match)
UPDATE "Track" SET "routingMode" = 'PARALLEL' WHERE "routingMode" = 'POST_MAIN';

-- 2. Rename PARALLEL -> SHARED in the enum (only if PARALLEL still exists)
DO $$ BEGIN
    ALTER TYPE "RoutingMode" RENAME VALUE 'PARALLEL' TO 'SHARED';
EXCEPTION WHEN invalid_parameter_value THEN NULL; WHEN others THEN NULL; END $$;

-- 3. Remove POST_MAIN from the enum
-- PostgreSQL doesn't support DROP VALUE directly, so we recreate the enum
-- Since we already converted POST_MAIN values to PARALLEL (now SHARED), this is safe

-- Only recreate if the old enum still has POST_MAIN (i.e., hasn't been done yet)
DO $$ BEGIN
    IF EXISTS (
        SELECT 1 FROM pg_enum
        WHERE enumlabel = 'POST_MAIN'
        AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'RoutingMode')
    ) THEN
        CREATE TYPE "RoutingMode_new" AS ENUM ('SHARED', 'EXCLUSIVE');

        ALTER TABLE "Track"
          ALTER COLUMN "routingMode" TYPE "RoutingMode_new"
          USING ("routingMode"::text::"RoutingMode_new");

        DROP TYPE "RoutingMode";
        ALTER TYPE "RoutingMode_new" RENAME TO "RoutingMode";
    END IF;
END $$;

-- 4. Drop the RoutingRule table (no longer needed)
DROP TABLE IF EXISTS "RoutingRule";
