<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media_ui\Plugin\Action;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\ffmpeg_media_ui\Service\MediaOperationsService;
use Drupal\ffmpeg_media_ui\Service\MediaOperationsServiceInterface;
use Drupal\media\MediaInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
 * Action to clear transcoded outputs from media.
 */
#[Action(
  id: 'ffmpeg_media_clear_outputs',
  label: new TranslatableMarkup('Clear video outputs'),
  type: 'media',
)]
final class ClearOutputsAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

  /**
   * Constructs a ClearOutputsAction object.
   *
   * @param array $configuration
   *   Plugin configuration.
   * @param string $plugin_id
   *   Plugin ID.
   * @param mixed $plugin_definition
   *   Plugin definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   * @param \Drupal\ffmpeg_media_ui\Service\MediaOperationsServiceInterface $mediaOperations
   *   Media operations service.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    #[Autowire(service: 'entity_type.manager')]
    protected readonly EntityTypeManagerInterface $entityTypeManager,
    #[Autowire(service: MediaOperationsService::class)]
    protected readonly MediaOperationsServiceInterface $mediaOperations,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   *
   * @param object|null $object
   *   Object to execute this action on.
   */
  #[\Override]
  public function execute($object = NULL): void {
    if (!$object instanceof MediaInterface) {
      return;
    }

    $deleteFiles = $this->configuration['delete_files'] ?? TRUE;
    $this->mediaOperations->clearOutputs($object, $deleteFiles);
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function defaultConfiguration(): array {
    return [
      'delete_files' => TRUE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
    $form['delete_files'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Delete files from disk'),
      '#default_value' => $this->configuration['delete_files'],
      '#description' => $this->t('Also delete the actual files, not just the field references.'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
    $this->configuration['delete_files'] = $form_state->getValue('delete_files');
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
    /** @var \Drupal\media\MediaInterface $object */
    $access = $object->access('update', $account, TRUE);

    if ($account !== NULL) {
      $access = $access->andIf(AccessResult::allowedIfHasPermission($account, 'manage media outputs'));
    }

    return $return_as_object ? $access : $access->isAllowed();
  }

}
