<?php
/**
 * Uninstall Theme Tool.
 *
 * Delete installed themes using delete_theme(). Prevents deletion of
 * currently active theme or parent of active child theme.
 *
 * @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;
}

/**
 * Uninstall_Theme_Tool class.
 */
class Uninstall_Theme_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Uninstall a theme by removing its files.';
	}

	/**
	 * 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(
				'stylesheet' => array(
					'type'        => 'string',
					'description' => 'Theme stylesheet (directory name)',
				),
			),
			'required'   => array( 'stylesheet' ),
		);
	}

	/**
	 * 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( 'delete_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['stylesheet'] ) || empty( $args['stylesheet'] ) ) {
			return $this->error_response( 'Theme stylesheet is required' );
		}

		$stylesheet = sanitize_text_field( $args['stylesheet'] );
		$theme      = wp_get_theme( $stylesheet );

		if ( ! $theme->exists() ) {
			return $this->error_response( 'Theme not found' );
		}

		// Cannot delete active theme
		if ( get_stylesheet() === $stylesheet ) {
			return $this->error_response( 'Cannot uninstall the active theme' );
		}

		// Cannot delete parent of active theme
		$active_theme = wp_get_theme();
		if ( $active_theme->parent() && $active_theme->parent()->get_stylesheet() === $stylesheet ) {
			return $this->error_response( 'Cannot uninstall the parent theme of the active theme' );
		}

		$theme_name = $theme->get( 'Name' );

		require_once ABSPATH . 'wp-admin/includes/file.php';

		$deleted = delete_theme( $stylesheet );

		if ( is_wp_error( $deleted ) ) {
			return $this->error_response( 'Failed to uninstall theme: ' . $deleted->get_error_message() );
		}

		return $this->success_response(
			'Theme uninstalled successfully',
			array(
				'stylesheet' => $stylesheet,
				'name'       => $theme_name,
			)
		);
	}
}
