<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\ffmpeg_media\Service\ProfileManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Confirmation form for setting a default conversion profile.
 */
final class ProfileSetDefaultForm extends ConfirmFormBase {

  /**
   * The profile ID to set as default.
   */
  protected string $profileId;

  /**
   * The profile label.
   */
  protected string $profileLabel;

  /**
   * Constructs a ProfileSetDefaultForm object.
   *
   * @param \Drupal\ffmpeg_media\Service\ProfileManagerInterface $profileManager
   *   The profile manager service.
   */
  public function __construct(
    protected readonly ProfileManagerInterface $profileManager,
  ) {}

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public static function create(ContainerInterface $container): static {
    return new static(
      $container->get(ProfileManagerInterface::class),
    );
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function getFormId(): string {
    return 'ffmpeg_media_profile_set_default';
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function getQuestion(): TranslatableMarkup {
    return $this->t('Are you sure you want to set %label as the default profile?', [
      '%label' => $this->profileLabel,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function getDescription(): TranslatableMarkup {
    return $this->t('All new conversions will use this profile unless overridden in field formatter settings.');
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function getCancelUrl(): Url {
    return new Url('ffmpeg_media.profile_list');
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function getConfirmText(): TranslatableMarkup {
    return $this->t('Set as default');
  }

  /**
   * {@inheritdoc}
   *
   * @param array<string, mixed> $form
   *   Form structure.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   * @param string|null $profile_id
   *   The profile ID to set as default.
   *
   * @return array<string, mixed>
   *   Form elements.
   */
  #[\Override]
  public function buildForm(array $form, FormStateInterface $form_state, ?string $profile_id = NULL): array {
    if ($profile_id === NULL || !$this->profileManager->profileExists($profile_id)) {
      throw new NotFoundHttpException();
    }

    $this->profileId = $profile_id;
    $profile = $this->profileManager->getProfile($profile_id);
    $this->profileLabel = $profile?->label ?? $profile_id;

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

  /**
   * {@inheritdoc}
   *
   * @param array<string, mixed> $form
   *   Form structure.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  #[\Override]
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    $this->configFactory()->getEditable('ffmpeg_media.settings')
      ->set('default_profile', $this->profileId)
      ->save();

    $this->profileManager->resetCache();

    $this->messenger()->addStatus($this->t('Default profile set to %label.', [
      '%label' => $this->profileLabel,
    ]));

    $form_state->setRedirectUrl($this->getCancelUrl());
  }

}
