-- Step 1: Add account_sequence columns to all tables first
ALTER TABLE "authorization_roles" ADD COLUMN "account_sequence" TEXT;
ALTER TABLE "monthly_assessments" ADD COLUMN "account_sequence" TEXT;
ALTER TABLE "monthly_bypasses" ADD COLUMN "account_sequence" TEXT;
ALTER TABLE "stickman_rankings" ADD COLUMN "account_sequence" TEXT;

-- Step 2: Backfill account_sequence from existing user_id (which is still String at this point)
UPDATE "authorization_roles" SET "account_sequence" = "user_id" WHERE "account_sequence" IS NULL;
UPDATE "monthly_assessments" SET "account_sequence" = "user_id" WHERE "account_sequence" IS NULL;
UPDATE "monthly_bypasses" SET "account_sequence" = "user_id" WHERE "account_sequence" IS NULL;
UPDATE "stickman_rankings" SET "account_sequence" = "user_id" WHERE "account_sequence" IS NULL;

-- Step 3: Make account_sequence NOT NULL
ALTER TABLE "authorization_roles" ALTER COLUMN "account_sequence" SET NOT NULL;
ALTER TABLE "monthly_assessments" ALTER COLUMN "account_sequence" SET NOT NULL;
ALTER TABLE "monthly_bypasses" ALTER COLUMN "account_sequence" SET NOT NULL;
ALTER TABLE "stickman_rankings" ALTER COLUMN "account_sequence" SET NOT NULL;

-- Step 4: Drop existing unique constraints that reference user_id
DROP INDEX IF EXISTS "authorization_roles_user_id_role_type_region_code_key";

-- Step 5: Change user_id column type from String to BigInt
ALTER TABLE "authorization_roles" ALTER COLUMN "user_id" TYPE BIGINT USING "user_id"::BIGINT;
ALTER TABLE "monthly_assessments" ALTER COLUMN "user_id" TYPE BIGINT USING "user_id"::BIGINT;
ALTER TABLE "monthly_bypasses" ALTER COLUMN "user_id" TYPE BIGINT USING "user_id"::BIGINT;
ALTER TABLE "stickman_rankings" ALTER COLUMN "user_id" TYPE BIGINT USING "user_id"::BIGINT;

-- Step 6: Change authorized_by, revoked_by, bypassed_by, grantedBy, approverXId to BigInt
ALTER TABLE "authorization_roles" ALTER COLUMN "authorized_by" TYPE BIGINT USING "authorized_by"::BIGINT;
ALTER TABLE "authorization_roles" ALTER COLUMN "revoked_by" TYPE BIGINT USING "revoked_by"::BIGINT;
ALTER TABLE "monthly_assessments" ALTER COLUMN "bypassed_by" TYPE BIGINT USING "bypassed_by"::BIGINT;
ALTER TABLE "monthly_bypasses" ALTER COLUMN "granted_by" TYPE BIGINT USING "granted_by"::BIGINT;
ALTER TABLE "monthly_bypasses" ALTER COLUMN "approver1_id" TYPE BIGINT USING "approver1_id"::BIGINT;
ALTER TABLE "monthly_bypasses" ALTER COLUMN "approver2_id" TYPE BIGINT USING "approver2_id"::BIGINT;
ALTER TABLE "monthly_bypasses" ALTER COLUMN "approver3_id" TYPE BIGINT USING "approver3_id"::BIGINT;

-- Step 7: Create new unique constraint using account_sequence
CREATE UNIQUE INDEX "authorization_roles_account_sequence_role_type_region_code_key" ON "authorization_roles"("account_sequence", "role_type", "region_code");

-- Step 8: Create indexes for account_sequence
CREATE INDEX "idx_authorization_roles_account_sequence" ON "authorization_roles"("account_sequence");
CREATE INDEX "idx_monthly_assessments_account_sequence_month" ON "monthly_assessments"("account_sequence", "assessment_month");
CREATE INDEX "idx_monthly_bypasses_account_sequence_month" ON "monthly_bypasses"("account_sequence", "bypass_month");
CREATE INDEX "idx_stickman_rankings_account_sequence_month" ON "stickman_rankings"("account_sequence", "current_month");
