<?php
/**
 * WP Cron Schedule List Tool.
 *
 * Allows the agent to list all available cron recurrence schedules including
 * built-in and custom schedules registered via cron_schedules filter.
 *
 * @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;
}

/**
 * WP_Cron_Schedule_List_Tool class.
 */
class WP_Cron_Schedule_List_Tool extends Tool {

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

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

	/**
	 * Get the description of the tool.
	 *
	 * @return string The description of the tool.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Lists all available WordPress cron recurrence schedules (hourly, twicedaily, daily, weekly, etc.).';
	}

	/**
	 * 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(
				'limit'  => array(
					'type'        => 'integer',
					'description' => 'Maximum number of schedules to return. Default: 100.',
				),
				'offset' => array(
					'type'        => 'integer',
					'description' => 'Number of schedules to skip before returning results. Default: 0.',
				),
			),
			'required'   => array(),
		);
	}

	/**
	 * 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 ) {
		$schedules = wp_get_schedules();
		$limit     = max( 1, isset( $args['limit'] ) ? (int) $args['limit'] : 100 );
		$offset    = max( 0, isset( $args['offset'] ) ? (int) $args['offset'] : 0 );

		if ( empty( $schedules ) ) {
			return $this->success_response(
				'No cron schedules found',
				array(
					'schedules' => array(),
					'count'     => 0,
					'total'     => 0,
					'limit'     => $limit,
					'offset'    => $offset,
					'has_more'  => false,
				)
			);
		}

		$schedule_list = array();
		foreach ( $schedules as $name => $schedule ) {
			$schedule_list[] = array(
				'name'     => $name,
				'interval' => $schedule['interval'],
				'display'  => $schedule['display'],
			);
		}

		// Sort by interval.
		usort(
			$schedule_list,
			function ( $a, $b ) {
				return $a['interval'] - $b['interval'];
			}
		);

		// Apply pagination
		$total     = count( $schedule_list );
		$paginated = array_slice( $schedule_list, $offset, $limit );
		$has_more  = $total > ( $offset + $limit );

		return $this->success_response(
			'Cron schedules retrieved successfully',
			array(
				'schedules' => $paginated,
				'count'     => count( $paginated ),
				'total'     => $total,
				'limit'     => $limit,
				'offset'    => $offset,
				'has_more'  => $has_more,
			)
		);
	}
}
