<?php

declare(strict_types=1);

namespace Drupal\Tests\simple_interactive_maps\Functional;

use Drupal\simple_interactive_maps\Entity\InteractiveMap;
use Drupal\Tests\BrowserTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Regression guard for InteractiveMapBlock's entity-storage refactor.
 *
 * Storage is now fetched from EntityTypeManagerInterface at each call site
 * instead of being cached as a class property.
 */
#[Group('simple_interactive_maps')]
#[RunTestsInSeparateProcesses]
final class InteractiveMapBlockTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

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

  /**
   * Seeds every region the plugin provides.
   *
   * Mirrors the way InteractiveMapForm::save() does, so MapBuilder does not
   * hit unconfigured region keys.
   */
  private function createFullyConfiguredMap(): InteractiveMap {
    $pluginManager = \Drupal::service('plugin.manager.map_definition');
    $plugin = $pluginManager->createInstance('us_states_territories');

    $regions = [];
    foreach ($plugin->mapData() as $key => $value) {
      $regions[$key] = [
        'id' => $key,
        'label' => $value['label'],
        'hidden' => 0,
        'group' => '',
        'fill_color' => '#4ba0a6',
        'hover_color' => '#076369',
        'stroke_color' => '#e6e6e6',
        'text_color' => '#000000',
        'tooltip' => ['value' => '', 'format' => 'plain_text'],
        'action' => ['plugin_id' => 'none', 'plugin_configuration' => []],
      ];
    }

    $map = InteractiveMap::create([
      'id' => 'test_map',
      'label' => 'Test Map',
      'base_map' => 'us_states_territories',
      'regions' => $regions,
    ]);
    $map->save();

    return $map;
  }

  /**
   * Tests that the block renders and carries the map's cache tags.
   */
  public function testBlockRendersAndCarriesTheMapCacheTags(): void {
    $this->createFullyConfiguredMap();

    $account = $this->drupalCreateUser(['administer blocks', 'access content']);
    $this->drupalLogin($account);

    $this->drupalPlaceBlock('simple_interactive_maps_interactive_map', [
      'map' => 'test_map',
      'show_description' => FALSE,
    ]);

    $this->drupalGet('<front>');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->responseContains('data-map-id="test_map"');

    // InteractiveMapBlock::getCacheTags() merges the map's own cache tags
    // in (via Cache::mergeTags(parent::getCacheTags(), $map->getCacheTags())),
    // which is only reachable through the entity-storage lookup this test
    // guards.
    $cacheTagsHeader = $this->getSession()->getResponseHeader('X-Drupal-Cache-Tags');
    $this->assertNotNull($cacheTagsHeader);
    $this->assertStringContainsString('config:simple_interactive_maps.interactive_map.test_map', $cacheTagsHeader);
  }

}
