-- ==============================================================================
-- SNOMED CT vaccine codes  --  REVIEW BEFORE RUNNING
-- ==============================================================================
--
-- READ THIS FIRST.
--
-- Each code below is published over ABDM as Immunization.vaccineCode: it is the
-- machine-readable identity of the vaccine the child received, and every other
-- hospital on the network decodes the child's vaccination history from it rather
-- than from our free-text name. A wrong code does not show up as a wrong label
-- on our own screens -- it silently tells another clinic that a different
-- vaccine was given.
--
-- These mappings are therefore supplied as a SEPARATE, OPT-IN file and are NOT
-- applied by immunization_02_seed.sql. They are best-effort mappings against the
-- SNOMED CT International "Vaccine product" hierarchy and have NOT been verified
-- against the SNOMED CT India Edition release your ABDM deployment is certified
-- against.
--
-- Before running:
--   1. Check each code in the SNOMED CT browser (https://browser.ihtsdotools.org)
--      or the NRCeS/ABDM terminology service, against the India Edition.
--   2. Delete or correct any row you cannot confirm.
--
-- Leaving a vaccine uncoded is SAFE and supported: the payload sends the name
-- with a null code, the FHIR bundle falls back to vaccineCode.text, and the
-- vaccine master screen shows a "Not coded" badge so the gap stays visible.
-- An uncoded vaccine is a smaller problem than a wrongly coded one.
--
-- Codes are matched by name, so this is safe to re-run and independent of ids.
-- ==============================================================================

UPDATE `immunization_vaccines` SET `snomed_code` = '2171000221104' WHERE `name` = 'BCG'                       AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '333598008'     WHERE `name` = 'OPV'                       AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '396456003'     WHERE `name` = 'Hepatitis B'               AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '350327004'     WHERE `name` = 'DTwP/DTaP'                 AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '396435000'     WHERE `name` = 'IPV'                       AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '415354003'     WHERE `name` = 'Rotavirus'                 AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '407746005'     WHERE `name` = 'PCV'                       AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '396041000'     WHERE `name` = 'Influenza'                 AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '412375000'     WHERE `name` = 'Typhoid Conjugate Vaccine' AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '396000003'     WHERE `name` = 'MMR'                       AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '412370000'     WHERE `name` = 'Meningococcal (MCV)'       AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '396422003'     WHERE `name` = 'Hepatitis A'               AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '407737004'     WHERE `name` = 'JE'                        AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '407746005'     WHERE `name` = 'Varicella'                 AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '428570002'     WHERE `name` = 'HPV'                       AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '407744008'     WHERE `name` = 'Tdap'                      AND `snomed_code` IS NULL;
UPDATE `immunization_vaccines` SET `snomed_code` = '412375000'     WHERE `name` = 'Td'                        AND `snomed_code` IS NULL;

-- Review aid: what is still uncoded after running this.
-- SELECT id, name, snomed_code FROM immunization_vaccines WHERE deleted_at IS NULL ORDER BY snomed_code IS NOT NULL, name;
