<?php

declare(strict_types=1);

namespace Drupal\Tests\simple_interactive_maps\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\simple_interactive_maps\Entity\InteractiveMap;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Regression guard for the check_markup() -> renderInIsolation() migration.
 *
 * Covers the RendererInterface::renderInIsolation() migration in
 * MapDataLoader::mergeConfigData().
 */
#[Group('simple_interactive_maps')]
#[CoversClass(\Drupal\simple_interactive_maps\MapDataLoader::class)]
#[RunTestsInSeparateProcesses]
final class MapDataLoaderTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'filter',
    'field_group',
    'simple_interactive_maps',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->installConfig(['filter']);
  }

  /**
   * Creates a test InteractiveMap entity with the given regions/groups.
   */
  private function createMap(array $regions, array $groups = []): InteractiveMap {
    $map = InteractiveMap::create([
      'id' => 'test_map',
      'label' => 'Test Map',
      'base_map' => 'us_states_territories',
      'regions' => $regions,
      'groups' => $groups,
    ]);
    $map->save();

    return $map;
  }

  /**
   * Tests that a region with no action is merged and its tooltip rendered.
   */
  public function testRegionWithNoActionIsMergedAndTooltipIsRendered(): void {
    $map = $this->createMap([
      'AL' => [
        'id' => 'AL',
        'label' => 'Alabama',
        'hidden' => 0,
        'group' => '',
        'fill_color' => '#111111',
        'hover_color' => '#222222',
        'stroke_color' => '#333333',
        'text_color' => '#444444',
        'tooltip' => ['value' => 'Alabama tooltip', 'format' => 'plain_text'],
        'action' => ['plugin_id' => 'none', 'plugin_configuration' => []],
      ],
    ]);

    /** @var \Drupal\simple_interactive_maps\MapDataLoader $mapDataLoader */
    $mapDataLoader = $this->container->get('simple_interactive_maps.map_data_loader');
    $data = $mapDataLoader->loadMapData($map);

    $this->assertArrayHasKey('AL', $data);
    $this->assertSame('none', $data['AL']['action']);
    $this->assertSame([], $data['AL']['action_configuration']);
    $this->assertSame('#111111', $data['AL']['fill_color']);
    $this->assertStringContainsString('Alabama tooltip', (string) $data['AL']['tooltip']['processed']);
  }

  /**
   * Tests that a navigate_action region resolves its plugin configuration.
   */
  public function testRegionWithNavigateActionResolvesPluginConfiguration(): void {
    $map = $this->createMap([
      'AL' => [
        'id' => 'AL',
        'label' => 'Alabama',
        'hidden' => 0,
        'group' => '',
        'fill_color' => '#111111',
        'hover_color' => '#222222',
        'stroke_color' => '#333333',
        'text_color' => '#444444',
        'tooltip' => ['value' => '', 'format' => 'plain_text'],
        'action' => [
          'plugin_id' => 'navigate_action',
          'plugin_configuration' => ['url' => 'https://example.com', 'new_tab' => TRUE],
        ],
      ],
    ]);

    /** @var \Drupal\simple_interactive_maps\MapDataLoader $mapDataLoader */
    $mapDataLoader = $this->container->get('simple_interactive_maps.map_data_loader');
    $data = $mapDataLoader->loadMapData($map);

    $this->assertSame('navigate_action', $data['AL']['action']);
    $this->assertSame('https://example.com', $data['AL']['action_configuration']['url']);
    $this->assertTrue($data['AL']['action_configuration']['new_tab']);
  }

  /**
   * Tests that a group color override is applied.
   */
  public function testGroupColorOverrideIsApplied(): void {
    $map = $this->createMap(
      [
        'AL' => [
          'id' => 'AL',
          'label' => 'Alabama',
          'hidden' => 0,
          'group' => 'group_a',
          'fill_color' => '#111111',
          'hover_color' => '#222222',
          'stroke_color' => '#333333',
          'text_color' => '#444444',
          'tooltip' => ['value' => '', 'format' => 'plain_text'],
          'action' => ['plugin_id' => 'none', 'plugin_configuration' => []],
        ],
      ],
      [
        'group_a' => [
          'override_colors' => TRUE,
          'fill_color' => '#aaaaaa',
          'hover_color' => '#bbbbbb',
          'stroke_color' => '#cccccc',
          'text_color' => '#dddddd',
          'override_tooltip' => FALSE,
          'override_action' => FALSE,
        ],
      ],
    );

    /** @var \Drupal\simple_interactive_maps\MapDataLoader $mapDataLoader */
    $mapDataLoader = $this->container->get('simple_interactive_maps.map_data_loader');
    $data = $mapDataLoader->loadMapData($map);

    $this->assertSame('#aaaaaa', $data['AL']['fill_color']);
    $this->assertSame('#bbbbbb', $data['AL']['hover_color']);
  }

  /**
   * Tests that regions not configured on the map are left untouched.
   */
  public function testRegionsNotConfiguredOnTheMapAreLeftUntouched(): void {
    $map = $this->createMap([]);

    /** @var \Drupal\simple_interactive_maps\MapDataLoader $mapDataLoader */
    $mapDataLoader = $this->container->get('simple_interactive_maps.map_data_loader');
    $data = $mapDataLoader->loadMapData($map);

    $this->assertArrayHasKey('AL', $data);
    $this->assertArrayNotHasKey('action_configuration', $data['AL']);
  }

}
