<?php

/**
 * @file
 * Set up source data and destination configuration for the migration example
 * module. We do this in a separate module so migrate_example itself is a pure
 * migration module.
 */

const TABLE_TAXONOMY_TERM = 'pe_migrate_taxonomy_term';
const TABLE_USER = 'pe_migrate_user';
const TABLE_PROFILE_MAIN = 'pe_migrate_profile_main';
const TABLE_PROFILE_ALUMNUS = 'pe_migrate_profile_alumnus';
const TABLE_PROFILE_STAFF = 'pe_migrate_profile_staff';
const TABLE_PROFILE_STUDENT = 'pe_migrate_profile_student';
const TABLE_NODE_ARTICLE = 'pe_migrate_node_article';
const TABLE_NODE_PAGE = 'pe_migrate_node_page';
const TABLE_NODE_ANNOUNCEMENT = 'pe_migrate_node_announcement';
const TABLE_NODE_ACAD_YEAR = 'pe_migrate_node_acad_year';
const TABLE_NODE_ACAD_TERM = 'pe_migrate_node_acad_term';
const TABLE_NODE_EDU = 'pe_migrate_node_edu';
const TABLE_NODE_SECTION = 'pe_migrate_node_section';
const TABLE_NODE_PROGRAM = 'pe_migrate_node_program';
const TABLE_NODE_COURSE = 'pe_migrate_node_course';
const TABLE_NODE_PROGRAM_COURSE = 'pe_migrate_node_program_course';
const TABLE_NODE_COURSE_USER = 'pe_migrate_node_course_user';


/**
 * Implements hook_schema.
 */
function pe_migrate_schema() {
  $schema['pe_migrate_taxonomy_term'] = pe_migrate_schema_taxonomy_term();
  $schema['pe_migrate_user'] = pe_migrate_schema_user();
  $schema['pe_migrate_profile_main'] = pe_migrate_schema_profile_main();
  $schema['pe_migrate_profile_alumnus'] = pe_migrate_schema_profile_alumnus();
  $schema['pe_migrate_profile_staff'] = pe_migrate_schema_profile_staff();
  $schema['pe_migrate_profile_student'] = pe_migrate_schema_profile_student();
  $schema['pe_migrate_node_article'] = pe_migrate_schema_node_article();
  $schema['pe_migrate_node_page'] = pe_migrate_schema_node_page();
  $schema['pe_migrate_node_announcement'] = pe_migrate_schema_node_announcement();
  $schema['pe_migrate_node_acad_year'] = pe_migrate_schema_node_acad_year();
  $schema['pe_migrate_node_acad_term'] = pe_migrate_schema_node_acad_term();
  $schema['pe_migrate_node_edu'] = pe_migrate_schema_node_edu();
  $schema['pe_migrate_node_section'] = pe_migrate_schema_node_section();
  $schema['pe_migrate_node_program'] = pe_migrate_schema_node_program();
  $schema['pe_migrate_node_course'] = pe_migrate_schema_node_course();
  $schema['pe_migrate_node_program_course'] = pe_migrate_schema_node_program_course();
  //$schema['pe_migrate_node_course_user'] = pe_migrate_schema_node_course_user();

  return $schema;
}

/**
 * Implements hook_install().
 */
function pe_migrate_install() {
  pe_migrate_data_taxonomy_term();
  pe_migrate_data_user();
  pe_migrate_data_profile_main();
  pe_migrate_data_profile_alumnus();
  pe_migrate_data_profile_staff();
  pe_migrate_data_profile_student();
  pe_migrate_data_node_article();
  pe_migrate_data_node_page();
  pe_migrate_data_node_announcement();
  pe_migrate_data_node_acad_year();
  pe_migrate_data_node_acad_term();
  pe_migrate_data_node_edu();
  pe_migrate_data_node_section();
  pe_migrate_data_node_program();
  pe_migrate_data_node_course();
  pe_migrate_data_node_program_course();

  //pe_migrate_data_node_course_user();
}

/**
 * Implements hook_uninstall().
 */
