<?php
/**
 * List Installed Themes Tool.
 *
 * List all installed themes with their details including active/inactive status,
 * version, author, and parent theme for child themes.
 *
 * @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;
}

/**
 * List_Installed_Themes_Tool class.
 */
class List_Installed_Themes_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'List all installed themes.';
	}

	/**
	 * Get usage instructions for the LLM.
	 *
	 * @return string Instructions for using this tool.
	 * @since     1.0.0
	 */
	public function get_instructions() {
		return <<<'INST'
TRUNCATION NOTE:
The `truncated_description` field is truncated for brevity. Use the `get_theme_details` tool with the theme stylesheet to retrieve the full description.
INST;
	}

	/**
	 * 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(
				'limit'  => array(
					'type'        => 'integer',
					'description' => 'Maximum number of themes to return',
					'default'     => 100,
				),
				'offset' => array(
					'type'        => 'integer',
					'description' => 'Number of themes to skip',
					'default'     => 0,
				),
			),
			'required'   => array(),
		);
	}

	/**
	 * 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( 'switch_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 ) {
		$all_themes   = wp_get_themes();
		$active_theme = get_stylesheet();

		$themes = array();
		foreach ( $all_themes as $stylesheet => $theme ) {
			$themes[] = array(
				'stylesheet'            => $stylesheet,
				'name'                  => $theme->get( 'Name' ),
				'version'               => $theme->get( 'Version' ),
				'author'                => $theme->get( 'Author' ),
				'truncated_description' => String_Utils::truncate_to_token_boundary_with_ellipsis( $theme->get( 'Description' ), 200 ),
				'is_active'             => ( $stylesheet === $active_theme ),
				'parent'                => $theme->parent() ? $theme->parent()->get_stylesheet() : null,
			);
		}

		$limit  = isset( $args['limit'] ) ? intval( $args['limit'] ) : 100;
		$offset = isset( $args['offset'] ) ? intval( $args['offset'] ) : 0;
		$total  = count( $themes );
		$themes = array_slice( $themes, $offset, $limit );

		return $this->success_response(
			'Themes retrieved successfully',
			array(
				'themes'   => $themes,
				'count'    => count( $themes ),
				'total'    => $total,
				'limit'    => $limit,
				'offset'   => $offset,
				'has_more' => ( $offset + count( $themes ) ) < $total,
			)
		);
	}
}
