<?php

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

/**
 * Implements hook_schema().
 */
function uc_product_schema() {
  $schema = array();

  $schema['uc_product_features'] = array(
    'description' => 'Stores information of features added to products.',
    'fields' => array(
      'pfid' => array(
        'description' => 'Primary key: the product feature id.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'description' => 'The {node}.nid of the product that has this feature.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'fid' => array(
        'description' => 'The type of feature.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'The description of the feature.',
        'type' => 'text',
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
    ),
    'primary key' => array('pfid'),
    'foreign keys' => array(
      'uc_product' => array(
        'table' => 'uc_product',
        'columns' => array('nid' => 'nid'),
      ),
    ),
  );

  $schema['uc_products'] = array(
    'description' => 'Product information for nodes.',
    'fields' => array(
      'vid' => array(
        'description' => 'The {node}.vid of the product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {node}.nid of the product.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'model' => array(
        'description' => 'SKU or model number.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'cost' => array(
        'description' => 'The amount the store pays to sell the product.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'price' => array(
        'description' => 'The amount the customer pays for the product.',
        'type' => 'numeric',
        'precision' => 16,
        'scale' => 5,
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'weight' => array(
        'description' => 'Physical weight.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'weight_units' => array(
        'description' => 'Unit of measure for the weight field.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'lb',
      ),
      'length' => array(
        'description' => 'Physical length of the product or its packaging.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'width' => array(
        'description' => 'Physical width of the product or its packaging.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'height' => array(
        'description' => 'Physical height of the product or its packaging.',
        'type' => 'float',
        'not null' => TRUE,
        'default' => 0.0,
      ),
      'length_units' => array(
        'description' => 'Unit of measure for the length, width, and height.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'in',
      ),
      'pkg_qty' => array(
        'description' => 'The number of this product that fit in one package.',
        'type' => 'int',
        'size' => 'small',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
      'default_qty' => array(
        'description' => 'The default value for the quantity field in the "Add to Cart" form.',
        'type' => 'int',
        'size' => 'small',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
      'shippable' => array(
        'description' => 'Boolean flag signifying that the product can be shipped.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
    ),
    'primary key' => array('vid'),
    'foreign keys' => array(
      'node' => array(
        'table' => 'node',
        'columns' => array(
          'nid' => 'nid',
          'vid' => 'vid',
        ),
      ),
    ),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function uc_product_uninstall() {
  $styles = \Drupal\image\Entity\ImageStyle::loadMultiple(['uc_product', 'uc_thumbnail']);
  if (!empty($styles)) {
    $style_names = array();
    foreach ($styles as $style) {
      $style_names[] = $style->label();
    }
    drupal_set_message(\Drupal::translation()->formatPlural(count($style_names),
      'The image style %style has not been deleted. If you need to delete it, <a href=":url">please do so manually</a>.',
      'The image styles %styles have not been deleted. If you need to delete them, <a href=":url">please do so manually</a>.',
      array(
        '%style' => $style_names[0],
        '%styles' => implode(', ', $style_names),
        ':url' => \Drupal\Core\Url::fromRoute('entity.image_style.collection')->toString(),
      )
    ), 'warning');
  }
}
