<?php

/**
 * @file
 * Installation code for the social_event_content_block module.
 */

/**
 * Add field event date sorting to event content list blocks.
 */
function social_event_content_block_update_8001() {
  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper->executeUpdate('social_event_content_block', 'social_event_content_block_update_8001');

  // Output logged messages to related channel of update execution.
  return $updateHelper->logger()->output();
}

/**
 * Add field event date filter to event content list blocks.
 */
function social_event_content_block_update_8002() {
  $configs = [
    'field_storage_config' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'module' => [
          'block_content',
          'options',
        ],
      ],
      'id' => 'block_content.field_event_date',
      'field_name' => 'field_event_date',
      'entity_type' => 'block_content',
      'type' => 'list_string',
      'settings' => [
        'allowed_values' => [
          [
            'value' => 'future',
            'label' => t('Future Events'),
          ],
          [
            'value' => 'past',
            'label' => t('Past Events'),
          ],
          [
            'value' => 'last_month',
            'label' => t('Last Month'),
          ],
          [
            'value' => 'current_month',
            'label' => t('This month'),
          ],
          [
            'value' => 'next_month',
            'label' => t('Next month'),
          ],
          [
            'value' => 'ongoing',
            'label' => t('Ongoing'),
          ],
          [
            'value' => 'future_ongoing',
            'label' => t('Future and ongoing'),
          ],
          [
            'value' => 'last_30',
            'label' => t('Last 30 days'),
          ],
          [
            'value' => 'next_30',
            'label' => t('Next 30 days'),
          ],
          [
            'value' => 'last_14',
            'label' => t('Last 14 days'),
          ],
          [
            'value' => 'next_14',
            'label' => t('Next 14 days'),
          ],
          [
            'value' => 'last_7',
            'label' => t('Last 7 days'),
          ],
          [
            'value' => 'next_7',
            'label' => t('Next 7 days'),
          ],
        ],
        'allowed_values_function' => '',
      ],
      'module' => 'options',
      'locked' => FALSE,
      'cardinality' => 1,
      'translatable' => TRUE,
      'indexes' => [],
      'persist_with_no_fields' => FALSE,
      'custom_storage' => FALSE,
    ],
    'field_config' => [
      'langcode' => 'en',
      'status' => TRUE,
      'dependencies' => [
        'config' => [
          'block_content.type.custom_content_list',
          'field.storage.block_content.field_event_date',
        ],
        'module' => [
          'options',
        ],
      ],
      'id' => 'block_content.custom_content_list.field_event_date',
      'field_name' => 'field_event_date',
      'entity_type' => 'block_content',
      'bundle' => 'custom_content_list',
      'label' => t('Date period'),
      'description' => '',
      'required' => FALSE,
      'translatable' => FALSE,
      'default_value' => [
        [
          'value' => 'future',
        ],
      ],
      'default_value_callback' => '',
      'settings' => [],
      'field_type' => 'list_string',
    ],
  ];

  foreach ($configs as $entity_type_id => $data) {
    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);

    $storage->createFromStorageRecord($data)->save();
  }
}

/**
 * Add "ongoing" allowed value to the event date field storage.
 */
function social_event_content_block_update_8003() {
  $event_date_storage = \Drupal::configFactory()->getEditable('field.storage.block_content.field_event_date');
  $allowed_values = $event_date_storage->get('settings.allowed_values');
  $allowed_values[] = [
    'value' => 'ongoing',
    'label' => t('Ongoing'),
  ];

  $event_date_storage->set('settings.allowed_values', $allowed_values)->save();
}

/**
 * Add "future_ongoing" allowed value to the event date field storage.
 */
function social_event_content_block_update_11501(): void {
  $event_date_storage = \Drupal::configFactory()->getEditable('field.storage.block_content.field_event_date');
  $allowed_values = $event_date_storage->get('settings.allowed_values');
  $allowed_values[] = [
    'value' => 'future_ongoing',
    'label' => t('Future and ongoing'),
  ];

  $event_date_storage->set('settings.allowed_values', $allowed_values)->save();
}

/**
 * Add "flexible" group type to the field_event_group settings.
 */
function social_event_content_block_update_11502(): void {
  $event_field_group_storage = \Drupal::configFactory()->getEditable('field.field.block_content.custom_content_list.field_event_group');
  $handler_settings = $event_field_group_storage->get('settings.handler_settings');
  $handler_settings['target_bundles']['flexible_group'] = "flexible_group";

  $event_field_group_storage->set('settings.handler_settings', $handler_settings)->save();
}
