<?php

declare(strict_types=1);

namespace Drupal\flowdrop_workflow\Hook;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\flowdrop_workflow\Entity\FlowDropWorkflow;
use Psr\Log\LoggerInterface;

/**
 * Hook implementations for the flowdrop_workflow module.
 *
 * @internal This class is not part of the public API and may change without notice.
 */
final class Hooks {

  use StringTranslationTrait;

  /**
   * Constructs a new Hooks object.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger channel.
   * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
   *   The current route match.
   */
  public function __construct(
    protected readonly LoggerInterface $logger,
    protected readonly RouteMatchInterface $routeMatch,
  ) {}

  /**
   * Implements hook_theme().
   */
  #[Hook('theme')]
  public function theme($existing, $type, $theme, $path): array {
    return [
      'html__flowdrop_workflow_fullscreen' => [
        'render element' => 'elements',
        'base hook' => 'html',
        'template' => 'html--flowdrop-workflow-fullscreen',
      ],
      'page__flowdrop_workflow_editor' => [
        'render element' => 'elements',
        'base hook' => 'page',
        'template' => 'page--flowdrop-workflow-editor',
      ],
    ];
  }

  /**
   * Implements hook_theme_suggestions_html_alter().
   */
  #[Hook('theme_suggestions_html_alter')]
  public function themeSuggestionsHtmlAlter(array &$suggestions, array $variables): void {
    $route_name = $this->routeMatch->getRouteName();
    if ($route_name === 'flowdrop.workflow.editor') {
      $suggestions[] = 'html__flowdrop_workflow_fullscreen';
    }
  }

  /**
   * Implements hook_theme_suggestions_page_alter().
   */
  #[Hook('theme_suggestions_page_alter')]
  public function themeSuggestionsPageAlter(array &$suggestions, array $variables): void {
    $route_name = $this->routeMatch->getRouteName();
    if ($route_name === 'flowdrop.workflow.editor') {
      $suggestions[] = 'page__flowdrop_workflow_editor';
    }
  }

  /**
   * Implements hook_entity_operation_alter().
   *
   * Adds an "Open in FlowDrop" operation to FlowDropWorkflow entities.
   */
  #[Hook('entity_operation_alter')]
  public function entityOperationAlter(array &$operations, EntityInterface $entity): void {
    if (!$entity instanceof FlowDropWorkflow) {
      return;
    }

    if ($entity->id()) {
      try {
        $operations['open_in_flowdrop'] = [
          'title' => $this->t('Open in FlowDrop'),
          'weight' => -1,
          'url' => Url::fromRoute('flowdrop.workflow.editor', [
            'flowdrop_workflow' => $entity->id(),
          ]),
        ];
      }
      catch (\Exception $e) {
        $this->logger->error('Failed to generate Open in Editor URL for entity @id: @message', [
          '@id' => $entity->id(),
          '@message' => $e->getMessage(),
        ]);
      }
    }
  }

}
