<?php
/**
 * Small pending-state probes used by admin autonomy runner boot logic.
 *
 * @package Niwa_AI
 */

defined( 'ABSPATH' ) || exit;

class Niwa_AI_Admin_Autonomy_Pending_Runtime_State {
	/** @var object|null */
	private $control_plane;

	/** @var object|null */
	private $chat_agent;

	/** @var object|null */
	private $telegram;

	/** @var string */
	private $album_index_option;

	/** @var string */
	private $text_fragment_index_option;

	/** @var string */
	private $whatsapp_text_batch_index_option;

	/** @var array<string,callable> */
	private $callbacks;

	public function __construct( $control_plane, $chat_agent, $telegram, $album_index_option, $text_fragment_index_option, $whatsapp_text_batch_index_option = '', $callbacks = array() ) {
		$this->control_plane              = $control_plane;
		$this->chat_agent                 = $chat_agent;
		$this->telegram                   = $telegram;
		$this->album_index_option         = (string) $album_index_option;
		$this->text_fragment_index_option = (string) $text_fragment_index_option;
		$this->whatsapp_text_batch_index_option = (string) $whatsapp_text_batch_index_option;
		$this->callbacks                  = is_array( $callbacks ) ? $callbacks : array();
	}

	public function set_dependencies( $control_plane, $chat_agent, $telegram ) {
		$this->control_plane = $control_plane;
		$this->chat_agent    = $chat_agent;
		$this->telegram      = $telegram;
	}

	public function has_pending_actor_messages() {
		return $this->control_plane
			&& method_exists( $this->control_plane, 'has_pending_actor_messages' )
			&& ! empty( $this->control_plane->has_pending_actor_messages() );
	}

	public function has_active_actor_messages() {
		$this->call( 'reap_dead_actor_runtime_state' );

		if ( ! $this->control_plane || ! method_exists( $this->control_plane, 'list_sessions_with_pending_actor_messages' ) ) {
			return false;
		}

		$now = time();
		foreach ( (array) $this->control_plane->list_sessions_with_pending_actor_messages( 10 ) as $record ) {
			if ( ! empty( $record['has_active'] ) && intval( isset( $record['active_lease_expires_at'] ) ? $record['active_lease_expires_at'] : 0 ) > $now ) {
				return true;
			}
		}

		return false;
	}

	public function has_pending_background_subagents() {
		return $this->chat_agent
			&& method_exists( $this->chat_agent, 'has_pending_background_subagents' )
			&& ! empty( $this->chat_agent->has_pending_background_subagents() );
	}

	public function is_telegram_webhook_active() {
		if ( ! $this->telegram || ! method_exists( $this->telegram, 'get_webhook_state' ) ) {
			return false;
		}

		$state = (array) $this->telegram->get_webhook_state();
		return ! empty( $state['active'] );
	}

	public function has_pending_transport_buffers() {
		$album_index = get_option( $this->album_index_option, array() );
		$text_index  = get_option( $this->text_fragment_index_option, array() );
		$wa_index    = '' !== $this->whatsapp_text_batch_index_option
			? get_option( $this->whatsapp_text_batch_index_option, array() )
			: array();

		return ! empty( $album_index ) || ! empty( $text_index ) || ! empty( $wa_index );
	}

	private function call( $name, $args = array(), $default = null ) {
		return Niwa_AI_Admin_Autonomy_Callbacks::call( $this->callbacks, $name, $args, $default );
	}
}