function pe_migrate_uninstall() {
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_acad_term')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_acad_year')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_article')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_course')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_edu')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_page')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_announcement')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_profile_alumnus')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_profile_main')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_profile_staff')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_profile_student')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_program')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_program_course')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_section')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_term')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration.demo_uni_user')->delete();
  \Drupal::configFactory()->getEditable('migrate_plus.migration_group.demo_uni')->delete();
}

/**
 * Taxonomy terms table schema.
 *
 * @return array
 */
function pe_migrate_schema_taxonomy_term() {
  return array(
    'description' => 'Taxonomy terms',
    'fields' => array(
      'name'  => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Term name',
      ),
      'description' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Description',
      ),
      'format' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'The format of the description field',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Weight',
      ),
      'puuid' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => TRUE,
        'description' => 'UUID',
      ),
      'vocabulary_machine_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'The vocabulary ID.',
      ),
      'parent' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'The parent term UUID.',
      ),
    ),
    'primary key' => array('puuid'),
  );
}

/**
 * Users table schema.
 *
 * @return array
 */
function pe_migrate_schema_user() {
  return array(
    'description' => 'User accounts.',
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Account username',
      ),
      'mail' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account email',
      ),
      'pass' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account password (raw)',
      ),
      'created' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Created tim',
      ),
      'picture' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Name of the user picture',
      ),
      'roles' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account roles',
      ),
    ),
    'primary key' => array('name'),
  );
}

/**
 * Main profiles table schema.
 *
 * @return array
 */
function pe_migrate_schema_profile_main() {
  return array(
    'description' => 'User main profiles.',
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Account usename',
      ),
      'field_personal_title' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Personal title - term UUID',
      ),
      'field_firstname' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'First name',
      ),
      'field_middlename' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'Middle name',
      ),
      'field_lastname' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'Last name',
      ),
      'field_birth_date' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Date of birth',
      ),
      'field_gender' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Gender - term UUID',
      ),
      'field_marital_status' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Marital status - term UUID',
      ),
      'field_address__streetAddress' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'Address - street',
      ),
      'field_address__addressLocality' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'Address - town/city',
      ),
      'field_address__addressRegion' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'Address - region/state/province',
      ),
      'field_address__postalCode' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Address - postcode',
      ),
      'field_address__addressCountry' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'Address - country',
      ),
      'field_mobile' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Mobile number',
      ),
      'field_phone' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Telephone number',
      ),
      'field_url__url' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'Website',
      ),
      'field_languages' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Languages spoken',
      ),
      'field_interests' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Interests',
      ),
    ),
    'primary key' => array('name'),
  );
}

/**
 * Alumnus profiles table schema.
 *
 * @return array
 */
function pe_migrate_schema_profile_alumnus() {
  return array(
    'description' => 'User alumni profiles.',
    'fields' => array(
      'name'  => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Username',
      ),
      'uuid' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => TRUE,
        'description' => 'UUID',
      ),
      'field_alumni_association_member' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Alumni association membership flag.',
      ),
      'field_acad_program_title'  => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Academic program',
      ),
      'field_graduation_year_title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Year of graduation.',
      ),
    ),
    'primary key' => array('name'),
  );
}

/**
 * Staff profiles table schema.
 *
 * @return array
 */
function pe_migrate_schema_profile_staff() {
  return array(
    'description' => 'User staff profiles.',
    'fields' => array(
      'name'  => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Username',
      ),
      'roles' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'User roles',
      ),
      'uuid' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => TRUE,
        'description' => 'UUID',
      ),
      'field_section_title'  => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Faculty/Department',
      ),
      'field_position_title' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => TRUE,
        'description' => 'Position - term UUID',
      ),
      'field_academic' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Academic?  - term UUID',
      ),
      'field_staff_number' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Staff number',
      ),
      'field_employment_year_title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Year of employment',
      ),
    ),
    'primary key' => array('name'),
  );
}

/**
 * Student profiles table schema.
 *
 * @return array
 */
