<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop_memory\Unit\Plugin\FlowDropNodeProcessor;

use Drupal\flowdrop\DTO\ParameterBagInterface;
use Drupal\flowdrop_memory\Plugin\FlowDropNodeProcessor\MemoryReadRaw;
use Drupal\flowdrop_memory\Service\MemoryManager;
use Drupal\Tests\UnitTestCase;
use Prophecy\PhpUnit\ProphecyTrait;

/**
 * Tests the MemoryReadRaw node processor.
 *
 * @coversDefaultClass \Drupal\flowdrop_memory\Plugin\FlowDropNodeProcessor\MemoryReadRaw
 * @group flowdrop_memory
 */
class MemoryReadRawTest extends UnitTestCase {

  use ProphecyTrait;

  /**
   * The memory manager mock.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy|\Drupal\flowdrop_memory\Service\MemoryManager
   */
  private $memoryManager;

  /**
   * The processor under test.
   *
   * @var \Drupal\flowdrop_memory\Plugin\FlowDropNodeProcessor\MemoryReadRaw
   */
  private MemoryReadRaw $processor;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->memoryManager = $this->prophesize(MemoryManager::class);
    $this->processor = new MemoryReadRaw(
      [],
      'memory_read_raw',
      ['id' => 'memory_read_raw'],
      $this->memoryManager->reveal(),
    );
  }

  /**
   * Stubs a ParameterBagInterface prophecy.
   */
  private function stubParams(string $scope, string $scopeId, string $key, mixed $default = NULL) {
    $params = $this->prophesize(ParameterBagInterface::class);
    $params->getString('scope')->willReturn($scope);
    $params->getString('scope_id', '')->willReturn($scopeId);
    $params->getString('key')->willReturn($key);
    $params->get('default_value')->willReturn($default);
    $params->getString('backend', 'static')->willReturn('static');
    return $params;
  }

  /**
   * @covers ::validateParams
   */
  public function testValidateParamsFailsWhenScopeAndKeyBothMissing(): void {
    $result = $this->processor->validateParams([]);
    $this->assertFalse($result->isValid());
  }

  /**
   * @covers ::validateParams
   */
  public function testValidateParamsFailsWhenScopeMissing(): void {
    $result = $this->processor->validateParams(['key' => 'my_key']);
    $this->assertFalse($result->isValid());
  }

  /**
   * @covers ::validateParams
   */
  public function testValidateParamsFailsWhenScopeIsEmpty(): void {
    $result = $this->processor->validateParams(['scope' => '', 'key' => 'my_key']);
    $this->assertFalse($result->isValid());
  }

  /**
   * @covers ::validateParams
   */
  public function testValidateParamsFailsWhenKeyMissing(): void {
    $result = $this->processor->validateParams(['scope' => 'global']);
    $this->assertFalse($result->isValid());
  }

  /**
   * @covers ::validateParams
   */
  public function testValidateParamsFailsWhenKeyIsEmpty(): void {
    $result = $this->processor->validateParams(['scope' => 'global', 'key' => '']);
    $this->assertFalse($result->isValid());
  }

  /**
   * @covers ::validateParams
   */
  public function testValidateParamsSucceedsWithScopeAndKey(): void {
    $result = $this->processor->validateParams(['scope' => 'global', 'key' => 'my_key']);
    $this->assertTrue($result->isValid());
  }

  /**
   * @covers ::validateParams
   */
  public function testValidateParamsSucceedsWithCustomScope(): void {
    $result = $this->processor->validateParams(['scope' => 'custom_scope', 'key' => 'k']);
    $this->assertTrue($result->isValid());
  }

  /**
   * @covers ::process
   */
  public function testProcessReadWithExplicitScopeId(): void {
    $params = $this->stubParams('workflow', 'wf-explicit', 'counter');
    $this->memoryManager->get('workflow', 'wf-explicit', 'counter', NULL, 'static')->willReturn(10);
    $this->memoryManager->has('workflow', 'wf-explicit', 'counter', 'static')->willReturn(TRUE);

    $output = $this->processor->process($params->reveal());

    $this->assertSame(10, $output['value']);
    $this->assertTrue($output['found']);
  }

  /**
   * @covers ::process
   */
  public function testProcessDefaultsToEmptyScopeId(): void {
    $params = $this->stubParams('global', '', 'key');
    $this->memoryManager->get('global', '', 'key', NULL, 'static')->willReturn('val');
    $this->memoryManager->has('global', '', 'key', 'static')->willReturn(TRUE);

    $output = $this->processor->process($params->reveal());

    $this->assertSame('val', $output['value']);
  }

  /**
   * @covers ::process
   */
  public function testProcessReturnsDefaultWhenNotFound(): void {
    $params = $this->stubParams('global', '', 'missing', 'my_default');
    $this->memoryManager->get('global', '', 'missing', 'my_default', 'static')->willReturn('my_default');
    $this->memoryManager->has('global', '', 'missing', 'static')->willReturn(FALSE);

    $output = $this->processor->process($params->reveal());

    $this->assertSame('my_default', $output['value']);
    $this->assertFalse($output['found']);
  }

  /**
   * @covers ::process
   */
  public function testProcessReturnsScopeAndKeyInOutput(): void {
    $params = $this->stubParams('custom', 'id-1', 'my_key');
    $this->memoryManager->get('custom', 'id-1', 'my_key', NULL, 'static')->willReturn(NULL);
    $this->memoryManager->has('custom', 'id-1', 'my_key', 'static')->willReturn(FALSE);

    $output = $this->processor->process($params->reveal());

    $this->assertSame('custom', $output['scope']);
    $this->assertSame('my_key', $output['key']);
  }

  /**
   * @covers ::process
   */
  public function testProcessDoesNotExposeResolvedScopeId(): void {
    // MemoryReadRaw does not auto-resolve scope_id from context.
    $params = $this->stubParams('global', '', 'key');
    $this->memoryManager->get('global', '', 'key', NULL, 'static')->willReturn(NULL);
    $this->memoryManager->has('global', '', 'key', 'static')->willReturn(FALSE);

    $output = $this->processor->process($params->reveal());

    $this->assertArrayNotHasKey('resolved_scope_id', $output);
  }

  /**
   * @covers ::process
   */
  public function testProcessExposesAllRequiredOutputKeys(): void {
    $params = $this->stubParams('global', '', 'k');
    $this->memoryManager->get('global', '', 'k', NULL, 'static')->willReturn(NULL);
    $this->memoryManager->has('global', '', 'k', 'static')->willReturn(FALSE);

    $output = $this->processor->process($params->reveal());

    $this->assertArrayHasKey('value', $output);
    $this->assertArrayHasKey('found', $output);
    $this->assertArrayHasKey('key', $output);
    $this->assertArrayHasKey('scope', $output);
  }

}
