-- Restore foreign key constraints removed from Schema.sql for IHP schema-compiler compatibility.
-- IHP infers FK relationships from column naming conventions; these ALTER TABLE statements
-- restore referential integrity enforcement at the database level.
-- Workplan: IHUB-WP-0014 (A2 — schema parser fixes)

-- Phase 1: Core hub/widget/event structure
ALTER TABLE widgets ADD FOREIGN KEY (hub_id) REFERENCES hubs(id);
ALTER TABLE widget_versions ADD FOREIGN KEY (widget_id) REFERENCES widgets(id);
ALTER TABLE interaction_events ADD FOREIGN KEY (widget_id) REFERENCES widgets(id);
ALTER TABLE annotation_threads ADD FOREIGN KEY (widget_id) REFERENCES widgets(id);
ALTER TABLE annotation_threads ADD FOREIGN KEY (created_by) REFERENCES users(id);
ALTER TABLE annotations ADD FOREIGN KEY (widget_id) REFERENCES widgets(id);
ALTER TABLE annotations ADD FOREIGN KEY (parent_id) REFERENCES annotations(id);
ALTER TABLE annotations ADD FOREIGN KEY (thread_id) REFERENCES annotation_threads(id);
ALTER TABLE annotations ADD FOREIGN KEY (created_by) REFERENCES users(id);

-- Phase 2: Requirement candidates and triage
ALTER TABLE requirement_candidates ADD FOREIGN KEY (source_widget_id) REFERENCES widgets(id);
ALTER TABLE requirement_candidates ADD FOREIGN KEY (source_thread_id) REFERENCES annotation_threads(id);
ALTER TABLE requirement_candidates ADD FOREIGN KEY (source_annotation_id) REFERENCES annotations(id);
ALTER TABLE requirement_candidates ADD FOREIGN KEY (created_by) REFERENCES users(id);
ALTER TABLE triage_states ADD FOREIGN KEY (candidate_id) REFERENCES requirement_candidates(id);
ALTER TABLE triage_states ADD FOREIGN KEY (changed_by) REFERENCES users(id);
ALTER TABLE reviewer_assignments ADD FOREIGN KEY (candidate_id) REFERENCES requirement_candidates(id);
ALTER TABLE reviewer_assignments ADD FOREIGN KEY (user_id) REFERENCES users(id);
ALTER TABLE reviewer_assignments ADD FOREIGN KEY (assigned_by) REFERENCES users(id);
ALTER TABLE requirement_candidates ADD FOREIGN KEY (source_candidate_id) REFERENCES requirement_candidates(id);
ALTER TABLE requirement_candidates ADD FOREIGN KEY (requirement_id) REFERENCES requirements(id);

-- Phase 3: Requirements and decisions
ALTER TABLE requirements ADD FOREIGN KEY (source_candidate_id) REFERENCES requirement_candidates(id);
ALTER TABLE requirements ADD FOREIGN KEY (created_by) REFERENCES users(id);
ALTER TABLE decision_records ADD FOREIGN KEY (requirement_id) REFERENCES requirements(id);
ALTER TABLE decision_records ADD FOREIGN KEY (candidate_id) REFERENCES requirement_candidates(id);
ALTER TABLE implementation_change_references ADD FOREIGN KEY (decision_id) REFERENCES decision_records(id);
ALTER TABLE policy_references ADD FOREIGN KEY (decision_id) REFERENCES decision_records(id);

-- Phase 4: Outcome observation
ALTER TABLE deployment_records ADD FOREIGN KEY (impl_ref_id) REFERENCES implementation_change_references(id);
ALTER TABLE deployment_records ADD FOREIGN KEY (decision_id) REFERENCES decision_records(id);
ALTER TABLE outcome_signals ADD FOREIGN KEY (widget_id) REFERENCES widgets(id);
ALTER TABLE outcome_signals ADD FOREIGN KEY (deployment_id) REFERENCES deployment_records(id);

-- Phase 5: Agent proposals
ALTER TABLE agent_review_records ADD FOREIGN KEY (proposal_id) REFERENCES agent_proposals(id);
ALTER TABLE confidence_annotations ADD FOREIGN KEY (proposal_id) REFERENCES agent_proposals(id);

-- Phase 9: API consumers and keys
ALTER TABLE api_keys ADD FOREIGN KEY (api_consumer_id) REFERENCES api_consumers(id);
ALTER TABLE webhook_subscriptions ADD FOREIGN KEY (api_consumer_id) REFERENCES api_consumers(id);

-- Phase 10: Widget patterns
ALTER TABLE pattern_adoptions ADD FOREIGN KEY (widget_pattern_id) REFERENCES widget_patterns(id);

-- Phase 12: Learning
ALTER TABLE institutional_knowledge_entries ADD FOREIGN KEY (hub_id) REFERENCES hubs(id);
ALTER TABLE institutional_knowledge_entries ADD FOREIGN KEY (decision_record_id) REFERENCES decision_records(id);
