<?php

declare(strict_types=1);

namespace Drupal\flowdrop_playground\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Settings form for FlowDrop Playground configuration.
 *
 * This form provides configuration options for the playground module,
 * including auto-run behavior for workflows.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
class PlaygroundSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames(): array {
    return ["flowdrop_playground.settings"];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return "flowdrop_playground_settings_form";
  }

  /**
   * {@inheritdoc}
   *
   * @return array<string, mixed>
   *   The form array.
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $config = $this->config("flowdrop_playground.settings");

    // Execution Settings.
    $form["execution"] = [
      "#type" => "details",
      "#title" => $this->t("Execution"),
      "#open" => TRUE,
      "#tree" => TRUE,
    ];

    $form["execution"]["auto_run"] = [
      "#type" => "checkbox",
      "#title" => $this->t("Auto-run workflows without chat input"),
      "#description" => $this->t("When enabled, workflows without a chat input node will automatically execute when the playground page loads. Disable this to require users to manually click the Run button."),
      "#default_value" => $config->get("auto_run") ?? TRUE,
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->config("flowdrop_playground.settings")
      ->set("auto_run", (bool) $form_state->getValue(["execution", "auto_run"]))
      ->save();

    parent::submitForm($form, $form_state);
  }

}
