-- Add Onboarding System Schema Changes
-- This migration adds the onboarding configuration system for the public application wizard

-- CreateEnum: SpecialFieldType
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'SpecialFieldType') THEN
        CREATE TYPE "SpecialFieldType" AS ENUM (
            'TEAM_MEMBERS',
            'COMPETITION_CATEGORY',
            'OCEAN_ISSUE',
            'FILE_UPLOAD',
            'GDPR_CONSENT',
            'COUNTRY_SELECT'
        );
    END IF;
END $$;

-- CreateTable: OnboardingStep
CREATE TABLE IF NOT EXISTS "OnboardingStep" (
    "id" TEXT NOT NULL,
    "formId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "isOptional" BOOLEAN NOT NULL DEFAULT false,
    "conditionJson" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "OnboardingStep_pkey" PRIMARY KEY ("id")
);

-- Add columns to ApplicationForm
DO $$
BEGIN
    -- roundId column (unique)
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'ApplicationForm' AND column_name = 'roundId'
    ) THEN
        ALTER TABLE "ApplicationForm" ADD COLUMN "roundId" TEXT;
    END IF;

    -- sendConfirmationEmail column
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'ApplicationForm' AND column_name = 'sendConfirmationEmail'
    ) THEN
        ALTER TABLE "ApplicationForm" ADD COLUMN "sendConfirmationEmail" BOOLEAN NOT NULL DEFAULT true;
    END IF;

    -- sendTeamInviteEmails column
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'ApplicationForm' AND column_name = 'sendTeamInviteEmails'
    ) THEN
        ALTER TABLE "ApplicationForm" ADD COLUMN "sendTeamInviteEmails" BOOLEAN NOT NULL DEFAULT true;
    END IF;

    -- confirmationEmailSubject column
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'ApplicationForm' AND column_name = 'confirmationEmailSubject'
    ) THEN
        ALTER TABLE "ApplicationForm" ADD COLUMN "confirmationEmailSubject" TEXT;
    END IF;

    -- confirmationEmailBody column
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'ApplicationForm' AND column_name = 'confirmationEmailBody'
    ) THEN
        ALTER TABLE "ApplicationForm" ADD COLUMN "confirmationEmailBody" TEXT;
    END IF;
END $$;

-- Add columns to ApplicationFormField
DO $$
BEGIN
    -- stepId column
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'ApplicationFormField' AND column_name = 'stepId'
    ) THEN
        ALTER TABLE "ApplicationFormField" ADD COLUMN "stepId" TEXT;
    END IF;

    -- projectMapping column
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'ApplicationFormField' AND column_name = 'projectMapping'
    ) THEN
        ALTER TABLE "ApplicationFormField" ADD COLUMN "projectMapping" TEXT;
    END IF;

    -- specialType column
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'ApplicationFormField' AND column_name = 'specialType'
    ) THEN
        ALTER TABLE "ApplicationFormField" ADD COLUMN "specialType" "SpecialFieldType";
    END IF;
END $$;

-- Create indexes for OnboardingStep
CREATE INDEX IF NOT EXISTS "OnboardingStep_formId_idx" ON "OnboardingStep"("formId");
CREATE INDEX IF NOT EXISTS "OnboardingStep_sortOrder_idx" ON "OnboardingStep"("sortOrder");

-- Create index for ApplicationForm.roundId
CREATE UNIQUE INDEX IF NOT EXISTS "ApplicationForm_roundId_key" ON "ApplicationForm"("roundId");
CREATE INDEX IF NOT EXISTS "ApplicationForm_roundId_idx" ON "ApplicationForm"("roundId");

-- Create index for ApplicationFormField.stepId
CREATE INDEX IF NOT EXISTS "ApplicationFormField_stepId_idx" ON "ApplicationFormField"("stepId");

-- Add foreign key constraints
DO $$
BEGIN
    -- OnboardingStep -> ApplicationForm
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.table_constraints
        WHERE constraint_name = 'OnboardingStep_formId_fkey'
    ) THEN
        ALTER TABLE "OnboardingStep" ADD CONSTRAINT "OnboardingStep_formId_fkey"
            FOREIGN KEY ("formId") REFERENCES "ApplicationForm"("id") ON DELETE CASCADE ON UPDATE CASCADE;
    END IF;

    -- ApplicationFormField -> OnboardingStep
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.table_constraints
        WHERE constraint_name = 'ApplicationFormField_stepId_fkey'
    ) THEN
        ALTER TABLE "ApplicationFormField" ADD CONSTRAINT "ApplicationFormField_stepId_fkey"
            FOREIGN KEY ("stepId") REFERENCES "OnboardingStep"("id") ON DELETE SET NULL ON UPDATE CASCADE;
    END IF;

    -- ApplicationForm -> Round
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.table_constraints
        WHERE constraint_name = 'ApplicationForm_roundId_fkey'
    ) THEN
        ALTER TABLE "ApplicationForm" ADD CONSTRAINT "ApplicationForm_roundId_fkey"
            FOREIGN KEY ("roundId") REFERENCES "Round"("id") ON DELETE SET NULL ON UPDATE CASCADE;
    END IF;
END $$;
