<?php

declare(strict_types=1);

namespace Drupal\Tests\simple_interactive_maps\Unit;

use Drupal\simple_interactive_maps\Entity\InteractiveMap;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests the InteractiveMap config entity's non-storage-dependent behavior.
 */
#[Group('simple_interactive_maps')]
#[CoversClass(InteractiveMap::class)]
final class InteractiveMapTest extends UnitTestCase {

  /**
   * Tests that identical groups compare equal.
   */
  public function testIdenticalGroupsCompareEqual(): void {
    $groups = [
      'group_a' => ['label' => 'Group A', 'fill_color' => '#ffffff'],
    ];
    $map = new InteractiveMap(['groups' => $groups], 'interactive_map');
    $this->assertTrue($map->compareGroupsData($groups));
  }

  /**
   * Tests that groups with different values compare unequal.
   */
  public function testGroupsWithDifferentValuesCompareUnequal(): void {
    $map = new InteractiveMap([
      'groups' => ['group_a' => ['label' => 'Group A', 'fill_color' => '#ffffff']],
    ], 'interactive_map');

    $changed = ['group_a' => ['label' => 'Group A', 'fill_color' => '#000000']];
    $this->assertFalse($map->compareGroupsData($changed));
  }

  /**
   * Tests that groups with different key counts compare unequal.
   */
  public function testGroupsWithDifferentKeyCountsCompareUnequal(): void {
    $map = new InteractiveMap([
      'groups' => ['group_a' => ['label' => 'Group A']],
    ], 'interactive_map');

    $extra = [
      'group_a' => ['label' => 'Group A'],
      'group_b' => ['label' => 'Group B'],
    ];
    $this->assertFalse($map->compareGroupsData($extra));
  }

  /**
   * Tests that nested arrays are compared recursively.
   */
  public function testNestedArraysAreComparedRecursively(): void {
    $map = new InteractiveMap([
      'groups' => [
        'group_a' => [
          'label' => 'Group A',
          'action' => ['plugin_id' => 'none', 'plugin_configuration' => []],
        ],
      ],
    ], 'interactive_map');

    $same = [
      'group_a' => [
        'label' => 'Group A',
        'action' => ['plugin_id' => 'none', 'plugin_configuration' => []],
      ],
    ];
    $this->assertTrue($map->compareGroupsData($same));

    $different = [
      'group_a' => [
        'label' => 'Group A',
        'action' => ['plugin_id' => 'navigate_action', 'plugin_configuration' => []],
      ],
    ];
    $this->assertFalse($map->compareGroupsData($different));
  }

  /**
   * Tests getEmbedCode().
   */
  public function testGetEmbedCode(): void {
    $map = new InteractiveMap(['id' => 'us_states'], 'interactive_map');
    $this->assertSame('[interactive_map map=us_states]', $map->getEmbedCode());
  }

  /**
   * Tests setRegions()/getRegions().
   */
  public function testSetAndGetRegions(): void {
    $map = new InteractiveMap([], 'interactive_map');
    $this->assertSame([], $map->getRegions());

    $regions = ['AL' => ['label' => 'Alabama']];
    $map->setRegions($regions);
    $this->assertSame($regions, $map->getRegions());
  }

  /**
   * Tests deleteGroup().
   */
  public function testDeleteGroup(): void {
    $map = new InteractiveMap([
      'groups' => [
        'group_a' => ['label' => 'Group A'],
        'group_b' => ['label' => 'Group B'],
      ],
    ], 'interactive_map');

    $map->deleteGroup('group_a');
    $this->assertSame(['group_b' => ['label' => 'Group B']], $map->getGroups());
  }

}
