-- 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

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.embeddings DROP CONSTRAINT IF EXISTS embeddings_thought_id_fkey;
ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_thought_id_fkey;
ALTER TABLE public.stored_files DROP CONSTRAINT IF EXISTS stored_files_project_id_fkey;
ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_project_id_fkey;
ALTER TABLE public.project_skills DROP CONSTRAINT IF EXISTS project_skills_skill_id_fkey;
ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_project_id_fkey;
ALTER TABLE public.project_guardrails DROP CONSTRAINT IF EXISTS project_guardrails_guardrail_id_fkey;
ALTER TABLE public.chat_histories DROP CONSTRAINT IF EXISTS chat_histories_project_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_project_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_thought_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_related_skill_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_duplicate_of_learning_id_fkey;
ALTER TABLE public.learnings DROP CONSTRAINT IF EXISTS learnings_supersedes_learning_id_fkey;
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_project_id_fkey;
ALTER TABLE public.plans DROP CONSTRAINT IF EXISTS plans_supersedes_plan_id_fkey;
ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_plan_id_fkey;
ALTER TABLE public.plan_dependencies DROP CONSTRAINT IF EXISTS plan_dependencies_depends_on_plan_id_fkey;
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 plan_related_plans_plan_b_id_fkey;
ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_plan_id_fkey;
ALTER TABLE public.plan_skills DROP CONSTRAINT IF EXISTS plan_skills_skill_id_fkey;
ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_plan_id_fkey;
ALTER TABLE public.plan_guardrails DROP CONSTRAINT IF EXISTS plan_guardrails_guardrail_id_fkey;

-- ============================================================
-- 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_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;

-- ============================================================
-- STEP 3: Rename uuid id → guid on formerly uuid-pk tables
-- ============================================================

ALTER TABLE public.agent_skills RENAME COLUMN id TO guid;
ALTER TABLE public.agent_guardrails RENAME COLUMN id TO guid;
ALTER TABLE public.chat_histories RENAME COLUMN id TO guid;
ALTER TABLE public.learnings RENAME COLUMN id TO guid;
ALTER TABLE public.plans RENAME COLUMN id TO guid;

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

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

-- ============================================================
-- STEP 5: Add new bigint FK columns (prefixed _new) where uuid FKs exist
-- ============================================================

-- thoughts.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.thoughts ADD COLUMN project_id_new bigint;
UPDATE public.thoughts t SET project_id_new = p.id FROM public.projects p WHERE p.guid = t.project_id;

-- embeddings.thought_id (uuid → bigint via thoughts.guid)
ALTER TABLE public.embeddings ADD COLUMN thought_id_new bigint;
UPDATE public.embeddings e SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = e.thought_id;

-- stored_files.thought_id (uuid → bigint via thoughts.guid)
ALTER TABLE public.stored_files ADD COLUMN thought_id_new bigint;
UPDATE public.stored_files f SET thought_id_new = t.id FROM public.thoughts t WHERE t.guid = f.thought_id;

-- stored_files.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.stored_files ADD COLUMN project_id_new bigint;
UPDATE public.stored_files f SET project_id_new = p.id FROM public.projects p WHERE p.guid = f.project_id;

-- project_skills.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.project_skills ADD COLUMN project_id_new bigint;
UPDATE public.project_skills ps SET project_id_new = p.id FROM public.projects p WHERE p.guid = ps.project_id;

-- project_skills.skill_id (uuid → bigint via agent_skills.guid)
ALTER TABLE public.project_skills ADD COLUMN skill_id_new bigint;
UPDATE public.project_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id;

-- project_guardrails.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.project_guardrails ADD COLUMN project_id_new bigint;
UPDATE public.project_guardrails pg SET project_id_new = p.id FROM public.projects p WHERE p.guid = pg.project_id;

-- project_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid)
ALTER TABLE public.project_guardrails ADD COLUMN guardrail_id_new bigint;
UPDATE public.project_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id;

-- chat_histories.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.chat_histories ADD COLUMN project_id_new bigint;
UPDATE public.chat_histories ch SET project_id_new = p.id FROM public.projects p WHERE p.guid = ch.project_id;

-- learnings.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.learnings ADD COLUMN project_id_new bigint;
UPDATE public.learnings l SET project_id_new = p.id FROM public.projects p WHERE p.guid = l.project_id;

-- learnings.related_thought_id (uuid → bigint via thoughts.guid)
ALTER TABLE public.learnings ADD COLUMN related_thought_id_new bigint;
UPDATE public.learnings l SET related_thought_id_new = t.id FROM public.thoughts t WHERE t.guid = l.related_thought_id;

