<?php

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

use Drupal\Component\Utility\Crypt;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Update\EquivalentUpdate;

/**
 * Implements hook_install().
 */
function system_install(): void {
  // Populate the cron key state variable.
  $cron_key = Crypt::randomBytesBase64(55);
  \Drupal::state()->set('system.cron_key', $cron_key);

  // Populate the site UUID and default name (if not set).
  $site = \Drupal::configFactory()->getEditable('system.site');
  $site->set('uuid', \Drupal::service('uuid')->generate());
  if (!$site->get('name')) {
    $site->set('name', 'Drupal');
  }
  $site->save();

  // Populate the dummy query string added to all CSS and JavaScript files.
  \Drupal::service('asset.query_string')->reset();
}

/**
 * Implements hook_schema().
 */
function system_schema(): array {
  // @deprecated The sequences table has been deprecated in drupal:10.2.0 and is
  // removed from drupal:12.0.0. See https://www.drupal.org/node/3220378.
  // @todo Remove sequences table in Drupal 12. See https://www.drupal.org/i/3335756
  $schema['sequences'] = [
    'description' => 'Stores IDs.',
    'fields' => [
      'value' => [
        'description' => 'The value of the sequence.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
    ],
    'primary key' => ['value'],
  ];

  return $schema;
}

/**
 * Implements hook_update_last_removed().
 */
function system_update_last_removed(): int {
  return 10201;
}

/**
 * Add a [time] column to the {simpletest} table, if existing.
 */
function system_update_11200(): void {
  $schema = \Drupal::database()->schema();
  if ($schema->tableExists('simpletest') && !$schema->fieldExists('simpletest', 'time')) {
    $schema->addField('simpletest', 'time', [
      'type' => 'float',
      'not null' => TRUE,
      'default' => 0,
      'description' => 'Time elapsed for the execution of the test.',
    ]);
  }
}

/**
 * Invalidate container because the module handler has changed.
 */
function system_update_11100(): void {
  \Drupal::service('kernel')->invalidateContainer();
}

/**
 * Update length of menu_tree fields url and route_param_key from 255 to 2048.
 */
function system_update_11001(): void {
  $schema = \Drupal::database()->schema();
  $spec = [
    'description' => 'The external path this link points to (when not using a route).',
    'type' => 'varchar',
    'length' => 2048,
    'not null' => TRUE,
    'default' => '',
  ];
  $schema->changeField('menu_tree', 'url', 'url', $spec);

  $spec = [
    'description' => 'An encoded string of route parameters for loading by route.',
    'type' => 'varchar',
    'length' => 2048,
  ];
  $schema->changeField('menu_tree', 'route_param_key', 'route_param_key', $spec);
}

/**
 * Equivalent update to 10400.
 */
function system_update_11102(): TranslatableMarkup|null {
  // This is a no-op that exists to prevent an upgrade from 10.4+ to 11.0. That
  // path is actually a downgrade.
  $equivalent_update = \Drupal::service('update.update_hook_registry')
    ->getEquivalentUpdate();
  if ($equivalent_update instanceof EquivalentUpdate) {
    return $equivalent_update->toSkipMessage();
  }
  return NULL;
}

/**
 * Add the [alias] field to the {router} table.
 */
function system_update_11201(): void {
  $schema = \Drupal::database()->schema();

  if ($schema->tableExists('router') && !$schema->fieldExists('router', 'alias')) {
    $spec = [
      'fields' => [
        'alias' => [
          'description' => 'The alias of the route, if applicable.',
          'type' => 'varchar_ascii',
          'length' => 255,
        ],
      ],
    ];
    $schema->addField('router', 'alias', $spec['fields']['alias']);
    $schema->addIndex('router', 'alias', ['alias'], $spec);
  }
}

/**
 * Add an [exit_code] column to the {simpletest} table, if existing.
 */
function system_update_11202(): void {
  $schema = \Drupal::database()->schema();
  if ($schema->tableExists('simpletest') && !$schema->fieldExists('simpletest', 'exit_code')) {
    $schema->addField('simpletest', 'exit_code', [
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
      'description' => 'The exit code of the test executed.',
    ]);
  }
}

/**
 * Equivalent update to 10600.
 */
function system_update_11300(): TranslatableMarkup|null {
  // This is a no-op that exists to prevent an upgrade from 10.6+ to 11.2. That
  // path is actually a downgrade.
  $equivalent_update = \Drupal::service('update.update_hook_registry')
    ->getEquivalentUpdate();
  if ($equivalent_update instanceof EquivalentUpdate) {
    return $equivalent_update->toSkipMessage();
  }
  return NULL;
}

/**
 * Moved to search_update_11401().
 */
function system_update_11400(): void {
}

/**
 * Moved to search_update_11402().
 */
function system_update_11401(): void {
}
