<?php

namespace Drupal\api_monitor_client\Plugin;

use Drupal\Core\Plugin\PluginBase;

/**
 * Base class for API monitor probe plugins.
 */
abstract class ApiMonitorProbeBase extends PluginBase implements ApiMonitorProbeInterface {

  /**
   * Used for custom payload to be sent to the API.
   *
   * @return array
   *   Es. ['id' => 1]
   */
  public function getPayload(): array {
    return [];
  }

  /**
   * Used to validate custom the response from the API.
   *
   * @param $response
   *   The GuzzleHttp\Psr7\Response
   *
   * @return bool
   *   The response is valid or not.
   */
  public function validateResponse($response) {
    $status = $response->getStatusCode();
    return ($status >= 200 && $status < 300);
  }

  /**
   * Used this method to set the route parameters.
   *
   * @return array
   *   Es: ['id' => 1]
   */
  public function getRouteParameters(): array {
    return [];
  }

  /**
   * Used this method to set the query options.
   *
   * Es. ['status' => 'active', 'limit' => 1]
   * Query String ?status=active&limit=1
   *
   * @return array
   *   The query options.
   */
  public function getQueryOptions(): array {
    return [];
  }

  /**
   * Set the uptime of critical alerts required before triggering an action.
   *
   * @return int
   *   The uptime threshold in seconds.
   */
  public function getUptimeThreshold(): int {
    return $this->pluginDefinition['uptime_threshold'] ?? 95;
  }

  /**
   * Set the number of critical alerts required before triggering an action.
   *
   * @return int
   *   The number of critical alerts.
   */
  public function getCriticalAlertCount(): int {
    return $this->pluginDefinition['critical_alert_count'] ?? 3;
  }

  /**
   * {@inheritdoc}
   */
  public function isPiiAuthorized(): bool {
    return $this->pluginDefinition['is_pii_authorized'] ?? FALSE;
  }

}
