<?php

declare(strict_types=1);

namespace Drupal\Tests\simple_interactive_maps\Kernel;

use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Regression guard for the annotation -> PHP Attribute plugin migration.
 *
 * Covers Drupal\simple_interactive_maps\MapDefinitionPluginManager and the
 * MapDefinition attribute. Both congressional_districts_118 and
 * us_state_county_maps ship map_definition plugins purely as generated PHP
 * classes with #[MapDefinition(...)] attributes and no Yaml/config
 * counterpart, so if attribute discovery silently breaks (wrong attribute
 * class passed to the plugin manager, wrong dual-discovery wiring, a typo in
 * a generated file) these plugins simply vanish with no obvious error
 * elsewhere - this test is what would catch that.
 */
#[Group('simple_interactive_maps')]
#[RunTestsInSeparateProcesses]
final class MapDefinitionPluginDiscoveryTest extends KernelTestBase {

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

  /**
   * Tests that all generated map plugins are discovered.
   */
  public function testAllGeneratedMapPluginsAreDiscovered(): void {
    $manager = $this->container->get('plugin.manager.map_definition');
    $definitions = $manager->getDefinitions();

    // 1 in the main module (UnitedStates) + 52 congressional district maps
    // (50 states + DC + "undefined") + 56 county maps (50 states + DC + 5
    // territories).
    $this->assertCount(1 + 52 + 56, $definitions);

    $this->assertArrayHasKey('us_states_territories', $definitions);
    $this->assertArrayHasKey('alabama_congressional_districts', $definitions);
    $this->assertArrayHasKey('alabama_counties', $definitions);
  }

  /**
   * Tests the fields of a congressional district map definition.
   */
  public function testCongressionalDistrictDefinitionFields(): void {
    $manager = $this->container->get('plugin.manager.map_definition');
    $definition = $manager->getDefinition('alabama_congressional_districts');

    $this->assertSame('alabama_congressional_districts', $definition['id']);
    $this->assertSame('Alabama Congressional Districts Map', (string) $definition['label']);
    $this->assertSame('A map of Alabama Congressional Districts data', (string) $definition['description']);
    $this->assertTrue($definition['uses_default_tooltip']);
    $this->assertSame('@label', $definition['default_tooltip']);
    $this->assertSame('US Census Bureau 118th Congressional District Maps', $definition['map_category']);
  }

  /**
   * Tests the fields of a county map definition.
   */
  public function testCountyMapDefinitionFields(): void {
    $manager = $this->container->get('plugin.manager.map_definition');
    $definition = $manager->getDefinition('alabama_counties');

    $this->assertSame('alabama_counties', $definition['id']);
    $this->assertSame('Alabama Counties Map', (string) $definition['label']);
    $this->assertSame('US Census Bureau County Maps', $definition['map_category']);
  }

  /**
   * Tests that the main module's own plugin uses attribute defaults.
   *
   * It omits map_category/default_tooltip and relies on the attribute's
   * declared defaults.
   */
  public function testUnitedStatesDefinitionUsesAttributeDefaults(): void {
    $manager = $this->container->get('plugin.manager.map_definition');
    $definition = $manager->getDefinition('us_states_territories');

    $this->assertSame('US States Map', (string) $definition['label']);
    $this->assertFalse($definition['uses_default_tooltip']);
    $this->assertSame('', $definition['default_tooltip']);
    $this->assertSame('', $definition['map_category']);
  }

  /**
   * Tests that map action plugins are discovered.
   */
  public function testMapActionPluginsAreDiscovered(): void {
    $manager = $this->container->get('plugin.manager.map_action');
    $definitions = $manager->getDefinitions();

    $this->assertArrayHasKey('none', $definitions);
    $this->assertArrayHasKey('ajax_load_map', $definitions);
    $this->assertArrayHasKey('navigate_action', $definitions);
    $this->assertArrayHasKey('modal_content', $definitions);

    $this->assertTrue($definitions['none']['is_system']);
    $this->assertSame(-100, $definitions['none']['weight']);
  }

}
