<?php
/**
 * Search Themes Tool.
 *
 * Search the WordPress.org theme directory using themes_api() with filtering
 * by search term, tag, and author.
 *
 * @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;
}

/**
 * Search_Themes_Tool class.
 */
class Search_Themes_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Search the WordPress.org theme directory.';
	}

	/**
	 * 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(
				'search'   => array(
					'type'        => 'string',
					'description' => 'Search term',
				),
				'per_page' => array(
					'type'        => 'integer',
					'description' => 'Number of results per page',
					'default'     => 10,
				),
			),
			'required'   => array( 'search' ),
		);
	}

	/**
	 * 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 current_user_can( 'install_themes' );
	}

	/**
	 * 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 ( ! isset( $args['search'] ) || empty( $args['search'] ) ) {
			return $this->error_response( 'Search term is required' );
		}

		if ( ! function_exists( 'themes_api' ) ) {
			require_once ABSPATH . 'wp-admin/includes/theme.php';
		}

		$per_page = isset( $args['per_page'] ) ? intval( $args['per_page'] ) : 10;

		$api_result = themes_api(
			'query_themes',
			array(
				'search'   => sanitize_text_field( $args['search'] ),
				'per_page' => $per_page,
				'fields'   => array(
					'description'    => true,
					'screenshot_url' => false,
				),
			)
		);

		if ( is_wp_error( $api_result ) ) {
			return $this->error_response( 'Failed to search themes: ' . $api_result->get_error_message() );
		}

		$themes = array();
		foreach ( $api_result->themes as $t ) {
			$themes[] = array(
				'slug'        => $t->slug,
				'name'        => $t->name,
				'version'     => $t->version,
				'author'      => $t->author,
				'rating'      => $t->rating,
				'num_ratings' => $t->num_ratings,
				'description' => isset( $t->description ) ? wp_trim_words( $t->description, 30 ) : '',
			);
		}

		return $this->success_response(
			'Theme search completed',
			array(
				'themes' => $themes,
				'total'  => $api_result->info['results'],
			)
		);
	}
}
