<?php

namespace Drupal\user_provisioning\ProviderSpecific\ResponseProcessor\UserResponseProcessor;

use Drupal\user_provisioning\Helpers\moUserProvisioningLogger;
use Drupal\user_provisioning\moUserProvisioningConstants;
use Drupal\user_provisioning\ProviderSpecific\ResponseProcessor\moResourceResponseProcessorInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * Processes user API responses returned by Okta.
 */
class moUserOktaResponseProcessor implements moResourceResponseProcessorInterface {

  /**
   * Logger for response processing diagnostics.
   *
   * @var \Drupal\user_provisioning\Helpers\moUserProvisioningLogger
   *   Logger instance.
   */
  protected moUserProvisioningLogger $moLogger;

  public function __construct() {
    $this->moLogger = new moUserProvisioningLogger();
  }

  /**
   * Processes Okta GET responses.
   *
   * @param \Psr\Http\Message\ResponseInterface $response
   *   Response received after the API call.
   *
   * @return array
   *   Status code, response content, and conflict status.
   */
  public function get(ResponseInterface $response): array {
    $status_code = $response->getStatusCode();
    $content = $response->getBody()->getContents();

    // Initializing as conflict is not determined.
    $conflict = moUserProvisioningConstants::OKTA_CONFLICT_UNDETERMINED;
    if ($status_code == 200) {
      $response = json_decode($content, TRUE);
      $user_id = NULL;
      if (isset($response[0]['id'])) {
        $user_id = $response[0]['id'];
      }
      if (is_null($user_id)) {
        // No conflict exists if no matching entity is found.
        $conflict = moUserProvisioningConstants::OKTA_NO_CONFLICT;
        $this->moLogger->addLog('No matching user found, setting conflict status to OKTA_NO_CONFLICT.', __LINE__, __FUNCTION__, __FILE__);
      }
      else {
        // Conflict exists when one or more entities match the requested query.
        $conflict = moUserProvisioningConstants::OKTA_CONFLICT;
        $this->moLogger->addLog('User found, setting conflict status to OKTA_CONFLICT.', __LINE__, __FUNCTION__, __FILE__);
      }
    }
    return [$status_code, $content, $conflict];
  }

  /**
   * Processes Okta POST responses.
   */
  public function post(ResponseInterface $response) {
    $status_code = $response->getStatusCode();
    $content = $response->getBody()->getContents();

    // @todo need to add the content and its details at the database to refer for future api calls.
    return [$status_code, $content];
  }

  /**
   * Processes Okta PATCH responses.
   */
  public function patch(ResponseInterface $response) {
    // @todo Implement patch() method.
  }

  /**
   * Processes Okta PUT responses.
   */
  public function put(ResponseInterface $response) {
    // @todo Implement put() method.
  }

  /**
   * Processes Okta DELETE responses.
   */
  public function delete(ResponseInterface $response) {
    // @todo Implement delete() method.
  }

  /**
   * Processes Okta deactivation responses.
   */
  public function deactivate(ResponseInterface $response) {
    $status_code = $response->getStatusCode();
    $content = $response->getBody()->getContents();
    return [$status_code, $content];
  }

}
