<?php

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

/**
 * Implements hook_schema().
 */
function search_schema(): array {
  $schema['search_dataset'] = [
    'description' => 'Stores items that will be searched.',
    'fields' => [
      'sid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Search item ID, e.g. node ID for nodes.',
      ],
      'langcode' => [
        'type' => 'varchar_ascii',
        'length' => '12',
        'not null' => TRUE,
        'description' => 'The {languages}.langcode of the item variant.',
        'default' => '',
      ],
      'type' => [
        'type' => 'varchar_ascii',
        'length' => 64,
        'not null' => TRUE,
        'description' => 'Type of item, e.g. node.',
      ],
      'data' => [
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'description' => 'List of space-separated words from the item.',
      ],
      'reindex' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Set to force node reindexing.',
      ],
    ],
    'primary key' => ['sid', 'langcode', 'type'],
  ];

  $schema['search_index'] = [
    'description' => 'Stores the search index, associating words, items and scores.',
    'fields' => [
      'word' => [
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The {search_total}.word that is associated with the search item.',
      ],
      'sid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {search_dataset}.sid of the searchable item to which the word belongs.',
      ],
      'langcode' => [
        'type' => 'varchar_ascii',
        'length' => '12',
        'not null' => TRUE,
        'description' => 'The {languages}.langcode of the item variant.',
        'default' => '',
      ],
      'type' => [
        'type' => 'varchar_ascii',
        'length' => 64,
        'not null' => TRUE,
        'description' => 'The {search_dataset}.type of the searchable item to which the word belongs.',
      ],
      'score' => [
        'type' => 'float',
        'not null' => FALSE,
        'description' => 'The numeric score of the word, higher being more important.',
      ],
    ],
    'indexes' => [
      'sid_type' => ['sid', 'langcode', 'type'],
    ],
    'foreign keys' => [
      'search_dataset' => [
        'table' => 'search_dataset',
        'columns' => [
          'sid' => 'sid',
          'langcode' => 'langcode',
          'type' => 'type',
        ],
      ],
    ],
    'primary key' => ['word', 'sid', 'langcode', 'type'],
  ];

  $schema['search_total'] = [
    'description' => 'Stores search totals for words.',
    'fields' => [
      'word' => [
        'description' => 'Primary Key: Unique word in the search index.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
        'default' => '',
      ],
      'count' => [
        'description' => "The count of the word in the index using Zipf's law to equalize the probability distribution.",
        'type' => 'float',
        'not null' => FALSE,
      ],
    ],
    'primary key' => ['word'],
  ];

  return $schema;
}

/**
 * Install Search Node and Search and Node are installed.
 */
function search_update_11400(): void {
  // Moved to system_update_11401().
}

/**
 * Install Help Search module if the Search and Help modules are installed.
 */
function search_update_11401(): void {
  $module_handler = \Drupal::moduleHandler();
  // Search help may already have been installed by sites on 11.4.0 or 11.4.1.
  if ($module_handler->moduleExists('search_help')) {
    return;
  }
  if ($module_handler->moduleExists('help')) {
    \Drupal::service('module_installer')->install(['search_help']);
  }
}

/**
 * Install Search node module if node is installed.
 */
function search_update_11402(): void {
  if (\Drupal::moduleHandler()->moduleExists('node') && !\Drupal::moduleHandler()->moduleExists('search_node')) {
    \Drupal::configFactory()->getEditable('core.entity_view_mode.node.search_index')->delete();
    \Drupal::configFactory()->getEditable('core.entity_view_mode.node.search_result')->delete();
    \Drupal::service('module_installer')->install(['search_node']);
    /** @var \Drupal\search\SearchPageInterface|null $node_search_page */
    $node_search_page = \Drupal::entityTypeManager()->getStorage('search_page')->load('node_search');
    $node_search_page?->calculateDependencies()->save();
  }
}
