// SwarmCare Phase 0: Neo4j Medical Ontologies Setup
// 13 Medical Ontologies Integration
// Story Points: 37 | Generated: 2025-10-27

// ============================================================================
// 1. SNOMED CT (Systematized Nomenclature of Medicine Clinical Terms)
// ============================================================================
CREATE CONSTRAINT snomed_code IF NOT EXISTS FOR (s:SNOMED) REQUIRE s.code IS UNIQUE;
CREATE INDEX snomed_term IF NOT EXISTS FOR (s:SNOMED) ON (s.term);

// Example SNOMED concepts
CREATE (:SNOMED {code: '73211009', term: 'Diabetes mellitus', category: 'disorder'});
CREATE (:SNOMED {code: '38341003', term: 'Hypertensive disorder', category: 'disorder'});
CREATE (:SNOMED {code: '22298006', term: 'Myocardial infarction', category: 'disorder'});

// ============================================================================
// 2. ICD-10 (International Classification of Diseases, 10th Revision)
// ============================================================================
CREATE CONSTRAINT icd10_code IF NOT EXISTS FOR (i:ICD10) REQUIRE i.code IS UNIQUE;
CREATE INDEX icd10_description IF NOT EXISTS FOR (i:ICD10) ON (i.description);

CREATE (:ICD10 {code: 'E11', description: 'Type 2 diabetes mellitus', category: 'Endocrine'});
CREATE (:ICD10 {code: 'I10', description: 'Essential (primary) hypertension', category: 'Circulatory'});
CREATE (:ICD10 {code: 'I21', description: 'Acute myocardial infarction', category: 'Circulatory'});

// ============================================================================
// 3. RxNorm (Normalized drug names)
// ============================================================================
CREATE CONSTRAINT rxnorm_code IF NOT EXISTS FOR (r:RxNorm) REQUIRE r.rxcui IS UNIQUE;
CREATE INDEX rxnorm_name IF NOT EXISTS FOR (r:RxNorm) ON (r.name);

CREATE (:RxNorm {rxcui: '203134', name: 'Metformin', drug_class: 'Biguanides', strength: '500 MG'});
CREATE (:RxNorm {rxcui: '197361', name: 'Lisinopril', drug_class: 'ACE Inhibitors', strength: '10 MG'});
CREATE (:RxNorm {rxcui: '106258', name: 'Aspirin', drug_class: 'NSAIDs', strength: '81 MG'});

// ============================================================================
// 4. LOINC (Logical Observation Identifiers Names and Codes)
// ============================================================================
CREATE CONSTRAINT loinc_code IF NOT EXISTS FOR (l:LOINC) REQUIRE l.code IS UNIQUE;
CREATE INDEX loinc_name IF NOT EXISTS FOR (l:LOINC) ON (l.long_name);

CREATE (:LOINC {code: '2339-0', long_name: 'Glucose [Mass/volume] in Blood', component: 'Glucose', system: 'Blood'});
CREATE (:LOINC {code: '4548-4', long_name: 'Hemoglobin A1c/Hemoglobin.total in Blood', component: 'HbA1c', system: 'Blood'});
CREATE (:LOINC {code: '8867-4', long_name: 'Heart rate', component: 'Heart rate', system: 'Vital Signs'});

// ============================================================================
// 5. CPT (Current Procedural Terminology)
// ============================================================================
CREATE CONSTRAINT cpt_code IF NOT EXISTS FOR (c:CPT) REQUIRE c.code IS UNIQUE;
CREATE INDEX cpt_description IF NOT EXISTS FOR (c:CPT) ON (c.description);

CREATE (:CPT {code: '99213', description: 'Office/outpatient visit, established patient, moderate complexity', category: 'E&M'});
CREATE (:CPT {code: '93000', description: 'Electrocardiogram, complete', category: 'Cardiovascular'});
CREATE (:CPT {code: '80053', description: 'Comprehensive metabolic panel', category: 'Laboratory'});

