<?php
/**
 * WP Cron Event Unschedule Tool.
 *
 * Allows the agent to unschedule WordPress cron events. Supports removing
 * single instances or all instances of a hook.
 *
 * @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_Unschedule_Tool class.
 */
class WP_Cron_Event_Unschedule_Tool extends Tool {

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

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

	/**
	 * Get the description of the tool.
	 *
	 * @return string The description of the tool.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Unschedules a WordPress cron event. Can remove all instances of a hook or a specific scheduled instance.';
	}

	/**
	 * 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 unschedule.',
				),
				'timestamp' => array(
					'type'        => 'integer',
					'description' => 'Specific timestamp to unschedule. If omitted, all instances of the hook are removed.',
				),
				'args'      => array(
					'type'        => 'array',
					'description' => 'Arguments that were passed when scheduling. Required if timestamp is specified.',
					'items'       => array( 'type' => 'string' ),
				),
			),
			'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 False - this tool modifies data.
	 * @since     1.0.0
	 */
	public function is_read_only() {
		return false;
	}

	/**
	 * 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'];
		$timestamp  = isset( $args['timestamp'] ) ? (int) $args['timestamp'] : null;
		$event_args = $args['args'] ?? array();

		// Check if the event exists.
		$crons = _get_cron_array();
		$found = false;

		if ( ! empty( $crons ) ) {
			foreach ( $crons as $cron_hooks ) {
				if ( isset( $cron_hooks[ $hook ] ) ) {
					$found = true;
					break;
				}
			}
		}

		if ( ! $found ) {
			return $this->error_response( "No cron event found with hook: {$hook}" );
		}

		// Unschedule the event(s).
		if ( $timestamp ) {
			// Unschedule specific instance.
			$result = wp_unschedule_event( $timestamp, $hook, $event_args );
			if ( false === $result ) {
				return $this->error_response( 'Failed to unschedule cron event. Check timestamp and args.' );
			}
			$message = "Unscheduled cron event '{$hook}' at timestamp {$timestamp}";
		} else {
			// Clear all instances of this hook.
			$count   = wp_clear_scheduled_hook( $hook, $event_args );
			$message = "Cleared {$count} instance(s) of cron event '{$hook}'";
		}

		return $this->success_response(
			$message,
			array(
				'hook'      => $hook,
				'timestamp' => $timestamp,
			)
		);
	}
}
