<?php

declare(strict_types=1);

namespace Drupal\ffmpeg_media\Access;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\ffmpeg_media\Service\HealthProbeAccessManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Route access callback for public health probes.
 *
 * @internal
 */
final class HealthProbeAccess implements ContainerInjectionInterface {

  /**
   * Constructs a HealthProbeAccess object.
   *
   * @param \Drupal\ffmpeg_media\Service\HealthProbeAccessManagerInterface $accessManager
   *   The probe access manager.
   */
  public function __construct(
    protected readonly HealthProbeAccessManagerInterface $accessManager,
  ) {}

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

  /**
   * Grants access only when the effective probe policy authorizes the request.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   Access decision with request-scoped cacheability.
   */
  public function access(): AccessResult {
    $accessResult = $this->accessManager->isAccessAuthorized()
      ? AccessResult::allowed()
      : AccessResult::forbidden('Health probe token is required.');

    return $accessResult->setCacheMaxAge(0);
  }

}
