<?php

/**
 * Create the schema.
 *
 * @return array
 */
function tripal_jbrowse_mgmt_schema() {
  $schema = [];

  $schema['tripal_jbrowse_mgmt_instances'] = [
    'description' => 'JBrowse instances.',
    'fields' => [
      'id' => [
        'type' => 'serial',
        'description' => 'Primary key',
        'not null' => TRUE,
      ],
      'uid' => [
        'type' => 'int',
        'description' => 'Submitter\'s User id',
        'not null' => TRUE,
      ],
      'organism_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'analysis_id' => [
        'type' => 'int',
        'not null' => FALSE,
      ],
      'title' => [
        'type' => 'varchar',
        'length' => 255,
      ],
      'description' => [
        'type' => 'text',
        'not null' => FALSE,
      ],
      'file' => [
        'type' => 'text',
        'not null' => FALSE,
      ],
      'created_at' => [
        'type' => 'int',
        'not null' => 'true',
      ],
    ],
    'primary key' => [
      'id',
    ],
  ];

  $schema['tripal_jbrowse_mgmt_tracks'] = [
    'description' => 'JBrowse tracks.',
    'fields' => [
      'id' => [
        'type' => 'serial',
        'description' => 'Primary key',
        'not null' => TRUE,
      ],
      'uid' => [
        'type' => 'int',
        'description' => 'Submitter\'s User id',
        'not null' => TRUE,
      ],
      'instance_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'organism_id' => [
        'type' => 'int',
        'not null' => FALSE,
      ],
      'label' => [
        'type' => 'varchar',
        'length' => 255,
      ],
      'track_type' => [
        'type' => 'varchar',
        'length' => 255,
      ],
      'file_type' => [
        'type' => 'varchar',
        'length' => 255,
      ],
      'file' => [
        'type' => 'text',
      ],
      'created_at' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'is_deleted' => [
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
      ],
    ],
    'primary key' => [
      'id',
    ],
  ];

  $schema['tripal_jbrowse_mgmt_instanceprop'] = [
    'description' => 'JBrowse Instance Metadata.',
    'fields' => [
      'instance_id' => [
        'type' => 'int',
        'not null' => TRUE,
      ],
      'property_type' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'value' => [
        'type' => 'text',
      ],
    ]
  ];

  return $schema;
}

/**
 * Create instance metadata table.
 */
function tripal_jbrowse_mgmt_update_7001(&$sandbox) {
  $schema = tripal_jbrowse_mgmt_schema();
  $name = "tripal_jbrowse_mgmt_instanceprop";
  $table = $schema[$name];
  db_create_table($name, $table);
}

/**
 * adding a new column analysis_id to table tripal_jbrowse_mgmt_instances
 */
function tripal_jbrowse_mgmt_update_7002(&$sandbox){
  $new_col_spec = [
    'type' => 'int',
    'not null' => FALSE,
  ];
  $schema = Database::getConnection()->schema();
  $schema->addField('tripal_jbrowse_mgmt_instances', 'analysis_id', $new_col_spec);
}