USE provisioning;

ALTER TABLE voip_preferences_enum ADD COLUMN default_val BOOLEAN DEFAULT NULL;

DELIMITER |

-- update existing enum preferences if the value
-- changes in voip_preferences_enum.value
CREATE TRIGGER enum_update AFTER UPDATE ON voip_preferences_enum
FOR EACH ROW BEGIN
    UPDATE voip_usr_preferences SET value=NEW.value
    WHERE attribute_id=NEW.preference_id;
    UPDATE voip_dom_preferences SET value=NEW.value
    WHERE attribute_id=NEW.preference_id;
    UPDATE voip_peer_preferences SET value=NEW.value
    WHERE attribute_id=NEW.preference_id;
    UPDATE voip_preferences_enum SET default_val=NEW.value
    WHERE OLD.id=NEW.id AND default_val IS NOT NULL AND default_val=OLD.value;
END |

-- update preferences for existing domains / peer hosts /subscribers
-- when enum preferences are created that use a default value
-- (does not care about reseller_id)
CREATE TRIGGER enum_set_default AFTER INSERT ON voip_preferences_enum
FOR EACH ROW BEGIN
    IF (NEW.dom_pref=1 AND NEW.default_val = 1 AND NEW.value IS NOT NULL) THEN
        INSERT into voip_dom_preferences (domain_id, attribute_id, value)
            SELECT id, NEW.preference_id, NEW.value
            FROM voip_domains;
    ELSEIF (NEW.peer_pref=1 AND NEW.default_val = 1 AND NEW.value IS NOT NULL) THEN
        INSERT into voip_peer_preferences (peer_host_id, attribute_id, value)
            SELECT id, NEW.preference_id, NEW.value
            FROM voip_peer_hosts;
    ELSEIF (NEW.usr_pref=1 AND NEW.default_val = 1 AND NEW.value IS NOT NULL) THEN
        INSERT into voip_usr_preferences (subscriber_id, attribute_id, value)
            SELECT id, NEW.preference_id, NEW.value
            FROM voip_subscribers;
    END IF;
END |

-- set default values for domain preferences
-- when adding domains

DROP TRIGGER voip_dom_crepl_trig |

-- LOCK voip_domains !
CREATE TRIGGER voip_dom_crepl_trig AFTER INSERT ON voip_domains
FOR EACH ROW BEGIN
    -- old action_statement (2012-06-18)
    INSERT INTO kamailio.domain (domain) VALUES(NEW.domain);
   
    -- new stuff
    INSERT INTO voip_dom_preferences (domain_id, attribute_id, value)
    SELECT NEW.id, p.id, pe.value
    FROM voip_preferences p, voip_preferences_enum pe
    WHERE p.id=preference_id AND p.dom_pref=1 AND pe.dom_pref=1 AND pe.default_val=1 AND pe.value IS NOT NULL;
END |

-- set default values for peer preferences
-- when adding peer hosts

DROP TRIGGER voip_phost_crepl_trig |

CREATE TRIGGER voip_phost_crepl_trig AFTER INSERT ON voip_peer_hosts
FOR EACH ROW BEGIN
    -- old action_statement (2012-06-18)
    INSERT INTO kamailio.lcr_gw (lcr_id, gw_name, ip_addr, hostname, port, uri_scheme, transport, strip, flags, group_id)
                          VALUES(1, NEW.name, NEW.ip, NEW.host, NEW.port, 1, 1, 0, NEW.id, NEW.group_id);
  
    INSERT INTO kamailio.lcr_rule_target (lcr_id, rule_id, gw_id, priority, weight)
           SELECT rule.lcr_id, rule.id, LAST_INSERT_ID(), vpg.priority, NEW.weight
             FROM kamailio.lcr_rule rule
             INNER JOIN provisioning.voip_peer_groups vpg ON vpg.id = rule.group_id
            WHERE vpg.id = NEW.group_id;
    
    -- new stuff
    INSERT INTO voip_peer_preferences (peer_host_id, attribute_id, value)
    SELECT NEW.id, p.id, pe.value
    FROM voip_preferences p, voip_preferences_enum pe
    WHERE p.id=preference_id AND p.peer_pref=1 AND pe.peer_pref=1 AND pe.default_val=1 AND pe.value IS NOT NULL;
END |

DROP TRIGGER voip_sub_crepl_trig |
CREATE TRIGGER voip_sub_crepl_trig AFTER INSERT ON voip_subscribers
FOR EACH ROW BEGIN
    -- old action_statement (2012-06-18)
    DECLARE subscriber_domain varchar(127);
  
    SELECT domain INTO subscriber_domain FROM voip_domains where id = NEW.domain_id;
  
    INSERT INTO kamailio.subscriber (username, domain, uuid, password, datetime_created, ha1, ha1b)
                     VALUES(NEW.username, subscriber_domain, NEW.uuid, NEW.password, '0',
                            MD5(CONCAT(NEW.username, ':', subscriber_domain, ':', NEW.password)),
                            MD5(CONCAT(NEW.username, '@', subscriber_domain, ':', subscriber_domain, ':', NEW.password)));

    -- new stuff
    INSERT INTO voip_usr_preferences (subscriber_id, attribute_id, value)
    SELECT NEW.id, p.id, pe.value
    FROM voip_preferences p, voip_preferences_enum pe
    WHERE p.id=preference_id AND p.usr_pref=1 AND pe.usr_pref=1 AND pe.default_val=1 AND pe.value IS NOT NULL;
END |

DELIMITER ;

-- -- delete example
-- DELETE FROM voip_preferences_enum WHERE preference_id = (SELECT id FROM voip_preferences WHERE attribute='testpref');
-- DELETE FROM voip_preferences WHERE attribute='testpref';
-- -- example
-- INSERT INTO voip_preferences (attribute, type, max_occur, usr_pref, dom_pref, peer_pref, internal, data_type, read_only, description)
--     VALUES('testpref', 1, 1, 0, 1, 1, 0, 'enum', 0, 'Imagine some really useful information here');
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref'), 'domain default', 0, 0, 1, 0, 1);
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref'), 'peer default', 0, 0, 0, 1, 1);
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref'), 'forward', '1', 0, 1, 1, 0);
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref'), 'do not forward', '2', 0, 1, 1, 0);
-- 
-- -- delete example
-- DELETE FROM voip_preferences_enum WHERE preference_id = (SELECT id FROM voip_preferences WHERE attribute='testpref2');
-- DELETE FROM voip_preferences WHERE attribute='testpref2';
-- -- example
-- INSERT INTO voip_preferences (attribute, type, max_occur, usr_pref, dom_pref, peer_pref, internal, data_type, read_only, description)
--     VALUES('testpref2', 1, 1, 1, 1, 1, 0, 'enum', 0, 'Imagine some really useful information here');
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref2'), 'domain default', 0, 0, 1, 0, 1);
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref2'), 'peer default', 0, 0, 0, 1, 1);
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref2'), 'user default', 0, 1, 0, 0, 1);
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref2'), 'relay', '1', 1, 1, 1, 0);
-- INSERT INTO voip_preferences_enum (preference_id, label, value, usr_pref, dom_pref, peer_pref, default_val)
--     VALUES ((SELECT id FROM voip_preferences WHERE attribute = 'testpref2'), 'do not relay', '2', 1, 1, 1, 0);
