<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop_interrupt\Unit\Handler;

use Drupal\flowdrop_interrupt\DTO\ExecutionStamp;
use Drupal\flowdrop_interrupt\DTO\InterruptCreationData;
use Drupal\flowdrop_interrupt\Entity\FlowDropInterruptInterface;
use Drupal\flowdrop_interrupt\Enum\InterruptDirection;
use Drupal\flowdrop_interrupt\Enum\InterruptType;
use Drupal\flowdrop_interrupt\Handler\LinkedInterruptMessageHandler;
use Drupal\flowdrop_interrupt\Message\LinkedInterruptMessage;
use Drupal\flowdrop_interrupt\Service\InterruptFactoryInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

/**
 * @coversDefaultClass \Drupal\flowdrop_interrupt\Handler\LinkedInterruptMessageHandler
 * @group flowdrop_interrupt
 */
class LinkedInterruptMessageHandlerTest extends UnitTestCase {

  use ProphecyTrait;

  /**
   * Tests that supports() returns the message class name.
   */
  public function testSupportsReturnsMessageClass(): void {
    $this->assertSame(LinkedInterruptMessage::class, LinkedInterruptMessageHandler::supports());
  }

  /**
   * Tests that handle projects linked id and stamp onto creation data.
   */
  public function testHandleProjectsLinkedIdAndStampOntoCreationData(): void {
    $entity = $this->prophesize(FlowDropInterruptInterface::class)->reveal();
    $factory = $this->prophesize(InterruptFactoryInterface::class);
    $factory->create(Argument::that(function (InterruptCreationData $data) {
      return $data->type === InterruptType::WorkflowExecution
        && $data->workflowId === 'wf'
        && $data->nodeId === 'node'
        && $data->message === 'waiting'
        && $data->linkedInterruptId === 'uuid-link'
        && $data->context === ['k' => 'v']
        && $data->direction === InterruptDirection::Outward
        && $data->pipelineId === 'pipe'
        && $data->jobId === 7
        && $data->schema === []
        && $data->defaultValue === NULL;
    }))->shouldBeCalled()->willReturn($entity);

    $handler = new LinkedInterruptMessageHandler($factory->reveal());
    $handler->handle(
      new LinkedInterruptMessage(
        type: InterruptType::WorkflowExecution,
        message: 'waiting',
        linkedInterruptId: 'uuid-link',
        context: ['k' => 'v'],
      ),
      new ExecutionStamp(workflowId: 'wf', nodeId: 'node', pipelineId: 'pipe', jobId: 7),
    );
  }

}
