<?php
/**
 * Thread Error Event Class.
 *
 * Represents an error message related to a specific thread.
 *
 * @package   Agenistrator
 * @since     1.0.0
 * @author    Alex Ruban <alex@maximum.guru>
 * @author    Maximum Software Inc <contact@maximum.software>
 * @license   GPL-3.0-only
 * @link      https://agenistrator.com/
 *
 * SPDX-FileCopyrightText: 2025-2026 Alex Ruban <alex@maximum.guru>
 * SPDX-FileCopyrightText: 2025-2026 Maximum Software Inc <contact@maximum.software>
 * SPDX-License-Identifier: GPL-3.0-only
 */

namespace Agenistrator\Events;

use Agenistrator\Thread_Event;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Thread_Error_Event class.
 */
class Thread_Error_Event extends Thread_Event {

	/**
	 * Error message
	 *
	 * @var string
	 */
	private $message;

	/**
	 * Error code
	 *
	 * @var int
	 */
	private $code;

	/**
	 * Error details
	 *
	 * @var array
	 */
	private $details;

	/**
	 * Message ID (optional, for message-related errors)
	 *
	 * @var string|null
	 */
	private $message_id;

	/**
	 * Constructor.
	 *
	 * @param int         $user_id   The user ID.
	 * @param int         $thread_id The thread ID.
	 * @param string      $message   The error message.
	 * @param int         $code      The error code.
	 * @param array       $details   Additional error details.
	 * @param string|null $message_id Optional message ID for message-related errors.
	 * @since     1.0.0
	 */
	public function __construct( $user_id, $thread_id, $message, $code = 0, $details = array(), $message_id = null ) {
		parent::__construct( $user_id, $thread_id );
		$this->message    = $message;
		$this->code       = $code;
		$this->details    = $details;
		$this->message_id = $message_id;
	}

	/**
	 * Get the event type.
	 *
	 * @return string
	 * @since     1.0.0
	 */
	public function get_type() {
		return 'error';
	}

	/**
	 * Get the message ID.
	 *
	 * @return string|null
	 * @since     1.0.0
	 */
	public function get_message_id() {
		return $this->message_id;
	}

	/**
	 * Get the event data.
	 *
	 * @return array
	 * @since     1.0.0
	 */
	public function get_data() {
		$data = array(
			'message' => $this->message,
			'code'    => $this->code,
			'details' => $this->details,
		);

		// Include message_id if it's set
		if ( null !== $this->message_id ) {
			$data['message_id'] = $this->message_id;
		}

		return $data;
	}

	/**
	 * Get the SSE payload for this event.
	 *
	 * Frontend needs: message, message_id (optional)
	 * Excluded: code, details (internal/debugging info)
	 *
	 * @return array
	 * @since     1.0.0
	 */
	public function to_sse_payload() {
		$payload = array_merge(
			parent::to_sse_payload(),
			array( 'message' => $this->message )
		);

		if ( null !== $this->message_id ) {
			$payload['message_id'] = $this->message_id;
		}

		return $payload;
	}

	/**
	 * Create an event from array data.
	 *
	 * @param array $data The event data.
	 * @return static
	 * @since     1.0.0
	 */
	public static function from_array( $data ) {
		$event_data = $data['data'] ?? $data;

		$event = new static(
			$data['user_id'],
			$data['thread_id'],
			$event_data['message'],
			$event_data['code'],
			$event_data['details'],
			$event_data['message_id'] ?? null
		);

		if ( isset( $data['id'] ) ) {
			$event->set_id( $data['id'] );
		}

		return $event;
	}
}
