<?php

namespace Drupal\azure_ad;

use Drupal\user_provisioning\moUserProvisioningConstants;
use Drupal\user_provisioning\moUserProvisioningCustomer;
use Drupal\user_provisioning\moUserProvisioningUtilities;

/**
 * Handles support and trial queries for the Azure AD module.
 */
class moAzureADSupport {
  /**
   * User email address.
   *
   * @var string|null
   */
  public ?string $email;

  /**
   * User phone number.
   *
   * @var string|null
   */
  public ?string $phone;

  /**
   * Support query message.
   *
   * @var string
   */
  public string $query;

  /**
   * Type of query (Trial Request / Contact Support).
   *
   * @var string
   */
  public string $queryType;

  /**
   * Constructs the Azure AD support object.
   */
  public function __construct(string $query, string $queryType, ?string $email = NULL, ?string $phone = NULL) {
    $this->email = $email;
    $this->phone = $phone;
    $this->query = $query;
    $this->queryType = $queryType;
  }

  /**
   * Sends the support or trial request to miniOrange support.
   */
  public function sendSupportQuery() {
    $modules_info = \Drupal::service('extension.list.module')->getExtensionInfo('azure_ad');
    $modules_version = $modules_info['version'];
    $server_name = \Drupal::request()->getHost();
    if ($this->queryType == 'Trial Request' || $this->queryType == 'Contact Support') {

      $url = moUserProvisioningConstants::MO_NOTIFY_SEND;
      $request_for = $this->queryType == 'Trial Request' ? 'Trial' : 'Support';
      $subject = $request_for . ' request for Drupal-' . \Drupal::VERSION . ' Azure AD Sync Module | ' . $modules_version;
      $this->query = ($this->queryType == 'Contact Support' ? 'Query - ' : $request_for . ' requested for - ') . $this->query;

      $utilities = new MoAzureUtilities();
      [$customerKey, $currentTimeInMillis, $hashValue] = $utilities->customerApiKey();

      $phone = $this->queryType == 'Contact Support' ? '<strong>Support needed for:</strong> ' : 'Phone Number: ';
      $content = '<div >Hello, <br><br>Company :<a href="' . $server_name . '" target="_blank" >' . $server_name . '</a><br><br>' . $phone . $this->phone . '<br><br>Email:<a href="mailto:' . $this->email . '" target="_blank">' . $this->email . '</a><br><br>Query:[DRUPAL ' . moUserProvisioningUtilities::mo_get_drupal_core_version() . ' Azure AD Sync Free | PHP ' . phpversion() . ' | ' . $modules_version . ' ] ' . $this->query . '</div>';

      $fields = [
        'customerKey' => $customerKey,
        'sendEmail' => TRUE,
        'email' => [
          'customerKey' => $customerKey,
          'fromEmail' => $this->email,
          'fromName' => 'miniOrange',
          'toEmail' => moUserProvisioningConstants::SUPPORT_EMAIL,
          'toName' => moUserProvisioningConstants::SUPPORT_EMAIL,
          'subject' => $subject,
          'content' => $content,
        ],
      ];

      $header = [
        'Content-Type' => 'application/json',
        'Customer-Key' => $customerKey,
        'Timestamp' => $currentTimeInMillis,
        'Authorization' => $hashValue,
      ];

    }
    else {
      $this->query = '[Drupal ' . \Drupal::VERSION . ' Azure AD Sync Module | PHP ' . phpversion() . ' | ' . $modules_version . '] ' . $this->query;
      $fields = [
        'company' => $server_name,
        'email' => $this->email,
        'phone' => $this->phone,
        'ccEmail' => moUserProvisioningConstants::SUPPORT_EMAIL,
        'query' => $this->query,
      ];

      $url = moUserProvisioningConstants::CONTACT_US;
      $header = [
        'Content-Type' => 'application/json',
        'charset' => 'UTF-8',
        'Authorization' => 'Basic',
      ];
    }

    $field_string = json_encode($fields);
    $mo_user_provisioning_customer = new moUserProvisioningCustomer($this->email, NULL, NULL, NULL);
    $mo_user_provisioning_customer->callService($url, $field_string, $header);
    return TRUE;
  }

}
