-- Universal Apply Page: Make Project.roundId nullable and add programId FK
-- This migration enables projects to be submitted to a program/edition without being assigned to a specific round
-- NOTE: Written to be idempotent (safe to re-run if partially applied)

-- Step 1: Add Program.slug for edition-wide apply URLs (nullable for existing programs)
ALTER TABLE "Program" ADD COLUMN IF NOT EXISTS "slug" TEXT;
CREATE UNIQUE INDEX IF NOT EXISTS "Program_slug_key" ON "Program"("slug");

-- Step 2: Add programId column (nullable initially to handle existing data)
ALTER TABLE "Project" ADD COLUMN IF NOT EXISTS "programId" TEXT;

-- Step 3: Backfill programId from existing round relationships
-- Only update rows where programId is still NULL (idempotent)
UPDATE "Project" p
SET "programId" = r."programId"
FROM "Round" r
WHERE p."roundId" = r.id
  AND p."programId" IS NULL;

-- Step 4: Handle orphaned projects (no roundId = no way to derive programId)
-- Assign them to the first available program, or delete them if no program exists
DO $$
DECLARE
  null_count INTEGER;
  fallback_program_id TEXT;
BEGIN
  SELECT COUNT(*) INTO null_count FROM "Project" WHERE "programId" IS NULL;
  IF null_count > 0 THEN
    SELECT id INTO fallback_program_id FROM "Program" ORDER BY "createdAt" ASC LIMIT 1;
    IF fallback_program_id IS NOT NULL THEN
      UPDATE "Project" SET "programId" = fallback_program_id WHERE "programId" IS NULL;
      RAISE NOTICE 'Assigned % orphaned projects to fallback program %', null_count, fallback_program_id;
    ELSE
      DELETE FROM "Project" WHERE "programId" IS NULL;
      RAISE NOTICE 'Deleted % orphaned projects (no program exists to assign them to)', null_count;
    END IF;
  END IF;
END $$;

-- Step 5: Make programId required (NOT NULL constraint) - safe if already NOT NULL
DO $$
BEGIN
  IF EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name = 'Project' AND column_name = 'programId' AND is_nullable = 'YES'
  ) THEN
    ALTER TABLE "Project" ALTER COLUMN "programId" SET NOT NULL;
  END IF;
END $$;

-- Step 6: Add foreign key constraint for programId (skip if already exists)
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE constraint_name = 'Project_programId_fkey' AND table_name = 'Project'
  ) THEN
    ALTER TABLE "Project" ADD CONSTRAINT "Project_programId_fkey"
      FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE;
  END IF;
END $$;

-- Step 7: Make roundId nullable (allow projects without round assignment)
-- Safe: DROP NOT NULL is idempotent if already nullable
DO $$
BEGIN
  IF EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name = 'Project' AND column_name = 'roundId' AND is_nullable = 'NO'
  ) THEN
    ALTER TABLE "Project" ALTER COLUMN "roundId" DROP NOT NULL;
  END IF;
END $$;

-- Step 8: Update round FK to SET NULL on delete (instead of CASCADE)
-- Projects should remain in the database if their round is deleted
DO $$
BEGIN
  IF EXISTS (
    SELECT 1 FROM information_schema.table_constraints
    WHERE constraint_name = 'Project_roundId_fkey' AND table_name = 'Project'
  ) THEN
    ALTER TABLE "Project" DROP CONSTRAINT "Project_roundId_fkey";
  END IF;
  ALTER TABLE "Project" ADD CONSTRAINT "Project_roundId_fkey"
    FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL;
END $$;

-- Step 9: Add performance indexes
CREATE INDEX IF NOT EXISTS "Project_programId_idx" ON "Project"("programId");
CREATE INDEX IF NOT EXISTS "Project_programId_roundId_idx" ON "Project"("programId", "roundId");
