-- Migration: Convert uuid PKs to bigserial, rename old uuid id to guid, convert FK columns to bigint
-- Tables with uuid PK → bigserial: agent_skills, agent_guardrails, chat_histories, learnings, plans
-- Tables with serial → bigserial: thought_links, plan_dependencies, plan_related_plans, plan_skills, plan_guardrails, project_skills, project_guardrails
-- FK columns: all uuid FK columns converted to bigint referencing .id
-- Idempotent: safe to re-run at any point.

BEGIN;

-- ============================================================
-- STEP 1: Drop all FK constraints affected by this migration
-- ============================================================

ALTER TABLE public.thoughts DROP CONSTRAINT IF EXISTS thoughts_project_id_fkey;
ALTER TABLE public.thoughts DROP CONSTRAINT IF EXISTS fk_thoughts_project_id;
ALTER TABLE public.embeddings DROP CONSTRAINT IF EXISTS embeddings_thought_id_fkey;
ALTER TABLE public.embeddings DROP CONSTRAINT IF EXISTS fk_embeddings_thought_id;
ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_thought_id_fkey;
ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS fk_stored_files_thought_id;
ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_project_id_fkey;
ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS fk_stored_files_project_id;
ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_project_id_fkey;
ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS fk_project_skills_project_id;
ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_skill_id_fkey;
ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS fk_project_skills_skill_id;
ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_project_id_fkey;
ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS fk_project_guardrails_project_id;
ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_guardrail_id_fkey;
ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS fk_project_guardrails_guardrail_id;
ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_project_id_fkey;
ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS fk_chat_histories_project_id;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_project_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_project_id;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_thought_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_related_thought_id;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_skill_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_related_skill_id;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_duplicate_of_learning_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_duplicate_of_learning_id;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_supersedes_learning_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS fk_learnings_supersedes_learning_id;
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_project_id_fkey;
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS fk_plans_project_id;
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_supersedes_plan_id_fkey;
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS fk_plans_supersedes_plan_id;
ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_plan_id_fkey;
ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS fk_plan_dependencies_plan_id;
ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_depends_on_plan_id_fkey;
ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS fk_plan_dependencies_depends_on_plan_id;
ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS plan_related_plans_plan_a_id_fkey;
ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS fk_plan_related_plans_plan_a_id;
ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS plan_related_plans_plan_b_id_fkey;
ALTER TABLE public.plan_related_plans DROP CONSTRAINT IF EXISTS fk_plan_related_plans_plan_b_id;
ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_plan_id_fkey;
ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS fk_plan_skills_plan_id;
ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_skill_id_fkey;
ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS fk_plan_skills_skill_id;
ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_plan_id_fkey;
ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS fk_plan_guardrails_plan_id;
ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_guardrail_id_fkey;
ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS fk_plan_guardrails_guardrail_id;

-- ============================================================
-- STEP 2: Drop PK constraints on uuid-pk tables
-- ============================================================

ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS agent_skills_pkey;
ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS [REDACTED]
ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS agent_guardrails_pkey;
ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS [REDACTED]
ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_pkey;
ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS [REDACTED]
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_pkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS [REDACTED]
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_pkey;
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS pk_public_plans;

-- ============================================================
-- STEP 3: Rename uuid id → guid on formerly uuid-pk tables
-- Only renames if id exists and guid does not yet exist.
-- ============================================================

DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_skills' AND column_name='id')
     AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_skills' AND column_name='guid') THEN
    ALTER TABLE public.agent_skills RENAME COLUMN id TO guid;
  END IF;
END $$;

DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_guardrails' AND column_name='id')
     AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='agent_guardrails' AND column_name='guid') THEN
    ALTER TABLE public.agent_guardrails RENAME COLUMN id TO guid;
  END IF;
END $$;

DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='id')
     AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='guid') THEN
    ALTER TABLE public.chat_histories RENAME COLUMN id TO guid;
  END IF;
END $$;

DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='id')
     AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='guid') THEN
    ALTER TABLE public.learnings RENAME COLUMN id TO guid;
  END IF;
END $$;

DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='id')
     AND NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='guid') THEN
    ALTER TABLE public.plans RENAME COLUMN id TO guid;
  END IF;
