<?php
/**
 * Install Theme Tool.
 *
 * Install themes from WordPress.org using Theme_Upgrader with optional
 * activation after installation.
 *
 * @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;
}

/**
 * Install_Theme_Tool class.
 */
class Install_Theme_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Install a theme from WordPress.org by slug.';
	}

	/**
	 * 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(
				'slug'     => array(
					'type'        => 'string',
					'description' => 'The theme slug from WordPress.org (e.g., "twentytwentyfour")',
				),
				'activate' => array(
					'type'        => 'boolean',
					'description' => 'Whether to activate the theme after installation',
					'default'     => false,
				),
			),
			'required'   => array( 'slug' ),
		);
	}

	/**
	 * 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 false;
	}

	/**
	 * 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 ) {
		// Check if file modifications are disabled
		if ( ! wp_is_file_mod_allowed( 'theme_install' ) ) {
			return $this->error_response( 'File modifications are disabled' );
		}

		if ( ! isset( $args['slug'] ) || empty( $args['slug'] ) ) {
			return $this->error_response( 'Theme slug is required' );
		}

		$slug = sanitize_text_field( $args['slug'] );

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

		// Check if already installed
		$theme = wp_get_theme( $slug );
		if ( $theme->exists() ) {
			return $this->error_response( 'Theme is already installed' );
		}

		// Get theme info
		$api = themes_api( 'theme_information', array( 'slug' => $slug ) );

		if ( is_wp_error( $api ) ) {
			return $this->error_response( 'Theme not found: ' . $api->get_error_message() );
		}

		// Install the theme
		$upgrader = new \Theme_Upgrader( new \WP_Ajax_Upgrader_Skin() );
		$result   = $upgrader->install( $api->download_link );

		if ( is_wp_error( $result ) ) {
			return $this->error_response( 'Installation failed: ' . $result->get_error_message() );
		}

		if ( false === $result ) {
			return $this->error_response( 'Installation failed' );
		}

		$response = array(
			'slug'      => $slug,
			'name'      => $api->name,
			'version'   => $api->version,
			'activated' => false,
		);

		// Activate if requested
		if ( isset( $args['activate'] ) && true === $args['activate'] ) {
			switch_theme( $slug );
			$response['activated'] = true;
		}

		return $this->success_response( 'Theme installed successfully', $response );
	}
}
