<?php

/**
 * @file
 * Install/update hook implementations.
 */

/**
 * Implements hook_schema().
 */
function islandora_schema() {
  $schema = [];
  $schema['islandora_version_count'] = [
    'description' => 'Keeps track of the number of changes to an entity',
    'fields' => [
      'id' => [
        'description' => 'Autoincrementing id for record',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'uuid' => [
        'description' => 'UUID for an entity',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'unique' => TRUE,
      ],
      'count' => [
        'description' => 'Number of times an entity has been updated.',
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => ['id'],
    'unique keys' => [
      'uuid' => ['uuid'],
    ],
  ];
  return $schema;
}

/**
 * Implements hook_install().
 */
function islandora_install() {
  if (\Drupal::moduleHandler()->moduleExists('rest')) {
    $rest_resource_config_storage = \Drupal::service('entity_type.manager')->getStorage('rest_resource_config');
    $rest_resource_config = $rest_resource_config_storage->load('entity.node');

    if ($rest_resource_config) {
      $configuration = $rest_resource_config->get('configuration');

      if (!in_array('jsonld', $configuration['formats'])) {
        $configuration['formats'][] = 'jsonld';
      }

      if (!in_array('jwt_auth', $configuration['authentication'])) {
        $configuration['authentication'][] = 'jwt_auth';
      }

      $rest_resource_config->set('configuration', $configuration);
      $rest_resource_config->save(TRUE);
    }
  }
}