<?php

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

use Symfony\Component\Yaml\Yaml;

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of social_mentions.
 */
function social_mentions_install() {
  // Grant the default permissions for this feature.
  user_role_grant_permissions(
    'verified',
    [
      'access mentions',
    ]
  );
  user_role_grant_permissions(
    'contentmanager',
    [
      'access mentions',
    ]
  );
  user_role_grant_permissions(
    'sitemanager',
    [
      'access mentions',
    ]
  );
  // Set default configurations.
  _social_mentions_set_default_config();
}

/**
 * Set suggestion amount to the default of 8.
 */
function social_mentions_update_8001() {
  $config = \Drupal::configFactory()->getEditable('mentions.settings');

  $config->set('suggestions_amount', 8);

  $config->save();
}

/**
 * Set default settings.
 */
function _social_mentions_set_default_config() {
  $config = \Drupal::configFactory()->getEditable('mentions.settings');

  $allowed_entity_types = ['post', 'comment'];
  $allowed_entity_types = array_combine($allowed_entity_types, $allowed_entity_types);
  $config->set('supported_entity_types', $allowed_entity_types);

  $config->set('suggestions_format', SOCIAL_PROFILE_SUGGESTIONS_ALL);

  $config->set('suggestions_amount', 8);

  $config->save();
}

/**
 * Update the listed below message template files as per new email style.
 *
 * 1. message.template.create_comment_reply_mention
 * 2. message.template.create_mention_comment
 * 3. message.template.create_mention_post.
 *
 * Added new 'preview' and 'cta_button' tokens to these templates.
 */
function social_mentions_update_10301() {
  // Prepare the path.
  $path = \Drupal::service('extension.list.module')->getPath('social_mentions') . '/config/static/';

  $message_templates = [
    'message.template.create_comment_reply_mention' => $path . 'message.template.create_comment_reply_mention_10301.yml',
    'message.template.create_mention_comment' => $path . 'message.template.create_mention_comment_10301.yml',
    'message.template.create_mention_post' => $path . 'message.template.create_mention_post_10301.yml',
  ];

  // Retrieves the configuration factory.
  $config_factory = \Drupal::configFactory();

  // Update each template file.
  foreach ($message_templates as $key => $config_file) {
    if (is_file($config_file)) {
      $settings = Yaml::parse(file_get_contents($config_file));
      if (is_array($settings)) {
        $config = $config_factory->getEditable($key);
        $config->setData($settings)->save(TRUE);
      }
    }
  }
}

/**
 * Updating  configuration keys as per underlying schema.
 */
function social_mentions_update_11001() : string {
  /** @var \Drupal\update_helper\Updater $updater */
  $updater = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updater->executeUpdate('social_mentions', 'social_mentions_update_11001');

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

/**
 * Enable the jquery_ui_autocomplete module.
 */
function social_mentions_update_12001(): void {
  if (!\Drupal::service('module_handler')->moduleExists('jquery_ui_autocomplete')) {
    \Drupal::service('module_installer')->install(['jquery_ui_autocomplete']);
  }
}