// ============================================================================
// 6. HPO (Human Phenotype Ontology)
// ============================================================================
CREATE CONSTRAINT hpo_id IF NOT EXISTS FOR (h:HPO) REQUIRE h.id IS UNIQUE;
CREATE INDEX hpo_name IF NOT EXISTS FOR (h:HPO) ON (h.name);

CREATE (:HPO {id: 'HP:0000819', name: 'Diabetes mellitus', category: 'Phenotypic abnormality'});
CREATE (:HPO {id: 'HP:0000822', name: 'Hypertension', category: 'Phenotypic abnormality'});
CREATE (:HPO {id: 'HP:0001658', name: 'Myocardial infarction', category: 'Phenotypic abnormality'});

// ============================================================================
// 7. MeSH (Medical Subject Headings)
// ============================================================================
CREATE CONSTRAINT mesh_id IF NOT EXISTS FOR (m:MeSH) REQUIRE m.id IS UNIQUE;
CREATE INDEX mesh_term IF NOT EXISTS FOR (m:MeSH) ON (m.term);

CREATE (:MeSH {id: 'D003920', term: 'Diabetes Mellitus', tree_number: 'C18.452.394.750'});
CREATE (:MeSH {id: 'D006973', term: 'Hypertension', tree_number: 'C14.907.489'});
CREATE (:MeSH {id: 'D009203', term: 'Myocardial Infarction', tree_number: 'C14.280.647.500'});

// ============================================================================
// 8. UMLS (Unified Medical Language System)
// ============================================================================
CREATE CONSTRAINT umls_cui IF NOT EXISTS FOR (u:UMLS) REQUIRE u.cui IS UNIQUE;
CREATE INDEX umls_concept IF NOT EXISTS FOR (u:UMLS) ON (u.concept_name);

CREATE (:UMLS {cui: 'C0011849', concept_name: 'Diabetes Mellitus', semantic_type: 'Disease or Syndrome'});
CREATE (:UMLS {cui: 'C0020538', concept_name: 'Hypertensive disease', semantic_type: 'Disease or Syndrome'});
CREATE (:UMLS {cui: 'C0027051', concept_name: 'Myocardial Infarction', semantic_type: 'Disease or Syndrome'});

// ============================================================================
// 9. ATC (Anatomical Therapeutic Chemical Classification)
// ============================================================================
CREATE CONSTRAINT atc_code IF NOT EXISTS FOR (a:ATC) REQUIRE a.code IS UNIQUE;
CREATE INDEX atc_name IF NOT EXISTS FOR (a:ATC) ON (a.name);

CREATE (:ATC {code: 'A10BA02', name: 'Metformin', level: 5, category: 'Alimentary tract and metabolism'});
CREATE (:ATC {code: 'C09AA03', name: 'Lisinopril', level: 5, category: 'Cardiovascular system'});
CREATE (:ATC {code: 'B01AC06', name: 'Aspirin', level: 5, category: 'Blood and blood forming organs'});

// ============================================================================
// 10. OMIM (Online Mendelian Inheritance in Man)
// ============================================================================
CREATE CONSTRAINT omim_id IF NOT EXISTS FOR (o:OMIM) REQUIRE o.mim_number IS UNIQUE;
CREATE INDEX omim_title IF NOT EXISTS FOR (o:OMIM) ON (o.title);

CREATE (:OMIM {mim_number: '125853', title: 'Diabetes mellitus, type 2', phenotype: true});
CREATE (:OMIM {mim_number: '145500', title: 'Hypertension, essential', phenotype: true});
CREATE (:OMIM {mim_number: '608446', title: 'Myocardial infarction, susceptibility to', phenotype: true});

// ============================================================================
// 11. GO (Gene Ontology)
// ============================================================================
CREATE CONSTRAINT go_id IF NOT EXISTS FOR (g:GO) REQUIRE g.id IS UNIQUE;
CREATE INDEX go_name IF NOT EXISTS FOR (g:GO) ON (g.name);

