<?php

declare(strict_types=1);

namespace SiteFort\Modules\Scanner\Queue\Handler;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Task execution result passed back to the durable task runner.
 */
class TaskResult {

	/**
	 * @var string
	 */
	public string $status;

	/**
	 * @var array<string,mixed>|null
	 */
	public ?array $result;

	/**
	 * @var array<int,array<string,mixed>>
	 */
	public array $enqueue;

	/**
	 * @var string|null
	 */
	public ?string $error;

	/**
	 * @param string                     $status
	 * @param array<string,mixed>|null   $result
	 * @param array<int,array<string,mixed>> $enqueue
	 * @param string|null                $error
	 */
	public function __construct( string $status, ?array $result = null, array $enqueue = [], ?string $error = null ) {
		$this->status = $status;
		$this->result = $result;
		$this->enqueue = $enqueue;
		$this->error = $error;
	}

	/**
	 * Successful task completion.
	 *
	 * @param array<string,mixed>|null   $result
	 * @param array<int,array<string,mixed>> $enqueue
	 * @return self
	 */
	public static function completed( ?array $result = null, array $enqueue = [] ): self {
		return new self( 'completed', $result, $enqueue, null );
	}

	/**
	 * Failed task execution.
	 *
	 * @param string                   $error
	 * @param array<string,mixed>|null $result
	 * @return self
	 */
	public static function failed( string $error, ?array $result = null ): self {
		return new self( 'failed', $result, [], $error );
	}

	public static function build_scan_poll_dedupe_key(): string {
		return 'poll:scan';
	}
}