END $$;

-- ============================================================
-- STEP 4: Add bigserial id columns to formerly uuid-pk tables
-- ============================================================

ALTER TABLE public.agent_skills ADD COLUMN IF NOT EXISTS id bigserial;
ALTER TABLE public.agent_guardrails ADD COLUMN IF NOT EXISTS id bigserial;
ALTER TABLE public.chat_histories ADD COLUMN IF NOT EXISTS id bigserial;
ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS id bigserial;
ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS id bigserial;

-- ============================================================
-- STEP 5: Add new bigint FK columns and migrate data from uuid columns.
-- Each update is guarded: only runs when the source column is still uuid type.
-- ============================================================

-- thoughts.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.thoughts ADD COLUMN IF NOT EXISTS project_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='thoughts' AND column_name='project_id' AND data_type='uuid') THEN
    UPDATE public.thoughts t SET project_id_new = p.id FROM public.projects p WHERE p.guid = t.project_id AND t.project_id_new IS NULL;
  END IF;
END $$;

-- embeddings.thought_id (uuid → bigint via thoughts.guid)
ALTER TABLE public.embeddings ADD COLUMN IF NOT EXISTS thought_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='embeddings' AND column_name='thought_id' AND data_type='uuid') THEN
    UPDATE public.embeddings e SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = e.thought_id AND e.thought_id_new IS NULL;
  END IF;
END $$;

-- stored_files.thought_id (uuid → bigint via thoughts.guid)
ALTER TABLE public.stored_files ADD COLUMN IF NOT EXISTS thought_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='thought_id' AND data_type='uuid') THEN
    UPDATE public.stored_files f SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = f.thought_id AND f.thought_id_new IS NULL;
  END IF;
END $$;

-- stored_files.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.stored_files ADD COLUMN IF NOT EXISTS project_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='project_id' AND data_type='uuid') THEN
    UPDATE public.stored_files f SET project_id_new = p.id FROM public.projects p WHERE p.guid = f.project_id AND f.project_id_new IS NULL;
  END IF;
END $$;

-- project_skills.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.project_skills ADD COLUMN IF NOT EXISTS project_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='project_id' AND data_type='uuid') THEN
    UPDATE public.project_skills ps SET project_id_new = p.id FROM public.projects p WHERE p.guid = ps.project_id AND ps.project_id_new IS NULL;
  END IF;
END $$;

-- project_skills.skill_id (uuid → bigint via agent_skills.guid)
ALTER TABLE public.project_skills ADD COLUMN IF NOT EXISTS skill_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='skill_id' AND data_type='uuid') THEN
    UPDATE public.project_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id AND ps.skill_id_new IS NULL;
  END IF;
END $$;

-- project_guardrails.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.project_guardrails ADD COLUMN IF NOT EXISTS project_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='project_id' AND data_type='uuid') THEN
    UPDATE public.project_guardrails pg SET project_id_new = p.id FROM public.projects p WHERE p.guid = pg.project_id AND pg.project_id_new IS NULL;
  END IF;
END $$;

-- project_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid)
ALTER TABLE public.project_guardrails ADD COLUMN IF NOT EXISTS guardrail_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='guardrail_id' AND data_type='uuid') THEN
    UPDATE public.project_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id AND pg.guardrail_id_new IS NULL;
  END IF;
END $$;

-- chat_histories.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.chat_histories ADD COLUMN IF NOT EXISTS project_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='project_id' AND data_type='uuid') THEN
    UPDATE public.chat_histories ch SET project_id_new = p.id FROM public.projects p WHERE p.guid = ch.project_id AND ch.project_id_new IS NULL;
  END IF;
END $$;

-- learnings.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS project_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='project_id' AND data_type='uuid') THEN
    UPDATE public.learnings l SET project_id_new = p.id FROM public.projects p WHERE p.guid = l.project_id AND l.project_id_new IS NULL;
  END IF;
END $$;

-- learnings.related_thought_id (uuid → bigint via thoughts.guid)
ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS related_thought_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_thought_id' AND data_type='uuid') THEN
    UPDATE public.learnings l SET related_thought_id_new = t.id FROM public.thoughts t WHERE t.guid = l.related_thought_id AND l.related_thought_id_new IS NULL;
  END IF;
