<?php

/**
 * @file
 * Install, update and uninstall functions for the wishlist module.
 *
 */



/**
 * Build out the necessary tables for the wishlist module
 */
function wishlist_install() {
  drupal_set_message(t('Installing wishlist'));
} 

function wishlist_enable() {
  // Ensure the wishlist node type is available.
  node_types_rebuild();
  $types = node_type_get_types();
  // Add a body type field to the wishlist nodes
  node_add_body_field($types['wishlist']);  
}

/**
 * @desc Defines the schema for the wishlist module's tables.
 *
 * Table wishlist holds the node specific fields.
 * Table wishlist_purchased holds the purchase records for a node.
 */
function wishlist_schema() {
  
  $schema['wishlist'] = array(
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Foreign key to the node table.',
      ),
      'item_is_public' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 1,
        'description' => 'Is this item public, or is it maintained on the user\'s private list',
      ),
      'item_est_cost' => array(
        'type' => 'int',
        'not null' => FALSE,
        'disp-width' => '11',
        'description' => 'The estimated cost for the item',
      ),
      'item_url1' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'A URL from which the item can be purchased',
      ),
      'item_url2' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'A URL from which the item can be purchased',
      ),
      'item_quantity_requested' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 1,
        'description' => 'How many of this item does the user want?',
      ),
      'item_currency' => array(
        'type' => 'varchar',
        'length' => 4,
        'not null' => TRUE,
        'default' => 'USD',
        'description' => 'What currency is the estimated cost in',
      ),
      'item_priority' => array(
        'type' => 'int',
        'size' => 'tiny',
        'default' => 3,
        'not null' => FALSE,
        'description' => 'How important is this item to the user?',
      ),
    ),
    'primary key' => array('nid'),
    'description' => 'Holds the details of a wishlist node',
  );

  $schema['wishlist_purchased'] = array(
    'fields' => array(
      'wishlist_purch_wid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Unique ID for this purchase record',
      ),
      'wishlist_purch_nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Foreign key to the wishlist table and node table',
      ),
      'wishlist_purch_buyer_uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'UID of the user that purchased the item',
      ),
      'wishlist_purch_quantity' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 1,
        'description' => 'How many this user purchased',
      ),
      'purch_date' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The date of the purchase',
      ),
    ),
    'primary key' => array('wishlist_purch_wid'),
    'description' => 'A record of an item purchased off of a wishlist',
  );

  return $schema;
}


