<?php
/**
 * WP Cron Event Exists Tool.
 *
 * Allows the agent to check if a WordPress cron event is scheduled and
 * optionally filter by specific arguments.
 *
 * @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;

// Direct access prevention
if ( ! defined( 'ABSPATH' ) ) {
	return;
}

/**
 * WP_Cron_Event_Exists_Tool class.
 */
class WP_Cron_Event_Exists_Tool extends Tool {

	/**
	 * Get the tool ID.
	 *
	 * @return string The tool ID.
	 * @since     1.0.0
	 */
	public function get_id() {
		return 'cron_event_exists';
	}

	/**
	 * Get the tool name.
	 *
	 * @return string The tool name.
	 * @since     1.0.0
	 */
	public function get_name() {
		return __( 'Cron Event Exists', 'agenistrator' );
	}

	/**
	 * Get the description of the tool.
	 *
	 * @return string The description of the tool.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Checks if a WordPress cron event with the specified hook name is scheduled.';
	}

	/**
	 * Get the parameters for this tool.
	 *
	 * @return array Parameters schema in JSON Schema format.
	 * @since     1.0.0
	 */
	public function get_parameters() {
		return array(
			'type'       => 'object',
			'properties' => array(
				'hook' => array(
					'type'        => 'string',
					'description' => 'The hook name of the cron event to check.',
				),
			),
			'required'   => array( 'hook' ),
		);
	}

	/**
	 * Check if the current user has permission to execute this tool.
	 *
	 * @return bool Whether the user has permission.
	 * @since     1.0.0
	 */
	public function current_user_can_execute() {
		return current_user_can( 'manage_options' );
	}

	/**
	 * Check if this tool is read-only.
	 *
	 * @return bool True - this tool only checks data.
	 * @since     1.0.0
	 */
	public function is_read_only() {
		return true;
	}

	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param array $args The arguments for the tool.
	 * @return array The result of executing the tool.
	 * @since     1.0.0
	 */
	protected function execute( $args ) {
		$hook     = $args['hook'];
		$crons    = _get_cron_array();
		$count    = 0;
		$next_run = null;

		if ( ! empty( $crons ) ) {
			foreach ( $crons as $timestamp => $cron_hooks ) {
				if ( isset( $cron_hooks[ $hook ] ) ) {
					$count += count( $cron_hooks[ $hook ] );
					if ( null === $next_run || $timestamp < $next_run ) {
						$next_run = $timestamp;
					}
				}
			}
		}

		$exists = $count > 0;

		return $this->success_response(
			$exists ? 'Cron event exists' : 'Cron event not found',
			array(
				'exists'        => $exists,
				'hook'          => $hook,
				'count'         => $count,
				'next_run'      => $next_run,
				'next_run_date' => $next_run ? gmdate( 'Y-m-d H:i:s e', $next_run ) : null,
			)
		);
	}
}