function pe_migrate_schema_profile_student() {
  return array(
    'description' => 'User student profiles.',
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Username',
      ),
      'uuid' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => TRUE,
        'description' => 'UUID',
      ),
      'field_acad_program_title' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Academic program',
      ),
      'field_level_title' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Level of study - term UUID',
      ),
      'field_entry_year_title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Year of entry',
      ),
      'field_matriculation_number' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Matriculation number',
      ),
      'field_entry_mode_title' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Mode of entry - term UUID',
      ),
    ),
    'primary key' => array('name'),
  );
}


/**
 * Article nodes table schema.
 *
 * @return array
 */function pe_migrate_schema_node_article() {
  return array(
    'description' => 'Article nodes.',
    'fields' => array(
      'title'  => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
      'field_image_art' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image name',
      ),
      'field_image_alt' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image ALT',
      ),
      'field_image_title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image title',
      ),
      'field_image_description' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image description',
      ),
      'promote' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Promote to home page.',
      ),
      'sticky' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Sticky at top of lists',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * Page nodes table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_page() {
  return array(
    'description' => 'Page nodes.',
    'fields' => array(
      'title'  => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body.',
      ),
      'promote' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Promote node to home page.',
      ),
      'sticky' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Make node sitcky at top of lists.',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * Announcements table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_announcement() {
  return array(
    'description' => 'Announcements.',
    'fields' => array(
      'title'  => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'field_start_date' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Start date',
      ),
      'field_end_date' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'End date',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * Academic years table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_acad_year() {
  return array(
    'description' => 'Academic years.',
    'fields' => array(
      'title'  => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'field_start_date' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Start date',
      ),
      'field_end_date' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'End date',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * Academic terms table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_acad_term() {
  return array(
    'description' => 'Academic terms.',
    'fields' => array(
      'title'  => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'field_term' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Term - term UUID.',
      ),
      'field_acad_year' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Academic year',
      ),
      'field_start_date' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Start date.',
      ),
      'field_end_date' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'End date',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * Edu nodes table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_edu() {
  return array(
    'description' => 'Educational organisation nodes.',
    'fields' => array(
      'title' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
      'path' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Path',
      ),
      'field_image' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image name',
      ),
      'field_founding_date' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Founding date',
      ),
      'field_defunct_date' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Defunct date',
      ),
      'field_institution_type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Instituition type - term UUID.)',
      ),
      'field_history' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'History',
      ),
      'field_main_location' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Main location flag',
      ),
      'field_code' => array(
        'type' => 'varchar',
        'length' => 10,
        'not null' => FALSE,
        'description' => 'Code',
      ),
      'field_address__streetAddress' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Address - stree',
      ),
      'field_address__addressLocality' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Address - town/city',
      ),
      'field_address__addressRegion' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account name (for display)',
      ),
      'field_address__postalCode' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Address - postcode',
      ),
      'field_address__addressCountry' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => FALSE,
        'description' => 'Address - country',
      ),
      'field_telephone_number' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Telephone number',
      ),
      'field_fax_number' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Fax number',
      ),
      'field_url__url' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Website',
      ),
      'field_email' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Email',
      ),
      'field_legal_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Legal name',
      ),
      'field_duns_number' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'DUNS number',
      ),
      'field_vat_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'VAT ID',
      ),
      'field_tax_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Tax ID',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * Section nodes table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_section() {
  return array(
    'description' => 'Section nodes.',
    'fields' => array(
      'title' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
      'field_image_sec' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image name',
      ),
      'field_code' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => FALSE,
        'description' => 'Code',
      ),
      'field_parent_section_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Parent section',
      ),
      'field_edu_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Capmus',
      ),
      'field_section_type_id' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Section type - term UUID',
      ),
      'field_founding_date' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Founding date',
      ),
      'field_defunct_date' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Defunct date',
      ),
      'field_history' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'History',
      ),
      'field_head_title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Head title)',
      ),
      'field_ahead_title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Asst/Deputy head title',
      ),
      'field_head' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Head)',
      ),
      'field_ahead' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Asst/Deputy head',
      ),
      'path' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Path',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * Program nodes table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_program() {
  return array(
    'description' => 'Academic programme nodes.',
    'fields' => array(
      'title' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
      'field_image' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image',
      ),
      'field_code' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'Code',
      ),
      'field_duration' => array(
        'type' => 'varchar',
        'length' => 10,
        'not null' => FALSE,
        'description' => 'Duration',
      ),
      'field_duration_unit' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Duration unit',
      ),
      'field_section_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Faculty/Department',
      ),
      'field_term_program' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Program - term UUID',
      ),
      'field_term_time' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Term - term UUID)',
      ),
      'field_term_program_type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account password (raw)',
      ),
      'field_requirements' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Requirements',
      ),
      'field_career' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Career',
      ),
    ),
    'primary key' => array('title'),
  );
}


