<?php

function jsdoc_update_1() {
  $items = array();
  $items[] = update_sql('ALTER TABLE {jsdoc_resource_hierarchy} ADD project integer');
  $items[] = update_sql('ALTER TABLE {jsdoc_objects} ADD INDEX (new)');
  $items[] = update_sql('ALTER TABLE {jsdoc_parameters} ADD INDEX (vid)');
  $items[] = update_sql('ALTER TABLE {jsdoc_examples} ADD INDEX (vid)');
  $items[] = update_sql('ALTER TABLE {jsdoc_resources} ADD INDEX (version)');
  $items[] = update_sql('ALTER TABLE {jsdoc_variables} ADD INDEX (version)');
  $items[] = update_sql('ALTER TABLE {jsdoc_variable_hierarchy} ADD INDEX (version)');
  $items[] = update_sql('ALTER TABLE {jsdoc_resource_hierarchy} ADD INDEX (version)');
  return $items;
}

function jsdoc_update_2() {
  $items = array();
  $items[] = update_sql('ALTER TABLE {jsdoc_objects} ADD INDEX(nid)');
  $items[] = update_sql('ALTER TABLE {jsdoc_variables} ADD INDEX(nid)');
  return $items;
}

function jsdoc_install() {
  foreach (taxonomy_get_vocabularies('jsdoc') as $vocabulary) {
    if ($vocabulary->name == 'Environments') {
      $environment_vocabulary = $vocabulary;
    }
    elseif ($vocabulary->name == 'Conditions') {
      $condition_vocabulary = $vocabulary;
    }
    elseif ($vocabulary->name == 'Flags') {
      $flag_vocabulary = $vocabulary;
    }
  }
  if (!$environment_vocabulary) {
    $edit = array(
      'name' => 'Environments',
      'description' => 'Environment we are running in (one at a time can be selected). Build using a hierarchy e.g. common -> browser -> dashboard where each loads on top of the code for the parent environments.',
      'help' => '',
      'multiple' => false,
      'required' => false,
      'hierarchy' => '2',
      'relations' => false,
      'tags' => false,
      'weight' => '0',
      'nodes' => array('jsdoc_object' => true)
    );
    taxonomy_save_vocabulary($edit);
    $environment_vocabulary = taxonomy_get_vocabulary($edit['vid']);
  }
  if (!$condition_vocabulary) {
    $edit = array(
      'name' => 'Conditions',
      'description' => 'Envrionmental conditions, such as svg or vml (many can be selected)',
      'help' => '',
      'multiple' => false,
      'required' => false,
      'hierarchy' => '0',
      'relations' => false,
      'tags' => false,
      'weight' => '0',
      'nodes' => array('jsdoc_object' => true)
    );
    taxonomy_save_vocabulary($edit);
    $condition_vocabulary = taxonomy_get_vocabulary($edit['vid']);
  }
  if (!$flag_vocabulary) {
    $edit = array(
      'name' => 'Flags',
      'description' => 'Allow users to quickly mark their opinion of the page\' documentation',
      'help' => '',
      'multiple' => true,
      'required' => false,
      'hierarchy' => '0',
      'relations' => false,
      'tags' => false,
      'weight' => '0',
      'nodes' => array('jsdoc_object' => true)
    );
    taxonomy_save_vocabulary($edit);
    $flag_vocabulary = taxonomy_get_vocabulary($edit['vid']);
  }

  switch ($GLOBALS['db_type']) {
  case 'mysql':
  case 'mysqli':
      // jsdoc_resources is used for referencing files
      db_query("CREATE TABLE {jsdoc_cache} (
        namespace varchar(31) NOT NULL,
        filepath varchar(255) NOT NULL,
        cid int NOT NULL,
        ctime int NOT NULL,
        used boolean NOT NULL DEFAULT 1,
        PRIMARY KEY (namespace, filepath)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      // jsdoc_versions is used for storing what project a version is used in
      db_query("CREATE TABLE {jsdoc_versions} (
        nid integer NOT NULL PRIMARY KEY REFERENCES {node} (nid), -- node-type: jsdoc_version
        project integer NOT NULL REFERENCES {node} (nid) --  node-type: jsdoc_project
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      // jsdoc_resources is used for referencing files
      db_query("CREATE TABLE {jsdoc_resources} (
        vid integer NOT NULL PRIMARY KEY REFERENCES {node_revisions} (vid),
        nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_resource
        project integer NULL REFERENCES {node} (nid), -- note-type: jsdoc_project
        version integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_version
        used boolean NOT NULL DEFAULT 1,
        new boolean NOT NULL DEFAULT 1,
        INDEX (used),
        INDEX (version)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      // jsdoc_variables is used for disambiguation
      // There should be one of these for each variable for each version
      db_query("CREATE TABLE {jsdoc_variables} (
        vid integer NOT NULL REFERENCES {node_revisions} (vid),
        nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_variable
        resource_vid integer NOT NULL REFERENCES {node_revisions} (vid), -- Give a specific resource priority
        resource_nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_resource
        private bool NOT NULL DEFAULT 0,
        private_parent bool NOT NULL DEFAULT 0,
        version integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_version
        PRIMARY KEY (vid, version),
        INDEX (version)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      // jsdoc_objects is used for specific objects
      // Note: resource is what resource the object actually exists in
      //    while bundle is what resource the object gets bundled
      db_query("CREATE TABLE {jsdoc_objects} (
        vid integer NOT NULL PRIMARY KEY REFERENCES {node_revisions} (vid),
        nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_object
        resource_vid integer NOT NULL REFERENCES {node_revisions} (vid),
        resource_nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_resource
        provide_vid integer NOT NULL REFERENCES {node_revisions} (vid),
        provide_nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_resource
        initialized bool NOT NULL, -- Whether or not this function has 'new' in front of it during declaration
        classlike bool NOT NULL DEFAULT 0, -- Whether or not this function acts as a class
        type varchar(255) NOT NULL,
        updating bool NOT NULL DEFAULT 1, -- Flag for update the next go around
        returns varchar(255) NOT NULL,
        return_summary longtext NOT NULL,
        source longtext NOT NULL,
        private boolean NOT NULL DEFAULT 0,
        private_parent bool NOT NULL DEFAULT 0,
        version integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_version
        used boolean NOT NULL DEFAULT 1,
        new boolean NOT NULL DEFAULT 1,
        UNIQUE (vid, resource_vid),
        INDEX (resource_vid),
        INDEX (provide_vid),
        INDEX (version),
        INDEX (used),
        INDEX (new)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      // jsdoc_variable_hierarchy is for showing how variables are associated with each other
      // There should be one of these for each type of join, for each variable pair, for each version
      db_query("CREATE TABLE {jsdoc_variable_hierarchy} (
        vid integer NOT NULL REFERENCES {node_revisions} (vid),
        nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_object
        parent_vid integer NOT NULL REFERENCES {node_revisions} (vid),
        parent_nid integer NOT NULL REFERENCES {node} (vid), -- node-type: jsdoc_variable
        type enum('normal', 'cascading', 'prototype', 'instance', 'alias', 'chain', 'mixin'), -- The type of join
        subtype enum('call', 'prototype', '') DEFAULT '', -- The subtype of the join
        version integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_version
        PRIMARY KEY (vid, parent_vid, type, subtype, version),
        INDEX (parent_vid),
        INDEX (version)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      // jsdoc_resource_hierarchy is for showing which resources require which resources
      // There should be on of these for each type of join, for each resource pair, for each version 
      db_query("CREATE TABLE {jsdoc_resource_hierarchy} (
        vid integer NOT NULL REFERENCES {node_revisions} (vid),
        nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_resource
        parent_vid integer NOT NULL REFERENCES {node_revisions} (vid),
        parent_nid integer NOT NULL REFERENCES {node} (vid), -- node-type: jsdoc_resource
        project integer REFERENCES {node} (nid), -- node-type: jsdoc_project
        tid integer REFERENCES {term_data} (tid), -- We use this for saying how a resource joins to a resource
        version integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_version
        PRIMARY KEY (vid, parent_vid, tid, version),
        INDEX (parent_vid),
        INDEX (tid),
        INDEX (version)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      // jsdoc_parameters is for showing which parameters are part of which object (assume the object is a function)
      // We need to have a set of these for every single object, to keep track of changes
      db_query("CREATE TABLE {jsdoc_parameters} (
        vid integer NOT NULL REFERENCES {node_revisions} (vid),
        nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_object
        weight integer NOT NULL,
        name varchar(255) NOT NULL,
        type varchar(255) NOT NULL,
        summary longtext NOT NULL,
        optional bool NOT NULL,
        repeating bool NOT NULL,
        PRIMARY KEY (vid, name),
        INDEX (vid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

      // jsdoc_examples is for associating examples with objects
      db_query("CREATE TABLE {jsdoc_examples} (
        vid integer NOT NULL REFERENCES {node_revisions} (vid),
        nid integer NOT NULL REFERENCES {node} (nid), -- node-type: jsdoc_object
        weight integer NOT NULL,
        example longtext NOT NULL,
        markedup longtext NOT NULL,
        PRIMARY KEY (vid, weight),
        INDEX (vid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
    break;
  case 'pgsql':
    break;
  }
}