USE provisioning;

CREATE TABLE `voip_subscriber_profile_sets` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `reseller_id` int(11) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `vsp_resname_idx` (`reseller_id`,`name`)
) ENGINE=InnoDB;

CREATE TABLE `voip_subscriber_profiles` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `set_id` int(11) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  `set_default` tinyint(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `set_name_idx` (`set_id`,`name`),
  CONSTRAINT `voip_subscriber_profile_sets_ibfk_1` FOREIGN KEY (`set_id`) REFERENCES `voip_subscriber_profile_sets` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

CREATE TABLE `voip_subscriber_profile_attributes` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `profile_id` int(11) unsigned NOT NULL,
  `attribute_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `prof_attr_idx` (`profile_id`,`attribute_id`),
  KEY `attribute_id` (`attribute_id`),
  KEY `profile_idx` (`profile_id`),
  CONSTRAINT `voip_subscriber_profile_attributes_ibfk_1` FOREIGN KEY (`attribute_id`) REFERENCES `voip_preferences` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `voip_subscriber_profile_attributes_ibfk_2` FOREIGN KEY (`profile_id`) REFERENCES `voip_subscriber_profiles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

ALTER TABLE voip_subscribers
  ADD COLUMN pbx_hunt_policy enum('serial','parallel') DEFAULT NULL AFTER is_pbx_group,
  ADD COLUMN pbx_hunt_timeout int(4) unsigned DEFAULT NULL AFTER pbx_hunt_policy,
  ADD COLUMN pbx_extension varchar(255) DEFAULT NULL AFTER pbx_group_id,
  ADD COLUMN profile_set_id INT(11) UNSIGNED DEFAULT NULL AFTER pbx_extension,
  ADD COLUMN profile_id INT(11) UNSIGNED DEFAULT NULL AFTER profile_set_id;

-- we hardcoded moh to group 5 before, which is wrong in most cases
SELECT id INTO @mohvpid FROM voip_preference_groups WHERE name = 'NAT and Media Flow Control';

INSERT INTO voip_preferences VALUES
  (NULL, @mohvpid, 'music_on_hold', 'Music on Hold', 1, 1, 1, 0, 0, now(), 0, 1, 
  'boolean', 0, '"Music on Hold" - if set to true and a music on hold file is provided, a calling party gets that file played when put on hold'
  );
