<?php

namespace Drupal\azure_ad;

use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Url;
use Drupal\user_provisioning\moUserProvisioningSupport;

/**
 * Azure AD Utilities.
 */
class MoAzureUtilities {
  /**
   * Immutable Azure AD module configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected ImmutableConfig $config;

  /**
   * Constructor for MoAzureUtilities.
   */
  public function __construct() {
    $this->config = \Drupal::config('azure_ad.settings');
  }

  /**
   * Create the callback URL.
   */
  public static function createCallbackUrl() {
    $baseUrl = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
    return preg_replace('/^http:/', 'https:', $baseUrl);

  }

  /**
   * Get the customer API key.
   */
  public function customerApiKey() {
    $customerKey = $this->config->get('mo_user_provisioning_customer_id');
    $apikey = $this->config->get('mo_user_provisioning_customer_api_key');
    if ($customerKey === '') {
      $customerKey = "16555";
      $apikey = "fFd2XcvTGDemZvbw1bcUesNJWEqKbbUq";
    }
    $currentTimeInMillis = moUserProvisioningSupport::getTimestamp();
    $stringToHash        = $customerKey . $currentTimeInMillis . $apikey;
    $hashValue           = hash("sha512", $stringToHash);

    return [$customerKey, $currentTimeInMillis, $hashValue];
  }

  /**
   * Get the module version.
   */
  public function getModuleVersion() {
    $modules_info = \Drupal::service('extension.list.module')->getExtensionInfo('azure_ad');
    return $modules_info['version'];
  }

}
