-- =============================================================================
-- Phase 7/8 Migration Part 1: Rename stageId -> roundId on 15 tables
-- =============================================================================
-- This migration renames stageId columns to roundId and updates FK constraints
-- to point to the Round table instead of Stage table.
--
-- NOTE: After the pipeline migration (20260213), most tables have BOTH a
-- nullable roundId column (legacy, no FK) AND a stageId column. We must
-- drop the old roundId column before renaming stageId -> roundId.

-- --- 1. EvaluationForm ---

-- Drop old roundId column (nullable, no FK since 20260213 migration)
ALTER TABLE "EvaluationForm" DROP COLUMN IF EXISTS "roundId";

-- Drop FK constraint on stageId
ALTER TABLE "EvaluationForm" DROP CONSTRAINT IF EXISTS "EvaluationForm_stageId_fkey";

-- Drop indexes
DROP INDEX IF EXISTS "EvaluationForm_stageId_version_key";
DROP INDEX IF EXISTS "EvaluationForm_stageId_isActive_idx";

-- Rename column (only if stageId exists)
DO $$ BEGIN
    ALTER TABLE "EvaluationForm" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

-- Recreate indexes with new name
CREATE UNIQUE INDEX IF NOT EXISTS "EvaluationForm_roundId_version_key" ON "EvaluationForm"("roundId", "version");
CREATE INDEX IF NOT EXISTS "EvaluationForm_roundId_isActive_idx" ON "EvaluationForm"("roundId", "isActive");

-- Recreate FK pointing to Round
DO $$ BEGIN
    ALTER TABLE "EvaluationForm" ADD CONSTRAINT "EvaluationForm_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 2. FileRequirement ---

ALTER TABLE "FileRequirement" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "FileRequirement" DROP CONSTRAINT IF EXISTS "FileRequirement_stageId_fkey";

DROP INDEX IF EXISTS "FileRequirement_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "FileRequirement" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE INDEX IF NOT EXISTS "FileRequirement_roundId_idx" ON "FileRequirement"("roundId");

DO $$ BEGIN
    ALTER TABLE "FileRequirement" ADD CONSTRAINT "FileRequirement_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 3. Assignment ---

ALTER TABLE "Assignment" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "Assignment" DROP CONSTRAINT IF EXISTS "Assignment_stageId_fkey";

DROP INDEX IF EXISTS "Assignment_userId_projectId_stageId_key";
DROP INDEX IF EXISTS "Assignment_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "Assignment" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE UNIQUE INDEX IF NOT EXISTS "Assignment_userId_projectId_roundId_key" ON "Assignment"("userId", "projectId", "roundId");
CREATE INDEX IF NOT EXISTS "Assignment_roundId_idx" ON "Assignment"("roundId");

DO $$ BEGIN
    ALTER TABLE "Assignment" ADD CONSTRAINT "Assignment_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 4. GracePeriod ---

ALTER TABLE "GracePeriod" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "GracePeriod" DROP CONSTRAINT IF EXISTS "GracePeriod_stageId_fkey";

DROP INDEX IF EXISTS "GracePeriod_stageId_idx";
DROP INDEX IF EXISTS "GracePeriod_stageId_userId_extendedUntil_idx";

DO $$ BEGIN
    ALTER TABLE "GracePeriod" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE INDEX IF NOT EXISTS "GracePeriod_roundId_idx" ON "GracePeriod"("roundId");
CREATE INDEX IF NOT EXISTS "GracePeriod_roundId_userId_extendedUntil_idx" ON "GracePeriod"("roundId", "userId", "extendedUntil");

DO $$ BEGIN
    ALTER TABLE "GracePeriod" ADD CONSTRAINT "GracePeriod_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 5. LiveVotingSession ---

ALTER TABLE "LiveVotingSession" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "LiveVotingSession" DROP CONSTRAINT IF EXISTS "LiveVotingSession_stageId_fkey";

DROP INDEX IF EXISTS "LiveVotingSession_stageId_key";