END $$;

-- learnings.related_skill_id (uuid → bigint via agent_skills.guid)
ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS related_skill_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_skill_id' AND data_type='uuid') THEN
    UPDATE public.learnings l SET related_skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = l.related_skill_id AND l.related_skill_id_new IS NULL;
  END IF;
END $$;

-- learnings.duplicate_of_learning_id (uuid → bigint via learnings.guid)
ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS duplicate_of_learning_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='duplicate_of_learning_id' AND data_type='uuid') THEN
    UPDATE public.learnings l SET duplicate_of_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.duplicate_of_learning_id AND l.duplicate_of_learning_id_new IS NULL;
  END IF;
END $$;

-- learnings.supersedes_learning_id (uuid → bigint via learnings.guid)
ALTER TABLE public.learnings ADD COLUMN IF NOT EXISTS supersedes_learning_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='supersedes_learning_id' AND data_type='uuid') THEN
    UPDATE public.learnings l SET supersedes_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.supersedes_learning_id AND l.supersedes_learning_id_new IS NULL;
  END IF;
END $$;

-- plans.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS project_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='project_id' AND data_type='uuid') THEN
    UPDATE public.plans pl SET project_id_new = p.id FROM public.projects p WHERE p.guid = pl.project_id AND pl.project_id_new IS NULL;
  END IF;
END $$;

-- plans.supersedes_plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plans ADD COLUMN IF NOT EXISTS supersedes_plan_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='supersedes_plan_id' AND data_type='uuid') THEN
    UPDATE public.plans pl SET supersedes_plan_id_new = pl2.id FROM public.plans pl2 WHERE pl2.guid = pl.supersedes_plan_id AND pl.supersedes_plan_id_new IS NULL;
  END IF;
END $$;

-- plan_dependencies.plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_dependencies ADD COLUMN IF NOT EXISTS plan_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='plan_id' AND data_type='uuid') THEN
    UPDATE public.plan_dependencies pd SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.plan_id AND pd.plan_id_new IS NULL;
  END IF;
END $$;

-- plan_dependencies.depends_on_plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_dependencies ADD COLUMN IF NOT EXISTS depends_on_plan_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='depends_on_plan_id' AND data_type='uuid') THEN
    UPDATE public.plan_dependencies pd SET depends_on_plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.depends_on_plan_id AND pd.depends_on_plan_id_new IS NULL;
  END IF;
END $$;

-- plan_related_plans.plan_a_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_related_plans ADD COLUMN IF NOT EXISTS plan_a_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_a_id' AND data_type='uuid') THEN
    UPDATE public.plan_related_plans pr SET plan_a_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_a_id AND pr.plan_a_id_new IS NULL;
  END IF;
END $$;

-- plan_related_plans.plan_b_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_related_plans ADD COLUMN IF NOT EXISTS plan_b_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_b_id' AND data_type='uuid') THEN
    UPDATE public.plan_related_plans pr SET plan_b_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_b_id AND pr.plan_b_id_new IS NULL;
  END IF;
END $$;

-- plan_skills.plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_skills ADD COLUMN IF NOT EXISTS plan_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='plan_id' AND data_type='uuid') THEN
    UPDATE public.plan_skills ps SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = ps.plan_id AND ps.plan_id_new IS NULL;
  END IF;
END $$;

-- plan_skills.skill_id (uuid → bigint via agent_skills.guid)
ALTER TABLE public.plan_skills ADD COLUMN IF NOT EXISTS skill_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='skill_id' AND data_type='uuid') THEN
    UPDATE public.plan_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id AND ps.skill_id_new IS NULL;
  END IF;
END $$;

-- plan_guardrails.plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_guardrails ADD COLUMN IF NOT EXISTS plan_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='plan_id' AND data_type='uuid') THEN
    UPDATE public.plan_guardrails pg SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pg.plan_id AND pg.plan_id_new IS NULL;
  END IF;
END $$;

