<?php
/**
 * User Event Base Class.
 *
 * Base class for user-specific events. These events are associated with a
 * specific user but not necessarily 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://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;

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

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

	/**
	 * User ID
	 *
	 * @var int
	 * @since     1.0.0
	 */
	protected $user_id;

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

	/**
	 * Get the user ID.
	 *
	 * @return int
	 * @since     1.0.0
	 */
	public function get_user_id() {
		return $this->user_id;
	}

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

	/**
	 * Get the SSE payload for this event.
	 *
	 * Adds user_id to the SSE payload.
	 *
	 * @return array
	 * @since     1.0.0
	 */
	public function to_sse_payload() {
		return array_merge(
			parent::to_sse_payload(),
			array( 'user_id' => $this->get_user_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'] );
		if ( isset( $data['id'] ) ) {
			$event->set_id( $data['id'] );
		}
		return $event;
	}
}