-- learnings.related_skill_id (uuid → bigint via agent_skills.guid)
ALTER TABLE public.learnings ADD COLUMN related_skill_id_new bigint;
UPDATE public.learnings l SET related_skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = l.related_skill_id;

-- learnings.duplicate_of_learning_id (uuid → bigint via learnings.guid)
ALTER TABLE public.learnings ADD COLUMN duplicate_of_learning_id_new bigint;
UPDATE public.learnings l SET duplicate_of_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.duplicate_of_learning_id;

-- learnings.supersedes_learning_id (uuid → bigint via learnings.guid)
ALTER TABLE public.learnings ADD COLUMN supersedes_learning_id_new bigint;
UPDATE public.learnings l SET supersedes_learning_id_new = l2.id FROM public.learnings l2 WHERE l2.guid = l.supersedes_learning_id;

-- plans.project_id (uuid → bigint via projects.guid)
ALTER TABLE public.plans ADD COLUMN project_id_new bigint;
UPDATE public.plans pl SET project_id_new = p.id FROM public.projects p WHERE p.guid = pl.project_id;

-- plans.supersedes_plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plans ADD COLUMN supersedes_plan_id_new bigint;
UPDATE public.plans pl SET supersedes_plan_id_new = pl2.id FROM public.plans pl2 WHERE pl2.guid = pl.supersedes_plan_id;

-- plan_dependencies.plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_dependencies ADD COLUMN plan_id_new bigint;
UPDATE public.plan_dependencies pd SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pd.plan_id;

-- plan_dependencies.depends_on_plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_dependencies ADD COLUMN depends_on_plan_id_new bigint;
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;

-- plan_related_plans.plan_a_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_related_plans ADD COLUMN plan_a_id_new bigint;
UPDATE public.plan_related_plans pr SET plan_a_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_a_id;

-- plan_related_plans.plan_b_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_related_plans ADD COLUMN plan_b_id_new bigint;
UPDATE public.plan_related_plans pr SET plan_b_id_new = pl.id FROM public.plans pl WHERE pl.guid = pr.plan_b_id;

-- plan_skills.plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_skills ADD COLUMN plan_id_new bigint;
UPDATE public.plan_skills ps SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = ps.plan_id;

-- plan_skills.skill_id (uuid → bigint via agent_skills.guid)
ALTER TABLE public.plan_skills ADD COLUMN skill_id_new bigint;
UPDATE public.plan_skills ps SET skill_id_new = s.id FROM public.agent_skills s WHERE s.guid = ps.skill_id;

-- plan_guardrails.plan_id (uuid → bigint via plans.guid)
ALTER TABLE public.plan_guardrails ADD COLUMN plan_id_new bigint;
UPDATE public.plan_guardrails pg SET plan_id_new = pl.id FROM public.plans pl WHERE pl.guid = pg.plan_id;

-- plan_guardrails.guardrail_id (uuid → bigint via agent_guardrails.guid)
ALTER TABLE public.plan_guardrails ADD COLUMN guardrail_id_new bigint;
UPDATE public.plan_guardrails pg SET guardrail_id_new = g.id FROM public.agent_guardrails g WHERE g.guid = pg.guardrail_id;

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

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

-- ============================================================
-- STEP 7: Rename _new columns to final names
-- ============================================================

