<?php

/**
 * @file
 * Update hooks for the redirect_404 module.
 */

use Drupal\Core\Language\LanguageInterface;
use Drupal\redirect_404\SqlRedirectNotFoundStorage;

/**
 * Implements hook_schema().
 */
function redirect_404_schema() {
  $schema['redirect_404'] = [
    'description' => 'Stores 404 requests.',
    'fields' => [
      'path' => [
        'description' => 'The path of the request.',
        'type' => 'varchar',
        'length' => SqlRedirectNotFoundStorage::MAX_PATH_LENGTH,
        'not null' => TRUE,
      ],
      'langcode' => [
        'description' => 'The language of this request.',
        'type' => 'varchar_ascii',
        'length' => 12,
        'not null' => TRUE,
        'default' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ],
      'count' => [
        'description' => 'The number of requests with that path and language.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'daily_count' => [
        'description' => 'The number of requests with that path and language in a day.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'timestamp' => [
        'description' => 'The timestamp of the last request with that path and language.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'resolved' => [
        'description' => 'Boolean indicating whether or not this path has a redirect assigned.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => ['path', 'langcode'],
  ];
  return $schema;
}

/**
 * Remove relevancy field from the redirect_404 table.
 */
function redirect_404_update_8101() {
  \Drupal::database()->schema()->dropField('redirect_404', 'relevancy');
}

/**
 * Add daily count field to redirect_404 table and view.
 */
function redirect_404_update_8102() {
  \Drupal::database()->schema()->addField('redirect_404', 'daily_count', [
    'description' => 'The number of requests with that path and language in a day.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ]);

  $view_config = \Drupal::configFactory()->getEditable('views.view.redirect_404');

  if (!$view_config->isNew()) {
    $columns_key = 'display.default.display_options.style.options.columns';
    $columns = $view_config->get($columns_key);
    $columns['daily_count'] = 'daily_count';
    $view_config->set($columns_key, $columns);

    $info_key = 'display.default.display_options.style.options.info';
    $info = $view_config->get($info_key);
    $info['daily_count'] = [
      'sortable' => TRUE,
      'default_sort_order' => 'desc',
      'align' => '',
      'separator' => '',
      'empty_column' => FALSE,
      'responsive' => '',
    ];
    $view_config->set($info_key, $info);

    $fields = $view_config->get('display.default.display_options.fields');

    $daily_count = [
      'id' => 'daily_count',
      'table' => 'redirect_404',
      'field' => 'daily_count',
      'relationship' => 'none',
      'group_type' => 'group',
      'admin_label' => '',
      'label' => 'Daily count',
      'exclude' => FALSE,
      'alter' => [
        'alter_text' => FALSE,
        'text' => '',
        'make_link' => FALSE,
        'path' => '',
        'absolute' => FALSE,
        'external' => FALSE,
        'replace_spaces' => FALSE,
        'path_case' => 'none',
        'trim_whitespace' => FALSE,
        'alt' => '',
        'rel' => '',
        'link_class' => '',
        'prefix' => '',
        'suffix' => '',
        'target' => '',
        'nl2br' => FALSE,
        'max_length' => 0,
        'word_boundary' => TRUE,
        'ellipsis' => TRUE,
        'more_link' => FALSE,
        'more_link_text' => '',
        'more_link_path' => '',
        'strip_tags' => FALSE,
        'trim' => FALSE,
        'preserve_tags' => '',
        'html' => FALSE,
      ],
      'element_type' => '',
      'element_class' => '',
      'element_label_type' => '',
      'element_label_class' => '',
      'element_label_colon' => TRUE,
      'element_wrapper_type' => '',
      'element_wrapper_class' => '',
      'element_default_classes' => TRUE,
      'empty' => '',
      'hide_empty' => FALSE,
      'empty_zero' => FALSE,
      'hide_alter_empty' => TRUE,
      'format' => 'unserialized',
      'key' => '',
      'plugin_id' => 'serialized',
    ];

    $count_index = array_search('count', array_keys($fields));
    $fields = array_slice($fields, 0, $count_index + 1) + ['daily_count' => $daily_count] + array_slice($fields, $count_index);

    $view_config->set('display.default.display_options.fields', $fields)->save();
  }
}
