<?php

namespace Drupal\user_provisioning\ProviderSpecific\Parsers\UserParser;

use Drupal\Core\Config\Config;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user_provisioning\Helpers\moUserProvisioningLogger;
use Drupal\user_provisioning\ProviderSpecific\Parsers\moResourceParserInterface;

/**
 * Parses user resources for Keycloak provisioning requests.
 */
class moUserKeycloakParser implements moResourceParserInterface {
  /**
   * Editable Keycloak provisioning configuration.
   *
   * @var \Drupal\Core\Config\Config
   *   Editable configuration object.
   */
  private Config $configFactory;

  /**
   * Immutable user provisioning configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   *   Read-only configuration object.
   */
  private ImmutableConfig $config;

  /**
   * Immutable Keycloak module configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   *   Read-only Keycloak config object.
   */
  private ImmutableConfig $keycloakConfig;

  /**
   * Logger for parser diagnostics.
   *
   * @var \Drupal\user_provisioning\Helpers\moUserProvisioningLogger
   *   Logger instance.
   */
  private moUserProvisioningLogger $moLogger;

  public function __construct() {
    $this->moLogger = new moUserProvisioningLogger();
    $this->configFactory = \Drupal::configFactory()->getEditable('keycloak_user_provisioning.settings');
    $this->config = \Drupal::config('user_provisioning.settings');
    $this->keycloakConfig = \Drupal::config('keycloak_user_provisioning.settings');
  }

  /**
   * Builds the Keycloak user search query string.
   */
  public function search($resource_id): string {
    $this->moLogger->addLog('Create search request parameter', __LINE__, __FUNCTION__, __FILE__);
    return '?email=' . $resource_id;
  }

  /**
   * {@inheritDoc}
   */
  #[\Override] public function get($resource_id) {
    // @todo Implement get() method.
  }

  /**
   * Builds a Keycloak POST request body for a Drupal user entity.
   */
  public function post(EntityInterface $entity) {
    $email = $entity->getEmail();

    $username = $entity->getAccountName();

    $first_name = $this->keycloakConfig->get('keycloak_user_provisioning_basic_first_name_mapping');
    $first_name = !empty($first_name) ? $entity->get($first_name)->value : $entity->getDisplayName();
    $last_name = $this->keycloakConfig->get('keycloak_user_provisioning_basic_last_name_mapping');
    $last_name = !empty($last_name) ? $entity->get($last_name)->value : $entity->getDisplayName();
    $enabled = $entity->isActive();

    // Prepare user data for Keycloak.
    return [
      "createdTimestamp" => round(microtime(TRUE) * 1000),
      "username" => $username,
      "enabled" => $enabled,
      "totp" => FALSE,
      "emailVerified" => TRUE,
      "firstName" => $first_name,
      "lastName" => $last_name,
      "email" => $email,
      "disableableCredentialTypes" => [],
      "requiredActions" => [],
      "notBefore" => 0,
      "access" => [
        "manageGroupMembership" => TRUE,
        "view" => TRUE,
        "mapRoles" => TRUE,
        "impersonate" => TRUE,
        "manage" => TRUE,
      ],
      "realmRoles" => ["mb-user"],
    ];
  }

  /**
   * {@inheritDoc}
   */
  #[\Override] public function put(array $resource) {
    // @todo Implement put() method.
  }

  /**
   * {@inheritDoc}
   */
  #[\Override] public function patch(array $resource) {
    // @todo Implement patch() method.
  }

  /**
   * {@inheritDoc}
   */
  #[\Override] public function delete(array $resource) {
    // @todo Implement delete() method.
  }

  /**
   * {@inheritDoc}
   */
  #[\Override] public function deactivate(EntityInterface $entity) {
    // @todo Implement deactivate() method.
  }

}
