<?php

declare(strict_types=1);

namespace Drupal\api_monitor_client\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;

/**
 * Defines the Api monitor provider entity.
 *
 * @ConfigEntityType(
 *   id = "api_monitor_provider",
 *   label = @Translation("API Monitor Provider"),
 *   label_collection = @Translation("API Monitor Providers"),
 *   label_singular = @Translation("api monitor provider"),
 *   label_plural = @Translation("api monitor providers"),
 *   label_count = @PluralTranslation(
 *     singular = "@count api monitor provider",
 *     plural = "@count api monitor providers",
 *   ),
 *   handlers = {
 *     "list_builder" = "Drupal\api_monitor_client\ApiMonitorProviderListBuilder",
 *     "form" = {
 *       "add" = "Drupal\api_monitor_client\Form\ApiMonitorProviderForm",
 *       "edit" = "Drupal\api_monitor_client\Form\ApiMonitorProviderForm",
 *       "delete" = "Drupal\Core\Entity\EntityDeleteForm",
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
 *     },
 *   },
 *   config_prefix = "api_monitor_provider",
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid",
 *   },
 *   links = {
 *     "add-form" = "/admin/config/development/api-monitoring/providers/add",
 *     "edit-form" = "/admin/config/development/api-monitoring/providers/{api_monitor_provider}/edit",
 *     "delete-form" = "/admin/config/development/api-monitoring/providers/{api_monitor_provider}/delete",
 *     "collection" = "/admin/config/development/api-monitoring/providers",
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *     "provider_url",
 *     "discovery_endpoint",
 *     "token_endpoint",
 *     "auth_enabled",
 *     "client_id",
 *     "client_secret",
 *     "scope",
 *   },
 * )
 */
final class ApiMonitorProvider extends ConfigEntityBase implements ApiMonitorProviderInterface {

  /**
   * The provider ID.
   *
   * @var string
   */
  protected string $id = '';

  /**
   * The provider label.
   *
   * @var string
   */
  protected string $label = '';

  /**
   * The provider URL.
   *
   * @var string
   */
  public string $provider_url = '';

  /**
   * The discovery endpoint.
   *
   * @var string
   */
  public string $discovery_endpoint = '';

  /**
   * The token endpoint.
   *
   * @var string
   */
  public string $token_endpoint = '';

  /**
   * Whether authentication is enabled.
   *
   * @var bool
   */
  public bool $auth_enabled = TRUE;

  /**
   * The client ID.
   *
   * @var string
   */
  protected string $client_id = '';

  /**
   * The client secret (Key ID).
   *
   * @var string
   */
  protected string $client_secret = '';

  /**
   * The scope.
   *
   * @var string
   */
  public string $scope = '';

  /**
   * {@inheritdoc}
   */
  public function getProviderUrl(): string {
    return $this->provider_url;
  }

  /**
   * {@inheritdoc}
   */
  public function getDiscoveryEndpoint(): string {
    return $this->discovery_endpoint;
  }

  /**
   * {@inheritdoc}
   */
  public function getTokenEndpoint(): string {
    return $this->token_endpoint;
  }

  /**
   * {@inheritdoc}
   */
  public function isAuthEnabled(): bool {
    return (bool) $this->auth_enabled;
  }

  /**
   * {@inheritdoc}
   */
  public function getClientId(): string {
    return $this->client_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getClientSecret(): string {
    $key_id = $this->client_secret;
    if (empty($key_id)) {
      return '';
    }

    // Try to retrieve the value from the Key module.
    if (\Drupal::moduleHandler()->moduleExists('key')) {
      $key = \Drupal::service('key.repository')->getKey($key_id);
      if ($key) {
        return $key->getKeyValue();
      }
    }

    return $key_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getScope(): string {
    return $this->scope;
  }

}
