<?php
/**
 * WP Cron Event Details Tool.
 *
 * Allows the agent to get detailed information about a specific cron event
 * including schedule, arguments, next run time, and callback details.
 *
 * @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_Details_Tool class.
 */
class WP_Cron_Event_Details_Tool extends Tool {

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

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

	/**
	 * Get the description of the tool.
	 *
	 * @return string The description of the tool.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Gets detailed information about a specific WordPress cron event by hook name.';
	}

	/**
	 * 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 get details for.',
				),
			),
			'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 retrieves 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();

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

		$instances = array();
		$schedules = wp_get_schedules();

		foreach ( $crons as $timestamp => $cron_hooks ) {
			if ( ! isset( $cron_hooks[ $hook ] ) ) {
				continue;
			}

			foreach ( $cron_hooks[ $hook ] as $sig => $event_data ) {
				$schedule_name    = $event_data['schedule'] ?? false;
				$schedule_display = $schedule_name;
				if ( $schedule_name && isset( $schedules[ $schedule_name ] ) ) {
					$schedule_display = $schedules[ $schedule_name ]['display'];
				}

				$instances[] = array(
					'next_run'         => $timestamp,
					'next_run_date'    => gmdate( 'Y-m-d H:i:s e', $timestamp ),
					'schedule'         => $schedule_name ? $schedule_name : 'one-time',
					'schedule_display' => $schedule_display ? $schedule_display : 'One-time',
					'interval'         => $event_data['interval'] ?? null,
					'args'             => $event_data['args'] ?? array(),
					'signature'        => $sig,
					'is_due'           => $timestamp <= time(),
				);
			}
		}

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

		// Sort by next run time.
		usort(
			$instances,
			function ( $a, $b ) {
				return $a['next_run'] - $b['next_run'];
			}
		);

		return $this->success_response(
			'Cron event details retrieved successfully',
			array(
				'hook'      => $hook,
				'instances' => $instances,
				'count'     => count( $instances ),
			)
		);
	}
}
