<?php

declare(strict_types=1);

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

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

/**
 * Tests the MemoryDeleteRaw node processor.
 *
 * @coversDefaultClass \Drupal\flowdrop_memory\Plugin\FlowDropNodeProcessor\MemoryDeleteRaw
 * @group flowdrop_memory
 */
class MemoryDeleteRawTest 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\MemoryDeleteRaw
   */
  private MemoryDeleteRaw $processor;

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

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

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

  // ---------------------------------------------------------------------------
  // validateParams
  // ---------------------------------------------------------------------------

  /**
   * @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());
  }

  // ---------------------------------------------------------------------------
  // process — delete behaviour
  // ---------------------------------------------------------------------------

  /**
   * @covers ::process
   */
  public function testProcessCallsDeleteWithExplicitScopeId(): void {
    $params = $this->stubParams('workflow', 'wf-explicit', 'my_key');
    $this->memoryManager->delete('workflow', 'wf-explicit', 'my_key', 'static')->shouldBeCalled();

    $this->processor->process($params->reveal());
  }

  /**
   * @covers ::process
   */
  public function testProcessCallsDeleteWithEmptyScopeId(): void {
    $params = $this->stubParams('global', '', 'chat_history');
    $this->memoryManager->delete('global', '', 'chat_history', 'static')->shouldBeCalled();

    $this->processor->process($params->reveal());
  }

  /**
   * @covers ::process
   */
  public function testProcessCallsDeleteWithCustomScope(): void {
    $params = $this->stubParams('user_data', 'user-42', 'preferences');
    $this->memoryManager->delete('user_data', 'user-42', 'preferences', 'static')->shouldBeCalled();

    $this->processor->process($params->reveal());
  }

  // ---------------------------------------------------------------------------
  // process — output shape
  // ---------------------------------------------------------------------------

  /**
   * @covers ::process
   */
  public function testProcessReturnsTrueForSuccess(): void {
    $params = $this->stubParams('global', '', 'key');
    $this->memoryManager->delete('global', '', 'key', 'static')->shouldBeCalled();

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

    $this->assertTrue($output['success']);
  }

  /**
   * @covers ::process
   */
  public function testProcessReturnsKeyInOutput(): void {
    $params = $this->stubParams('global', '', 'chat_history');
    $this->memoryManager->delete('global', '', 'chat_history', 'static')->shouldBeCalled();

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

    $this->assertSame('chat_history', $output['key']);
  }

  /**
   * @covers ::process
   */
  public function testProcessReturnsScopeInOutput(): void {
    $params = $this->stubParams('custom', 'scope-id', 'key');
    $this->memoryManager->delete('custom', 'scope-id', 'key', 'static')->shouldBeCalled();

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

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

  /**
   * @covers ::process
   */
  public function testProcessDoesNotExposeResolvedScopeId(): void {
    $params = $this->stubParams('global', '', 'key');
    $this->memoryManager->delete('global', '', 'key', 'static')->shouldBeCalled();

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

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

  /**
   * @covers ::process
   */
  public function testProcessExposesAllRequiredOutputKeys(): void {
    $params = $this->stubParams('global', '', 'key');
    $this->memoryManager->delete('global', '', 'key', 'static')->shouldBeCalled();

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

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

}
