<?php

declare(strict_types=1);

namespace Drupal\flowdrop_job\Form;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\flowdrop_job\Constants\JobStatus;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Filter form for the job admin list.
 *
 * @internal
 */
class JobFilterForm extends FormBase {

  public function __construct(
    private readonly EntityTypeManagerInterface $entityTypeManager,
  ) {}

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

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'flowdrop_job_filter_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {
    $request = $this->getRequest();

    $form['#method'] = 'get';
    $form['#action'] = Url::fromRoute('flowdrop.jobs')->toString();
    $form['#attributes']['class'][] = 'views-exposed-form';

    $form['filters'] = [
      '#type' => 'container',
      '#attributes' => ['class' => ['form--inline', 'clearfix']],
    ];

    $form['filters']['status'] = [
      '#type' => 'select',
      '#title' => $this->t('Status'),
      '#options' => ['' => $this->t('- Any status -')] + JobStatus::allowedValues(),
      '#default_value' => $request->query->get('status', ''),
    ];

    $nodeTypes = $this->entityTypeManager
      ->getStorage('flowdrop_node_type')
      ->loadMultiple();
    $nodeTypeOptions = ['' => $this->t('- Any node type -')];
    foreach ($nodeTypes as $nodeType) {
      $nodeTypeOptions[$nodeType->id()] = $nodeType->label();
    }
    asort($nodeTypeOptions);

    $form['filters']['node_type'] = [
      '#type' => 'select',
      '#title' => $this->t('Node Type'),
      '#options' => $nodeTypeOptions,
      '#default_value' => $request->query->get('node_type', ''),
    ];

    $form['filters']['date_from'] = [
      '#type' => 'date',
      '#title' => $this->t('Created from'),
      '#default_value' => $request->query->get('date_from', ''),
    ];

    $form['filters']['date_to'] = [
      '#type' => 'date',
      '#title' => $this->t('Created to'),
      '#default_value' => $request->query->get('date_to', ''),
    ];

    $form['filters']['actions'] = [
      '#type' => 'actions',
    ];

    $form['filters']['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Filter'),
    ];

    $form['filters']['actions']['reset'] = [
      '#type' => 'link',
      '#title' => $this->t('Reset'),
      '#url' => Url::fromRoute('flowdrop.jobs'),
      '#attributes' => ['class' => ['button']],
    ];

    $form['form_build_id']['#access'] = FALSE;
    $form['form_token']['#access'] = FALSE;
    $form['form_id']['#access'] = FALSE;

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {}

}
