<?php

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 {nodes} (nid), -- node-type: jsdoc_version
        project integer NOT NULL REFERENCES {nodes} (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 {nodes} (nid), -- node-type: jsdoc_resource
        version integer NOT NULL REFERENCES {nodes} (nid), -- node-type: jsdoc_version
        used boolean NOT NULL DEFAULT 1,
        new boolean NOT NULL DEFAULT 1
      ) /*!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 {nodes} (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 {nodes} (nid), -- node-type: jsdoc_resource
        private bool NOT NULL DEFAULT 0,
        private_parent bool NOT NULL DEFAULT 0,
        version integer NOT NULL REFERENCES {nodes} (nid), -- node-type: jsdoc_version
        PRIMARY KEY (vid, 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 REFERENCES {node_revisions} (vid),
        nid integer NOT NULL REFERENCES {nodes} (nid), -- node-type: jsdoc_object
        resource_vid integer NOT NULL REFERENCES {node_revisions} (vid),
        resource_nid integer NOT NULL REFERENCES {nodes} (nid), -- node-type: jsdoc_resource
        provide_vid integer NOT NULL REFERENCES {node_revisions} (vid),
        provide_nid integer NOT NULL REFERENCES {nodes} (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,
        source longtext NOT NULL,
        private boolean NOT NULL DEFAULT 0,
        private_parent bool NOT NULL DEFAULT 0,
        version integer NOT NULL REFERENCES {nodes} (nid), -- node-type: jsdoc_version
        used boolean NOT NULL DEFAULT 1,
        new boolean NOT NULL DEFAULT 1,
        PRIMARY KEY (vid, resource_vid),
        INDEX (version) 
      ) /*!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 {nodes} (nid), -- node-type: jsdoc_variable
        parent_vid integer NOT NULL REFERENCES {node_revisions} (vid),
        parent_nid integer NOT NULL REFERENCES {nodes} (vid), -- node-type: jsdoc_variable
        type enum('normal', 'prototype', 'instance', 'alias', 'chain'), -- The type of join
        subtype enum('call', 'prototype', '') DEFAULT '', -- The subtype of the join
        version integer NOT NULL REFERENCES {nodes} (nid), -- node-type: jsdoc_version
        PRIMARY KEY (vid, parent_vid, type, subtype, 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 {nodes} (nid), -- node-type: jsdoc_resource
        parent_vid integer NOT NULL REFERENCES {node_revisions} (vid),
        parent_nid integer NOT NULL REFERENCES {nodes} (vid), -- node-type: jsdoc_resource
        tid integer REFERENCES {term_data} (tid), -- We use this for saying how a resource joins to a resource
        version integer NOT NULL REFERENCES {nodes} (nid), -- node-type: jsdoc_version
        PRIMARY KEY (vid, parent_vid, tid, version),
        INDEX (parent_vid),
        INDEX (tid)
      ) /*!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 {nodes} (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)
      ) /*!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 {nodes} (nid), -- node-type: jsdoc_object
        weight integer NOT NULL,
        example longtext NOT NULL,
      PRIMARY KEY (vid, weight)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
    break;
  case 'pgsql':
    break;
  }
}