<?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\Plugin\Action\EntityActionBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
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;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Action to refresh (re-transcode) media outputs.
 */
#[Action(
  id: 'ffmpeg_media_refresh',
  label: new TranslatableMarkup('Refresh video outputs'),
  type: 'media',
)]
final class RefreshMediaAction extends EntityActionBase {

  /**
   * Constructs a RefreshMediaAction object.
   *
   * @param array $configuration
   *   Plugin configuration.
   * @param string $plugin_id
   *   Plugin ID.
   * @param mixed $plugin_definition
   *   Plugin definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   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')]
    EntityTypeManagerInterface $entity_type_manager,
    #[Autowire(service: MediaOperationsService::class)]
    protected readonly MediaOperationsServiceInterface $mediaOperations,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
  }

  /**
   * {@inheritdoc}
   */
  #[\Override]
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager'),
      $container->get(MediaOperationsService::class),
    );
  }

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

    // Clear existing outputs and re-queue.
    $this->mediaOperations->refresh($object, TRUE);
  }

  /**
   * {@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();
  }

}
