<?php
/**
 * Action Scheduler Details Tool.
 *
 * Get detailed information about a specific scheduled action including hook,
 * arguments, schedule, status, and execution timestamps.
 *
 * @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://agenistrator.com/
 *
 * 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' ) ) {
	exit;
}

/**
 * Action_Scheduler_Details_Tool class.
 */
class Action_Scheduler_Details_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Gets detailed information about a specific scheduled action by its ID.';
	}

	/**
	 * Get the parameters schema 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(
				'action_id' => array(
					'type'        => 'integer',
					'description' => 'The ID of the scheduled action to retrieve.',
				),
			),
			'required'   => array( 'action_id' ),
		);
	}

	/**
	 * Check if the current user can execute this tool.
	 *
	 * @return bool True if the current user has permission.
	 * @since     1.0.0
	 */
	public function current_user_can_execute() {
		return is_super_admin();
	}

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

	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param array $args The tool arguments.
	 *
	 * @return array The tool execution result.
	 * @since     1.0.0
	 */
	protected function execute( $args ) {
		if ( ! WordPress_Utils::is_action_scheduler_available() ) {
			return $this->error_response( 'Action Scheduler is not available or not initialized.' );
		}

		$action_id = (int) $args['action_id'];
		$store     = \ActionScheduler::store();
		$action    = $store->fetch_action( $action_id );

		if ( ! $action || ! $action->get_hook() ) {
			return $this->error_response( "Scheduled action with ID {$action_id} not found." );
		}

		$schedule     = $action->get_schedule();
		$next_run     = null;
		$recurrence   = null;
		$cron_pattern = null;

		if ( $schedule ) {
			if ( method_exists( $schedule, 'get_date' ) ) {
				$date     = $schedule->get_date();
				$next_run = $date ? (int) $date->format( 'U' ) : null;
			}
			if ( method_exists( $schedule, 'get_recurrence' ) ) {
				$recurrence = $schedule->get_recurrence();
			}
			if ( method_exists( $schedule, 'get_schedule' ) ) {
				$cron_pattern = $schedule->get_schedule();
			}
		}

		$status = $store->get_status( $action_id );

		$result = array(
			'id'            => $action_id,
			'hook'          => $action->get_hook(),
			'group'         => $action->get_group(),
			'status'        => $status,
			'args'          => $action->get_args(),
			'priority'      => method_exists( $action, 'get_priority' ) ? $action->get_priority() : 10,
			'next_run'      => $next_run,
			'next_run_date' => $next_run ? gmdate( 'Y-m-d H:i:s e', $next_run ) : null,
			'recurrence'    => $recurrence,
			'cron_pattern'  => $cron_pattern,
			'schedule_type' => $schedule ? get_class( $schedule ) : null,
		);

		return $this->success_response( 'Scheduled action details retrieved successfully', $result );
	}
}