CREATE (:GO {id: 'GO:0006006', name: 'glucose metabolic process', aspect: 'biological_process'});
CREATE (:GO {id: 'GO:0008217', name: 'regulation of blood pressure', aspect: 'biological_process'});
CREATE (:GO {id: 'GO:0010976', name: 'positive regulation of neuron projection development', aspect: 'biological_process'});

// ============================================================================
// 12. NDC (National Drug Code)
// ============================================================================
CREATE CONSTRAINT ndc_code IF NOT EXISTS FOR (n:NDC) REQUIRE n.code IS UNIQUE;
CREATE INDEX ndc_name IF NOT EXISTS FOR (n:NDC) ON (n.proprietary_name);

CREATE (:NDC {code: '00093-1073', proprietary_name: 'Metformin Hydrochloride', labeler: 'Teva'});
CREATE (:NDC {code: '00093-2063', proprietary_name: 'Lisinopril', labeler: 'Teva'});
CREATE (:NDC {code: '00904-2013', proprietary_name: 'Aspirin', labeler: 'Major'});

// ============================================================================
// 13. RadLex (Radiology Lexicon)
// ============================================================================
CREATE CONSTRAINT radlex_id IF NOT EXISTS FOR (r:RadLex) REQUIRE r.id IS UNIQUE;
CREATE INDEX radlex_name IF NOT EXISTS FOR (r:RadLex) ON (r.name);

CREATE (:RadLex {id: 'RID38780', name: 'chest X-ray', modality: 'radiography'});
CREATE (:RadLex {id: 'RID10312', name: 'cardiac MRI', modality: 'MRI'});
CREATE (:RadLex {id: 'RID38766', name: 'CT angiography', modality: 'CT'});

// ============================================================================
// CROSS-ONTOLOGY RELATIONSHIPS
// ============================================================================

// Link SNOMED to ICD-10
MATCH (s:SNOMED {code: '73211009'}), (i:ICD10 {code: 'E11'})
CREATE (s)-[:MAPS_TO]->(i);

MATCH (s:SNOMED {code: '38341003'}), (i:ICD10 {code: 'I10'})
CREATE (s)-[:MAPS_TO]->(i);

// Link ICD-10 to RxNorm (common treatments)
MATCH (i:ICD10 {code: 'E11'}), (r:RxNorm {name: 'Metformin'})
CREATE (i)-[:TREATS_WITH]->(r);

MATCH (i:ICD10 {code: 'I10'}), (r:RxNorm {name: 'Lisinopril'})
CREATE (i)-[:TREATS_WITH]->(r);

// Link SNOMED to LOINC (diagnostic tests)
MATCH (s:SNOMED {code: '73211009'}), (l:LOINC {code: '2339-0'})
CREATE (s)-[:DIAGNOSED_BY]->(l);

// Link to UMLS (integration hub)
MATCH (s:SNOMED {code: '73211009'}), (u:UMLS {cui: 'C0011849'})
CREATE (s)-[:EQUIVALENT_TO]->(u);

MATCH (i:ICD10 {code: 'E11'}), (u:UMLS {cui: 'C0011849'})
CREATE (i)-[:EQUIVALENT_TO]->(u);

// ============================================================================
// VERIFICATION QUERIES
// ============================================================================

// Count nodes by ontology
MATCH (n)
WITH labels(n)[0] AS ontology, count(*) AS count
WHERE ontology IN ['SNOMED', 'ICD10', 'RxNorm', 'LOINC', 'CPT', 'HPO', 'MeSH', 'UMLS', 'ATC', 'OMIM', 'GO', 'NDC', 'RadLex']
RETURN ontology, count
ORDER BY count DESC;

// Count relationships
MATCH ()-[r]->()
RETURN type(r) AS relationship_type, count(*) AS count
ORDER BY count DESC;

// ============================================================================
// SETUP COMPLETE
// ============================================================================
// 13 Medical Ontologies Integrated
// Foundation ready for Phase 1: RAG Heat System
// ============================================================================