-- plan_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid)
ALTER TABLE public.plan_guardrails ADD COLUMN IF NOT EXISTS guardrail_id_new bigint;
DO $$ BEGIN
  IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='guardrail_id' AND data_type='uuid') THEN
    UPDATE public.plan_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id AND pg.guardrail_id_new IS NULL;
  END IF;
END $$;

-- ============================================================
-- STEP 6: Drop old uuid FK columns
-- ============================================================

ALTER TABLE public.thoughts DROP COLUMN IF EXISTS project_id;
ALTER TABLE public.embeddings DROP COLUMN IF EXISTS thought_id;
ALTER TABLE public.stored_files DROP COLUMN IF EXISTS thought_id;
ALTER TABLE public.stored_files DROP COLUMN IF EXISTS project_id;
ALTER TABLE public.project_skills DROP COLUMN IF EXISTS project_id;
ALTER TABLE public.project_skills DROP COLUMN IF EXISTS skill_id;
ALTER TABLE public.project_guardrails DROP COLUMN IF EXISTS project_id;
ALTER TABLE public.project_guardrails DROP COLUMN IF EXISTS guardrail_id;
ALTER TABLE public.chat_histories DROP COLUMN IF EXISTS project_id;
ALTER TABLE public.learnings DROP COLUMN IF EXISTS project_id;
ALTER TABLE public.learnings DROP COLUMN IF EXISTS related_thought_id;
ALTER TABLE public.learnings DROP COLUMN IF EXISTS related_skill_id;
ALTER TABLE public.learnings DROP COLUMN IF EXISTS duplicate_of_learning_id;
ALTER TABLE public.learnings DROP COLUMN IF EXISTS supersedes_learning_id;
ALTER TABLE public.plans DROP COLUMN IF EXISTS project_id;
ALTER TABLE public.plans DROP COLUMN IF EXISTS supersedes_plan_id;
ALTER TABLE public.plan_dependencies DROP COLUMN IF EXISTS plan_id;
ALTER TABLE public.plan_dependencies DROP COLUMN IF EXISTS depends_on_plan_id;
ALTER TABLE public.plan_related_plans DROP COLUMN IF EXISTS plan_a_id;
ALTER TABLE public.plan_related_plans DROP COLUMN IF EXISTS plan_b_id;
ALTER TABLE public.plan_skills DROP COLUMN IF EXISTS plan_id;
ALTER TABLE public.plan_skills DROP COLUMN IF EXISTS skill_id;
ALTER TABLE public.plan_guardrails DROP COLUMN IF EXISTS plan_id;
ALTER TABLE public.plan_guardrails DROP COLUMN IF EXISTS guardrail_id;

-- ============================================================
-- STEP 7: Rename _new columns to final names (idempotent)
-- ============================================================

DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='thoughts' AND column_name='project_id_new') THEN ALTER TABLE public.thoughts RENAME COLUMN project_id_new TO project_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='embeddings' AND column_name='thought_id_new') THEN ALTER TABLE public.embeddings RENAME COLUMN thought_id_new TO thought_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='thought_id_new') THEN ALTER TABLE public.stored_files RENAME COLUMN thought_id_new TO thought_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='stored_files' AND column_name='project_id_new') THEN ALTER TABLE public.stored_files RENAME COLUMN project_id_new TO project_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='project_id_new') THEN ALTER TABLE public.project_skills RENAME COLUMN project_id_new TO project_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_skills' AND column_name='skill_id_new') THEN ALTER TABLE public.project_skills RENAME COLUMN skill_id_new TO skill_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='project_id_new') THEN ALTER TABLE public.project_guardrails RENAME COLUMN project_id_new TO project_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='project_guardrails' AND column_name='guardrail_id_new') THEN ALTER TABLE public.project_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='chat_histories' AND column_name='project_id_new') THEN ALTER TABLE public.chat_histories RENAME COLUMN project_id_new TO project_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='project_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN project_id_new TO project_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_thought_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN related_thought_id_new TO related_thought_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='related_skill_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN related_skill_id_new TO related_skill_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='duplicate_of_learning_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN duplicate_of_learning_id_new TO duplicate_of_learning_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='learnings' AND column_name='supersedes_learning_id_new') THEN ALTER TABLE public.learnings RENAME COLUMN supersedes_learning_id_new TO supersedes_learning_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='project_id_new') THEN ALTER TABLE public.plans RENAME COLUMN project_id_new TO project_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plans' AND column_name='supersedes_plan_id_new') THEN ALTER TABLE public.plans RENAME COLUMN supersedes_plan_id_new TO supersedes_plan_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_dependencies RENAME COLUMN plan_id_new TO plan_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_dependencies' AND column_name='depends_on_plan_id_new') THEN ALTER TABLE public.plan_dependencies RENAME COLUMN depends_on_plan_id_new TO depends_on_plan_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_a_id_new') THEN ALTER TABLE public.plan_related_plans RENAME COLUMN plan_a_id_new TO plan_a_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_related_plans' AND column_name='plan_b_id_new') THEN ALTER TABLE public.plan_related_plans RENAME COLUMN plan_b_id_new TO plan_b_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_skills RENAME COLUMN plan_id_new TO plan_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_skills' AND column_name='skill_id_new') THEN ALTER TABLE public.plan_skills RENAME COLUMN skill_id_new TO skill_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='plan_id_new') THEN ALTER TABLE public.plan_guardrails RENAME COLUMN plan_id_new TO plan_id; END IF; END $$;
DO $$ BEGIN IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='plan_guardrails' AND column_name='guardrail_id_new') THEN ALTER TABLE public.plan_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id; END IF; END $$;

-- ============================================================
-- STEP 8: Upgrade serial → bigserial (alter sequence type)
-- ============================================================

-- thought_links
ALTER SEQUENCE IF EXISTS thought_links_id_seq AS bigint;
ALTER TABLE public.thought_links ALTER COLUMN id TYPE bigint;

-- plan_dependencies
ALTER SEQUENCE IF EXISTS plan_dependencies_id_seq AS bigint;
ALTER TABLE public.plan_dependencies ALTER COLUMN id TYPE bigint;

-- plan_related_plans
ALTER SEQUENCE IF EXISTS plan_related_plans_id_seq AS bigint;
ALTER TABLE public.plan_related_plans ALTER COLUMN id TYPE bigint;

-- plan_skills
ALTER SEQUENCE IF EXISTS plan_skills_id_seq AS bigint;
ALTER TABLE public.plan_skills ALTER COLUMN id TYPE bigint;

-- plan_guardrails
ALTER SEQUENCE IF EXISTS plan_guardrails_id_seq AS bigint;
ALTER TABLE public.plan_guardrails ALTER COLUMN id TYPE bigint;

-- project_skills
ALTER SEQUENCE IF EXISTS project_skills_id_seq AS bigint;
ALTER TABLE public.project_skills ALTER COLUMN id TYPE bigint;

-- project_guardrails
ALTER SEQUENCE IF EXISTS project_guardrails_id_seq AS bigint;
ALTER TABLE public.project_guardrails ALTER COLUMN id TYPE bigint;

-- ============================================================
-- STEP 9: Add PK constraints to formerly uuid-pk tables
-- ============================================================

ALTER TABLE public.agent_skills DROP CONSTRAINT IF EXISTS agent_skills_pkey;
ALTER TABLE public.agent_guardrails DROP CONSTRAINT IF EXISTS agent_guardrails_pkey;
ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_pkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_pkey;
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_pkey;

ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_pkey PRIMARY KEY (id);
ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_pkey PRIMARY KEY (id);
ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_pkey PRIMARY KEY (id);
ALTER TABLE public.learnings ADD CONSTRAINT learnings_pkey PRIMARY KEY (id);
ALTER TABLE public.plans ADD CONSTRAINT plans_pkey PRIMARY KEY (id);

-- Add unique constraint on guid columns
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='agent_skills' AND constraint_name='agent_skills_guid_key') THEN ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_guid_key UNIQUE (guid); END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='agent_guardrails' AND constraint_name='agent_guardrails_guid_key') THEN ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_guid_key UNIQUE (guid); END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='chat_histories' AND constraint_name='chat_histories_guid_key') THEN ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_guid_key UNIQUE (guid); END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_guid_key') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_guid_key UNIQUE (guid); END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_guid_key') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_guid_key UNIQUE (guid); END IF; END $$;

