<?php

namespace Drupal\user_provisioning\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user_provisioning\moUserProvisioningUtilities;

/**
 * Provides a confirmation form to reset user provisioning configuration.
 */
class MoUserProvisioningResetConfiguration extends ConfirmFormBase {

  /**
   * Gets the form ID.
   *
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'mo_user_provisioning_reset_configuration';
  }

  /**
   * Gets the confirmation question.
   *
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to reset configuration?');
  }

  /**
   * Gets the description for the confirmation form.
   *
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->t('This action will clear all saved SCIM client settings and mappings. This cannot be undone.');
  }

  /**
   * Gets the confirm button text.
   *
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Confirm reset');
  }

  /**
   * Gets the cancel URL.
   *
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return Url::fromRoute('user_provisioning.provisioning');
  }

  /**
   * Submits the reset configuration form.
   *
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Perform the reset operation.
    moUserProvisioningUtilities::moDeleteConfigurations();

    // Show success message.
    $this->messenger()->addStatus(
          $this->t('User provisioning configuration has been reset successfully.')
      );

    // Redirect back to provisioning page.
    $form_state->setRedirect('user_provisioning.provisioning');
  }

}