DO $$ BEGIN
    ALTER TABLE "LiveVotingSession" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE UNIQUE INDEX IF NOT EXISTS "LiveVotingSession_roundId_key" ON "LiveVotingSession"("roundId");

DO $$ BEGIN
    ALTER TABLE "LiveVotingSession" ADD CONSTRAINT "LiveVotingSession_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 6. FilteringRule ---

ALTER TABLE "FilteringRule" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "FilteringRule" DROP CONSTRAINT IF EXISTS "FilteringRule_stageId_fkey";

DROP INDEX IF EXISTS "FilteringRule_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "FilteringRule" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE INDEX IF NOT EXISTS "FilteringRule_roundId_idx" ON "FilteringRule"("roundId");

DO $$ BEGIN
    ALTER TABLE "FilteringRule" ADD CONSTRAINT "FilteringRule_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 7. FilteringResult ---

ALTER TABLE "FilteringResult" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "FilteringResult" DROP CONSTRAINT IF EXISTS "FilteringResult_stageId_fkey";

DROP INDEX IF EXISTS "FilteringResult_stageId_projectId_key";
DROP INDEX IF EXISTS "FilteringResult_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "FilteringResult" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE UNIQUE INDEX IF NOT EXISTS "FilteringResult_roundId_projectId_key" ON "FilteringResult"("roundId", "projectId");
CREATE INDEX IF NOT EXISTS "FilteringResult_roundId_idx" ON "FilteringResult"("roundId");

DO $$ BEGIN
    ALTER TABLE "FilteringResult" ADD CONSTRAINT "FilteringResult_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 8. FilteringJob ---

ALTER TABLE "FilteringJob" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "FilteringJob" DROP CONSTRAINT IF EXISTS "FilteringJob_stageId_fkey";

DROP INDEX IF EXISTS "FilteringJob_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "FilteringJob" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE INDEX IF NOT EXISTS "FilteringJob_roundId_idx" ON "FilteringJob"("roundId");

DO $$ BEGIN
    ALTER TABLE "FilteringJob" ADD CONSTRAINT "FilteringJob_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 9. AssignmentJob ---

ALTER TABLE "AssignmentJob" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "AssignmentJob" DROP CONSTRAINT IF EXISTS "AssignmentJob_stageId_fkey";

DROP INDEX IF EXISTS "AssignmentJob_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "AssignmentJob" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE INDEX IF NOT EXISTS "AssignmentJob_roundId_idx" ON "AssignmentJob"("roundId");

DO $$ BEGIN
    ALTER TABLE "AssignmentJob" ADD CONSTRAINT "AssignmentJob_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 10. ReminderLog ---

ALTER TABLE "ReminderLog" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "ReminderLog" DROP CONSTRAINT IF EXISTS "ReminderLog_stageId_fkey";

DROP INDEX IF EXISTS "ReminderLog_stageId_userId_type_key";
DROP INDEX IF EXISTS "ReminderLog_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "ReminderLog" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE UNIQUE INDEX IF NOT EXISTS "ReminderLog_roundId_userId_type_key" ON "ReminderLog"("roundId", "userId", "type");
CREATE INDEX IF NOT EXISTS "ReminderLog_roundId_idx" ON "ReminderLog"("roundId");

DO $$ BEGIN
    ALTER TABLE "ReminderLog" ADD CONSTRAINT "ReminderLog_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 11. EvaluationSummary ---

ALTER TABLE "EvaluationSummary" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "EvaluationSummary" DROP CONSTRAINT IF EXISTS "EvaluationSummary_stageId_fkey";

DROP INDEX IF EXISTS "EvaluationSummary_projectId_stageId_key";
DROP INDEX IF EXISTS "EvaluationSummary_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "EvaluationSummary" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE UNIQUE INDEX IF NOT EXISTS "EvaluationSummary_projectId_roundId_key" ON "EvaluationSummary"("projectId", "roundId");
CREATE INDEX IF NOT EXISTS "EvaluationSummary_roundId_idx" ON "EvaluationSummary"("roundId");

