<?php

namespace Drupal\api_monitor_client\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Filter form for the status page.
 */
class StatusFilterForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'api_monitor_status_filter_form';
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  /**
   * Constructor.
   */
  public function __construct(
    protected EntityTypeManagerInterface $entityTypeManager
  ) {}

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Standard filter form usually uses GET.
    $form['#method'] = 'get';

    $providers = $this->entityTypeManager->getStorage('api_monitor_provider')->loadMultiple();
    $options = ['' => $this->t('- All Providers -')];
    foreach ($providers as $provider) {
      $options[$provider->id()] = $provider->label() . ' (' . $provider->getProviderUrl() . ')';
    }

    $form['provider'] = [
      '#type' => 'select',
      '#title' => $this->t('Provider URL'),
      '#options' => $options,
      '#default_value' => \Drupal::request()->query->get('provider'),
    ];

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Filter'),
    ];
    
    // For GET forms, we don't need a custom redirect in submitForm
    // unless we want to clean up the URL.
    
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // For GET forms, this is not strictly necessary as the browser
    // will append the values to the URL.
  }

}