/**
 * Course nodes table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_course() {
  return array(
    'description' => 'Course nodes.',
    'fields' => array(
      'title' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
      'field_image' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image',
      ),
      'field_section_id' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => FALSE,
        'description' => 'Faculty/Department',
      ),
      'field_code' => array(
        'type' => 'varchar',
        'length' => 20,
        'not null' => FALSE,
        'description' => 'Code',
      ),
      'field_delivery_method' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Delivery method - term UUID',
      ),
      'field_hours' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Hours',
      ),
      'field_credits' => array(
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Credits',
      ),
      'field_remarks' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Remarks',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * Program course nodes table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_program_course() {
  return array(
    'description' => 'Courses for academic programmes.',
    'fields' => array(
      'title' => array(
        'type' => 'varchar',
        'length' => 100,
        'not null' => TRUE,
        'description' => 'Title',
      ),
      'status' => array(
        'type' => 'char',
        'length' => 1,
        'not null' => FALSE,
        'description' => 'Status',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => FALSE,
        'description' => 'Body',
      ),
      'field_acad_program_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Academic program',
      ),
      'field_course_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Course',
      ),
      'field_acad_year_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Academic year',
      ),
      'field_acad_term_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Academic term',
      ),
      'field_course_weight' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Course weight',
      ),
      'field_level' => array(
        'type' => 'char',
        'length' => 36,
        'not null' => FALSE,
        'description' => 'Level of study - term UUID)',
      ),
      'field_prereq_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Course pre-requisites',
      ),
      'field_coreq_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Course co-requisites',
      ),
      'field_nonreq_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Course non-requisites',
      ),
      'field_antireq_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Course anti-requisites',
      ),
      'field_teacher_id' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Teachers',
      ),
    ),
    'primary key' => array('title'),
  );
}

/**
 * User courses table schema.
 *
 * @return array
 */
function pe_migrate_schema_node_course_user() {

}

/**
 * Helper function to read CSV file contents to array.
 *
 * @param $file
 *
 * @return array
 */
function _pe_migrate_get_csv_data($type, $file) {
  $filepath = drupal_get_path('module', 'pe_migrate') . '/data/' . $type . '/' . $file . '.csv';
  $data = (file_exists($filepath)) ? array_map('str_getcsv', file($filepath)) : [];
  return $data;
}

/**
 * Helper function to insert data into database tables.
 *
 * @param       $table
 * @param array $fields
 * @param array $data
 *
 * @throws \Exception
 */
function _pe_migrate_insert_db_data($table, array $fields, array $data) {
  $query = db_insert($table)->fields($fields);
  foreach ($data as $row) {
    $query->values(array_combine($fields, $row));
  }
  $query->execute();
}

/**
 * Import taxonomy term data.
 */
function pe_migrate_data_taxonomy_term() {
  $files = ['approval_status', 'business_type', 'course_weight', 'delivery_method', 'duration_unit', 'entry_mode', 'gender', 'institution_type', 'level', 'marital_status', 'personal_title', 'position', 'program', 'program_type', 'relationship', 'section_head_title', 'section_type', 'study_mode', 'terms'];
  foreach ($files as $file) {
    $data = _pe_migrate_get_csv_data('taxonomy_term', $file);
    if($data) {
      $fields = array_shift($data);
      _pe_migrate_insert_db_data(TABLE_TAXONOMY_TERM, $fields, $data);
    }
  }
}