DO $$ BEGIN
    ALTER TABLE "EvaluationSummary" ADD CONSTRAINT "EvaluationSummary_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 12. EvaluationDiscussion ---

ALTER TABLE "EvaluationDiscussion" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "EvaluationDiscussion" DROP CONSTRAINT IF EXISTS "EvaluationDiscussion_stageId_fkey";

DROP INDEX IF EXISTS "EvaluationDiscussion_projectId_stageId_key";
DROP INDEX IF EXISTS "EvaluationDiscussion_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "EvaluationDiscussion" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE UNIQUE INDEX IF NOT EXISTS "EvaluationDiscussion_projectId_roundId_key" ON "EvaluationDiscussion"("projectId", "roundId");
CREATE INDEX IF NOT EXISTS "EvaluationDiscussion_roundId_idx" ON "EvaluationDiscussion"("roundId");

DO $$ BEGIN
    ALTER TABLE "EvaluationDiscussion" ADD CONSTRAINT "EvaluationDiscussion_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 13. Message ---

-- Message has roundId (from init, nullable) and stageId (from pipeline, nullable)
ALTER TABLE "Message" DROP COLUMN IF EXISTS "roundId";

ALTER TABLE "Message" DROP CONSTRAINT IF EXISTS "Message_stageId_fkey";

DROP INDEX IF EXISTS "Message_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "Message" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE INDEX IF NOT EXISTS "Message_roundId_idx" ON "Message"("roundId");

DO $$ BEGIN
    ALTER TABLE "Message" ADD CONSTRAINT "Message_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 14. Cohort ---
-- Cohort was created in pipeline migration with stageId only (no roundId)

ALTER TABLE "Cohort" DROP CONSTRAINT IF EXISTS "Cohort_stageId_fkey";

DROP INDEX IF EXISTS "Cohort_stageId_idx";

DO $$ BEGIN
    ALTER TABLE "Cohort" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE INDEX IF NOT EXISTS "Cohort_roundId_idx" ON "Cohort"("roundId");

DO $$ BEGIN
    ALTER TABLE "Cohort" ADD CONSTRAINT "Cohort_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 15. LiveProgressCursor ---
-- LiveProgressCursor was created in pipeline migration with stageId only (no roundId)

ALTER TABLE "LiveProgressCursor" DROP CONSTRAINT IF EXISTS "LiveProgressCursor_stageId_fkey";

DROP INDEX IF EXISTS "LiveProgressCursor_stageId_key";

DO $$ BEGIN
    ALTER TABLE "LiveProgressCursor" RENAME COLUMN "stageId" TO "roundId";
EXCEPTION WHEN undefined_column THEN NULL; END $$;

CREATE UNIQUE INDEX IF NOT EXISTS "LiveProgressCursor_roundId_key" ON "LiveProgressCursor"("roundId");

DO $$ BEGIN
    ALTER TABLE "LiveProgressCursor" ADD CONSTRAINT "LiveProgressCursor_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 16. SpecialAward: Drop trackId column ---

ALTER TABLE "SpecialAward" DROP CONSTRAINT IF EXISTS "SpecialAward_trackId_fkey";

DROP INDEX IF EXISTS "SpecialAward_trackId_key";

ALTER TABLE "SpecialAward" DROP COLUMN IF EXISTS "trackId";

-- --- 17. ConflictOfInterest: roundId was made nullable in pipeline migration ---
-- It still exists, just restore FK to new Round table
DO $$ BEGIN
    ALTER TABLE "ConflictOfInterest" ADD CONSTRAINT "ConflictOfInterest_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE CASCADE ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;

-- --- 18. TaggingJob: roundId was made nullable in pipeline migration ---
-- Restore FK to new Round table
DO $$ BEGIN
    ALTER TABLE "TaggingJob" ADD CONSTRAINT "TaggingJob_roundId_fkey"
      FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL ON UPDATE CASCADE;
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
