<?php

/**
 * Implementation of hook_schema()
 */
function mytinytodo_schema() {
  $schema['mytinytodo_fields'] = array(
      'fields' => array(
          'id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
          'entity_type' => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE),
          'entity_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'delta' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
      ),
      'primary key' => array('id'),
      'unique keys' => array(
          'entity_field' => array('entity_type', 'entity_id', 'delta'),
      ),
  );

  $schema['mytinytodo_lists'] = array(
      'fields' => array(
          'id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
          'field_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'uuid' => array('type' => 'varchar', 'length' => 36, 'not null' => TRUE),
          'ow' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
          'name' => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE),
          'd_created' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'd_edited' => array('type' => 'int', 'unsigned' => TRUE, 'default' => 0),
          'sorting' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'published' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'taskview' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'settings' => array('type' => 'varchar', 'length' => 400, 'not null' => FALSE),
      ),
      'primary key' => array('id'),
      'unique keys' => array(
          'uuid' => array('uuid'),
      ),
  );

  $schema['mytinytodo_todos'] = array(
      'fields' => array(
          'id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
          'uuid' => array('type' => 'varchar', 'length' => 36, 'not null' => TRUE),
          'list_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'd_created' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'd_completed' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'd_edited' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'compl' => array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
          'title' => array('type' => 'varchar', 'length' => 250, 'not null' => TRUE),
          'note' => array('type' => 'text'),
          'prio' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0),
          'ow' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
          'tags' => array('type' => 'varchar', 'length' => 600, 'not null' => TRUE),
          'tags_ids' => array('type' => 'varchar', 'length' => 250, 'not null' => TRUE),
          'duedate' => array('type' => 'varchar', 'length' => 10),
      ),
      'primary key' => array('id'),
      'unique keys' => array(
          'uuid' => array('uuid'),
      ),
      'indexes' => array(
              'list_id' => array('list_id')
          ),
  );

  $schema['mytinytodo_tags'] = array(
      'fields' => array(
          'id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
          'name' => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE),
      ),
      'primary key' =>  array('id'),
      'unique keys' => array(
          'name' => array('name'),
      ),
  );

  $schema['mytinytodo_tag2task'] = array(
      'fields' => array(
          'tag_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
          'task_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
          'list_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE),
      ),
      'indexes' => array(
          'tag_id' => array('tag_id'),
          'task_id' => array('task_id'),
          'list_id' => array('list_id'),
      ),
  );

  return $schema;
}

/**
 * Implementation of hook_install()
 */
function mytinytodo_install() {
}

/**
 * Implementation of hook_uninstall()
 */
function mytinytodo_uninstall() {
    variable_del('mytinytodo_enable_default_content');
}

/** 
 * Change the duedate column to varchar
 */
function mytinytodo_update_7001(&$sandbox) {
    db_change_field('mytinytodo_todolist', 'duedate', 'duedate', array('type' => 'varchar', 'length' => 10));
}

/** 
 * Create new fields table
 */
function mytinytodo_update_7002(&$sandbox) {
    // Create the new fields table
    $schema = mytinytodo_schema();
    if (!db_table_exists('mytinytodo_fields'))
        db_create_table('mytinytodo_fields', $schema['mytinytodo_fields']);

    // Add a new field_id column
    db_add_field('mytinytodo_lists', 'field_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));

    // Populate the new mytinytodo_fields table
    $results = db_query("SELECT DISTINCT entity_id, delta FROM {mytinytodo_lists}");
    foreach($results as $result) {
        db_insert("mytinytodo_fields")
            ->fields(array(
                'entity_type' => 'node',
                'entity_id' => $result->entity_id,
                'delta' => $result->delta,
            ))
            ->execute();
    }

    // Populate the new mytinytodo_lists.field_id field
    $results = db_query("SELECT id, entity_id, delta FROM {mytinytodo_fields}");
    foreach($results as $result) {
        db_update("mytinytodo_lists")
            ->fields(array(
                'field_id' => $result->id,
            ))
            ->condition('entity_id', $result->entity_id)
            ->condition('delta', $result->delta)
            ->execute();
    }

    // Remove the old mytinytodo_lists.entity_id column
    db_drop_field('mytinytodo_lists', 'entity_id');

    // Remove the old mytinytodo_lists.delta column
    db_drop_field('mytinytodo_lists', 'delta');

    // Rename the mytinytodo_todolist table to mytinytodo_todos
    db_rename_table('mytinytodo_todolist', 'mytinytodo_todos');
}
