<?php
/**
 * Message Event Class.
 *
 * Represents a chat message event using Message instances.
 *
 * @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://maximum.software/
 *
 * 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;
use Agenistrator\Message;

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

/**
 * Message_Event class.
 */
class Message_Event extends Thread_Event {

	/**
	 * Message instance
	 *
	 * @var Message
	 */
	private $message;

	/**
	 * Whether the message is complete
	 *
	 * @var bool
	 */
	private $is_complete;

	/**
	 * Constructor.
	 *
	 * @param int     $user_id     The user ID.
	 * @param int     $thread_id   The thread ID.
	 * @param Message $message     The message instance.
	 * @param bool    $is_complete Whether the message is complete.
	 * @since     1.0.0
	 */
	public function __construct( $user_id, $thread_id, Message $message, $is_complete = true ) {
		parent::__construct( $user_id, $thread_id );
		$this->message     = $message;
		$this->is_complete = $is_complete;
	}

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

	/**
	 * Get the message instance.
	 *
	 * @return Message
	 * @since     1.0.0
	 */
	public function get_message() {
		return $this->message;
	}

	/**
	 * Get the event data.
	 *
	 * @return array
	 * @since     1.0.0
	 */
	public function get_data() {
		return array(
			'role'        => $this->message->get_role(),
			'content'     => $this->message->get_content(),
			'metadata'    => $this->message->get_metadata(),
			'message_id'  => $this->message->get_message_id(),
			'is_complete' => $this->is_complete,
		);
	}

	/**
	 * Get the SSE payload for this event.
	 *
	 * @return array
	 * @since     1.0.0
	 */
	public function to_sse_payload() {
		return array_merge(
			parent::to_sse_payload(),
			array(
				'message_id'  => $this->message->get_message_id(),
				'role'        => $this->message->get_role(),
				'content'     => $this->message->get_content(),
				'metadata'    => $this->message->get_metadata(),
				'is_complete' => $this->is_complete,
			)
		);
	}

	/**
	 * 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;

		// Create a Message instance from the event data
		$message_data = array(
			'role'       => $event_data['role'],
			'content'    => $event_data['content'],
			'metadata'   => $event_data['metadata'] ?? null,
			'message_id' => $event_data['message_id'],
		);
		$message      = Message::from_array( $message_data );

		if ( ! $message ) {
			throw new \InvalidArgumentException( 'Invalid message data in event' );
		}

		$event = new static(
			$data['user_id'],
			$data['thread_id'],
			$message,
			$event_data['is_complete']
		);

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

		return $event;
	}
}