ALTER TABLE public.thoughts RENAME COLUMN project_id_new TO project_id;
ALTER TABLE public.embeddings RENAME COLUMN thought_id_new TO thought_id;
ALTER TABLE public.stored_files RENAME COLUMN thought_id_new TO thought_id;
ALTER TABLE public.stored_files RENAME COLUMN project_id_new TO project_id;
ALTER TABLE public.project_skills RENAME COLUMN project_id_new TO project_id;
ALTER TABLE public.project_skills RENAME COLUMN skill_id_new TO skill_id;
ALTER TABLE public.project_guardrails RENAME COLUMN project_id_new TO project_id;
ALTER TABLE public.project_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id;
ALTER TABLE public.chat_histories RENAME COLUMN project_id_new TO project_id;
ALTER TABLE public.learnings RENAME COLUMN project_id_new TO project_id;
ALTER TABLE public.learnings RENAME COLUMN related_thought_id_new TO related_thought_id;
ALTER TABLE public.learnings RENAME COLUMN related_skill_id_new TO related_skill_id;
ALTER TABLE public.learnings RENAME COLUMN duplicate_of_learning_id_new TO duplicate_of_learning_id;
ALTER TABLE public.learnings RENAME COLUMN supersedes_learning_id_new TO supersedes_learning_id;
ALTER TABLE public.plans RENAME COLUMN project_id_new TO project_id;
ALTER TABLE public.plans RENAME COLUMN supersedes_plan_id_new TO supersedes_plan_id;
ALTER TABLE public.plan_dependencies RENAME COLUMN plan_id_new TO plan_id;
ALTER TABLE public.plan_dependencies RENAME COLUMN depends_on_plan_id_new TO depends_on_plan_id;
ALTER TABLE public.plan_related_plans RENAME COLUMN plan_a_id_new TO plan_a_id;
ALTER TABLE public.plan_related_plans RENAME COLUMN plan_b_id_new TO plan_b_id;
ALTER TABLE public.plan_skills RENAME COLUMN plan_id_new TO plan_id;
ALTER TABLE public.plan_skills RENAME COLUMN skill_id_new TO skill_id;
ALTER TABLE public.plan_guardrails RENAME COLUMN plan_id_new TO plan_id;
ALTER TABLE public.plan_guardrails RENAME COLUMN guardrail_id_new TO guardrail_id;

-- ============================================================
-- 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 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
ALTER TABLE public.agent_skills ADD CONSTRAINT agent_skills_guid_key UNIQUE (guid);
ALTER TABLE public.agent_guardrails ADD CONSTRAINT agent_guardrails_guid_key UNIQUE (guid);
ALTER TABLE public.chat_histories ADD CONSTRAINT chat_histories_guid_key UNIQUE (guid);
ALTER TABLE public.learnings ADD CONSTRAINT learnings_guid_key UNIQUE (guid);
ALTER TABLE public.plans ADD CONSTRAINT plans_guid_key UNIQUE (guid);

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

ALTER TABLE public.thoughts
  ADD CONSTRAINT thoughts_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id);

ALTER TABLE public.embeddings
  ADD CONSTRAINT embeddings_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id);

ALTER TABLE public.stored_files
  ADD CONSTRAINT stored_files_thought_id_fkey FOREIGN KEY (thought_id) REFERENCES public.thoughts(id);
ALTER TABLE public.stored_files
  ADD CONSTRAINT stored_files_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id);

ALTER TABLE public.project_skills
  ADD CONSTRAINT project_skills_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE;
ALTER TABLE public.project_skills
  ADD CONSTRAINT project_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE;

ALTER TABLE public.project_guardrails
  ADD CONSTRAINT project_guardrails_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE;
ALTER TABLE public.project_guardrails
  ADD CONSTRAINT project_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE;

ALTER TABLE public.chat_histories
  ADD CONSTRAINT chat_histories_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL;

ALTER TABLE public.learnings
  ADD CONSTRAINT learnings_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL;
ALTER TABLE public.learnings
  ADD CONSTRAINT learnings_related_thought_id_fkey FOREIGN KEY (related_thought_id) REFERENCES public.thoughts(id) ON DELETE SET NULL;
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;
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;
ALTER TABLE public.learnings
  ADD CONSTRAINT learnings_supersedes_learning_id_fkey FOREIGN KEY (supersedes_learning_id) REFERENCES public.learnings(id) ON DELETE SET NULL;

ALTER TABLE public.plans
  ADD CONSTRAINT plans_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE SET NULL;
ALTER TABLE public.plans
  ADD CONSTRAINT plans_supersedes_plan_id_fkey FOREIGN KEY (supersedes_plan_id) REFERENCES public.plans(id) ON DELETE SET NULL;

ALTER TABLE public.plan_dependencies
  ADD CONSTRAINT plan_dependencies_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE;
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;

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;
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;

ALTER TABLE public.plan_skills
  ADD CONSTRAINT plan_skills_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE;
ALTER TABLE public.plan_skills
  ADD CONSTRAINT plan_skills_skill_id_fkey FOREIGN KEY (skill_id) REFERENCES public.agent_skills(id) ON DELETE CASCADE;

ALTER TABLE public.plan_guardrails
  ADD CONSTRAINT plan_guardrails_plan_id_fkey FOREIGN KEY (plan_id) REFERENCES public.plans(id) ON DELETE CASCADE;
ALTER TABLE public.plan_guardrails
  ADD CONSTRAINT plan_guardrails_guardrail_id_fkey FOREIGN KEY (guardrail_id) REFERENCES public.agent_guardrails(id) ON DELETE CASCADE;

COMMIT;