/**
 * Import user data.
 */
function pe_migrate_data_user() {
  $data = _pe_migrate_get_csv_data('user', 'pe_user');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_USER, $fields, $data);
}

/**
 * Import main profile data.
 */
function pe_migrate_data_profile_main() {
  $data = _pe_migrate_get_csv_data('profile', 'pe_user_profile_main');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_PROFILE_MAIN, $fields, $data);
}

function pe_migrate_data_profile_alumnus() {
  $data = _pe_migrate_get_csv_data('profile', 'pe_user_profile_alumnus');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_PROFILE_ALUMNUS, $fields, $data);
}

/**
 * Import staff profile data.
 */
function pe_migrate_data_profile_staff() {
  $data = _pe_migrate_get_csv_data('profile', 'pe_user_profile_staff');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_PROFILE_STAFF, $fields, $data);
}

/**
 * Import student profile data.
 */
function pe_migrate_data_profile_student() {
  $data = _pe_migrate_get_csv_data('profile', 'pe_user_profile_student');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_PROFILE_STUDENT, $fields, $data);
}

/**
 * Import article data.
 */
function pe_migrate_data_node_article() {
  $data = _pe_migrate_get_csv_data('node', 'article');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_ARTICLE, $fields, $data);
}

/**
 * Import page data.
 */
function pe_migrate_data_node_page() {
  $data = _pe_migrate_get_csv_data('node', 'page');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_PAGE, $fields, $data);
}

/**
 * Import announcement data.
 */
function pe_migrate_data_node_announcement() {
  $data = _pe_migrate_get_csv_data('node', 'announcement');
  $fields = array_shift($data);

  // lectures - today.
  $data[0][1] = (new DateTime())->format('Y-m-d') . 'T15:00:00';
  $data[0][2] = (new DateTime())->format('Y-m-d') . 'T18:00:00';;
  // concert - next Saturday
  $sat = new DateTime('next saturday');
  $sat_formatted = $sat->format('Y-m-d');
  $data[1][1] = $sat_formatted . 'T19:30:00';
  $data[1][2] = $sat_formatted . 'T23:00:00';
  // open days - next Saturday and Sunday
  $data[2][1] = $sat_formatted . 'T09:00:00';
  $data[2][2] = $sat->add(new DateInterval('P1D'))->format('Y-m-d') . 'T17:00:00';

  _pe_migrate_insert_db_data(TABLE_NODE_ANNOUNCEMENT, $fields, $data);
}

/**
 * Import academic year data.
 */
function pe_migrate_data_node_acad_year() {
  $data = _pe_migrate_get_csv_data('node', 'pe_acad_year');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_ACAD_YEAR, $fields, $data);
}

/**
 * Import academic term data.
 */
function pe_migrate_data_node_acad_term() {
  $data = _pe_migrate_get_csv_data('node', 'pe_acad_term');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_ACAD_TERM, $fields, $data);
}

/**
 * Import campus data.
 */
function pe_migrate_data_node_edu() {
  $data = _pe_migrate_get_csv_data('node', 'pe_edu');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_EDU, $fields, $data);
}

/**
 * Import section data.
 */
function pe_migrate_data_node_section() {
  $data = _pe_migrate_get_csv_data('node', 'pe_section');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_SECTION, $fields, $data);
}

/**
 * Import program data.
 */
function pe_migrate_data_node_program() {
  $data = _pe_migrate_get_csv_data('node', 'pe_program');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_PROGRAM, $fields, $data);
}

/**
 * Import course data.
 */
function pe_migrate_data_node_course() {
  $data = _pe_migrate_get_csv_data('node', 'pe_course');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_COURSE, $fields, $data);
}

/**
 * Import program course data.
 */
function pe_migrate_data_node_program_course() {
  $data = _pe_migrate_get_csv_data('node', 'pe_program_course');
  $fields = array_shift($data);
  _pe_migrate_insert_db_data(TABLE_NODE_PROGRAM_COURSE, $fields, $data);
}


function pe_migrate_data_node_course_user() {

}