-- ============================================================
-- STEP 10: Re-add FK constraints
-- ============================================================

DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='thoughts' AND constraint_name='thoughts_project_id_fkey') THEN ALTER TABLE public.thoughts ADD CONSTRAINT thoughts_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='embeddings' AND constraint_name='embeddings_thought_id_fkey') THEN ALTER TABLE public.embeddings ADD CONSTRAINT embeddings_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='stored_files' AND constraint_name='stored_files_thought_id_fkey') THEN ALTER TABLE public.stored_files ADD CONSTRAINT stored_files_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id); END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='stored_files' AND constraint_name='stored_files_project_id_fkey') THEN ALTER TABLE public.stored_files ADD CONSTRAINT stored_files_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id); END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_skills' AND constraint_name='project_skills_project_id_fkey') THEN ALTER TABLE public.project_skills ADD CONSTRAINT project_skills_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_skills' AND constraint_name='project_skills_skill_id_fkey') THEN ALTER TABLE public.project_skills ADD CONSTRAINT project_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_guardrails' AND constraint_name='project_guardrails_project_id_fkey') THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT project_guardrails_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='project_guardrails' AND constraint_name='project_guardrails_guardrail_id_fkey') THEN ALTER TABLE public.project_guardrails ADD CONSTRAINT project_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='chat_histories' AND constraint_name='chat_histories_project_id_fkey') THEN ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_project_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_related_thought_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_related_thought_id_fkey FOREIGN KEY (related_thought_id) REFERENCES public.thoughts(id) ON DELETE SET NULL; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_related_skill_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_related_skill_id_fkey FOREIGN KEY (related_skill_id) REFERENCES public.agent_skills(id) ON DELETE SET NULL; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_duplicate_of_learning_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_duplicate_of_learning_id_fkey FOREIGN KEY (duplicate_of_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='learnings' AND constraint_name='learnings_supersedes_learning_id_fkey') THEN ALTER TABLE public.learnings ADD CONSTRAINT learnings_supersedes_learning_id_fkey FOREIGN KEY (supersedes_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_project_id_fkey') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plans' AND constraint_name='plans_supersedes_plan_id_fkey') THEN ALTER TABLE public.plans ADD CONSTRAINT plans_supersedes_plan_id_fkey FOREIGN KEY (supersedes_plan_id) REFERENCES public.plans(id) ON DELETE SET NULL; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_dependencies' AND constraint_name='plan_dependencies_plan_id_fkey') THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT plan_dependencies_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_dependencies' AND constraint_name='plan_dependencies_depends_on_plan_id_fkey') THEN ALTER TABLE public.plan_dependencies ADD CONSTRAINT plan_dependencies_depends_on_plan_id_fkey FOREIGN KEY (depends_on_plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_related_plans' AND constraint_name='plan_related_plans_plan_a_id_fkey') THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT plan_related_plans_plan_a_id_fkey FOREIGN KEY (plan_a_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_related_plans' AND constraint_name='plan_related_plans_plan_b_id_fkey') THEN ALTER TABLE public.plan_related_plans ADD CONSTRAINT plan_related_plans_plan_b_id_fkey FOREIGN KEY (plan_b_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_skills' AND constraint_name='plan_skills_plan_id_fkey') THEN ALTER TABLE public.plan_skills ADD CONSTRAINT plan_skills_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_skills' AND constraint_name='plan_skills_skill_id_fkey') THEN ALTER TABLE public.plan_skills ADD CONSTRAINT plan_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_guardrails' AND constraint_name='plan_guardrails_plan_id_fkey') THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT plan_guardrails_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE; END IF; END $$;
DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE table_schema='public' AND table_name='plan_guardrails' AND constraint_name='plan_guardrails_guardrail_id_fkey') THEN ALTER TABLE public.plan_guardrails ADD CONSTRAINT plan_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE; END IF; END $$;

COMMIT;
