<?php
/**
 * Thread Event Base Class.
 *
 * Base class for thread-specific events. These events are associated with both
 * a specific user and 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;

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

/**
 * Thread_Event class.
 */
abstract class Thread_Event extends User_Event {

	/**
	 * Thread ID
	 *
	 * @var int
	 * @since     1.0.0
	 */
	protected $thread_id;

	/**
	 * Constructor.
	 *
	 * @param int $user_id   The user ID.
	 * @param int $thread_id The thread ID.
	 * @since     1.0.0
	 */
	public function __construct( $user_id, $thread_id ) {
		parent::__construct( $user_id );
		$this->thread_id = (int) $thread_id;
	}

	/**
	 * Get the thread ID.
	 *
	 * @return int
	 * @since     1.0.0
	 */
	public function get_thread_id() {
		return $this->thread_id;
	}

	/**
	 * Convert the event to array.
	 *
	 * @return array
	 * @since     1.0.0
	 */
	public function to_array() {
		return array_merge(
			parent::to_array(),
			array( 'thread_id' => $this->get_thread_id() )
		);
	}

	/**
	 * Get the SSE payload for this event.
	 *
	 * Adds thread_id to the payload so the frontend can filter events by thread.
	 *
	 * @return array
	 * @since     1.0.0
	 */
	public function to_sse_payload() {
		return array_merge(
			parent::to_sse_payload(),
			array( 'thread_id' => $this->get_thread_id() )
		);
	}

	/**
	 * 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 = new static( $data['user_id'], $data['thread_id'] );
		if ( isset( $data['id'] ) ) {
			$event->set_id( $data['id'] );
		}
		return $event;
	}
}
