<?php

declare(strict_types=1);

namespace Drupal\Tests\nodehive_core\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\nodehive_core\Entity\Space;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests that the nodehive_space.content field and its sync hooks are removed.
 *
 * These tests are expected to FAIL before the removal and PASS after.
 * They act as a guard against accidental re-introduction of the antipattern.
 */
#[CoversNothing]
#[Group('nodehive_core')]
class SpaceContentFieldRemovedTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   *
   * @var string[]
   */
  protected static $modules = [
    'system',
    'user',
    'field',
    'text',
    'filter',
    'node',
    'link',
    'options',
    'image',
    'file',
    'taxonomy',
    'menu_ui',
    'nodehive_core',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('nodehive_space');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->installSchema('node', ['node_access']);
  }

  /**
   * Tests that the Space entity no longer exposes a 'content' field.
   *
   * Before removal: FAILS (field exists).
   * After removal:  PASSES (field gone).
   */
  public function testSpaceHasNoContentField(): void {
    $space = Space::create([
      'label' => 'Test Space',
      'machine_name' => 'test_space',
    ]);
    $space->save();

    self::assertFalse(
      $space->hasField('content'),
      'Space entity must not have a content field — load content via node query instead.'
    );
  }

  /**
   * Tests that the sync hooks keeping space->content in sync are removed.
   *
   * Before removal: FAILS (hooks exist).
   * After removal:  PASSES (hooks gone).
   */
  public function testSyncHooksNoLongerExist(): void {
    self::assertFalse(
      function_exists('nodehive_core_node_insert'),
      'nodehive_core_node_insert must be removed — space->content sync is no longer needed.'
    );

    self::assertFalse(
      function_exists('nodehive_core_node_update'),
      'nodehive_core_node_update must be removed — space->content sync is no longer needed.'
    );

    self::assertFalse(
      function_exists('nodehive_core_node_delete'),
      'nodehive_core_node_delete must be removed — space->content sync is no longer needed.'
    );
  }

}
