-- SQL statements to register the W-BDpT rule in the QCS database

-- Step 1: Add validation message for W-BDpT
INSERT INTO validationMessage(id,code,type,reference,description) VALUES
(119,'W-BDpT','WARNING','Other additional checks on the extent of the disease, page 33','pT + BoD not valid');

-- Step 2: Add rule definition (RuleDefinition ID = 216)
INSERT INTO ruledefinition(ID,NAME,DESCRIPTION,POSITIVE,CASESENSITIVITY,LOCATION,IMPLEMENTATION,VALIDATIONMESSAGE_ID,RULETYPE_ID) VALUES
(216,'Disease extent 3','Disease extent 3',true,'INSENSITIVE','eu.europa.ec.jrc.qcs.engine.rule.crossfield.encr2014','DiseaseExtentRule3',119,8);

-- Step 3: Add rule to protocols
INSERT INTO protocolToRule(id,protocol_id,priority,block,ruleDefinition_id,enabled) VALUES
(337,11,7,false,216,true);

-- Step 4: Rule-to-Field mappings

-- Protocol 11 (ENCR 2020): BoD=79, pT=95, Beh=82
INSERT INTO ruletofield(id,protocol_id,ruledefinition_id,target_id,position,key,field_id) VALUES ((SELECT next_val FROM ruleToField_SEQ),11,216,3,1,false,79); -- BoD (field_id=79)
UPDATE ruleToField_SEQ SET next_val = next_val + 1;
INSERT INTO ruletofield(id,protocol_id,ruledefinition_id,target_id,position,key,field_id) VALUES ((SELECT next_val FROM ruleToField_SEQ),11,216,3,2,false,95); -- pT (field_id=95)
UPDATE ruleToField_SEQ SET next_val = next_val + 1;
INSERT INTO ruletofield(id,protocol_id,ruledefinition_id,target_id,position,key,field_id) VALUES ((SELECT next_val FROM ruleToField_SEQ),11,216,3,3,false,82); -- Beh (field_id=82)
UPDATE ruleToField_SEQ SET next_val = next_val + 1;
