<?php
/**
 * Plugin Deactivate — server-side execution.
 *
 * Sister to PluginActivate. Runs `deactivate_plugins()` in-process
 * under the App Password user's identity. Idempotent — already-inactive
 * plugins return success without re-firing deactivation hooks.
 *
 * @since 0.0.5
 * @package zip-ai
 */

namespace ZipAI\MCP\Classes\Abilities\Zipai\System;

use ZipAI\MCP\Classes\Abilities\Abstract_Ability;
use ZipAI\MCP\Classes\Abilities\Zipai\System\PluginResolver;
use ZipAI\MCP\Classes\Core\Response;
use ZipAI\MCP\Classes\Core\Tool_Types;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class PluginDeactivate extends Abstract_Ability {

	protected $is_destructive = true;

	public function configure() {
		$this->id          = 'zipai/deactivate-plugin';
		$this->label       = 'Deactivate Plugin';
		$this->description = 'Deactivate an active plugin by slug or "folder/file" identifier. '
			. 'Runs `deactivate_plugins()` in-process under the App-Password user\'s identity. '
			. 'Requires `activate_plugins` capability.';
		$this->capability  = 'activate_plugins';

		$this->meta = array(
			'tool_type'          => Tool_Types::WRITE,
			'preflight_resource' => array(
				'kind'      => 'installed_plugin',
				'arg_field' => 'slug',
			),
		);
	}

	public function get_tool_type() {
		return Tool_Types::WRITE;
	}

	public function get_input_schema() {
		return array(
			'type'                 => 'object',
			'required'             => array( 'slug' ),
			'additionalProperties' => false,
			'properties'           => array(
				'slug' => array(
					'type'        => 'string',
					'description' => 'Plugin slug ("contact-form-7") or WP REST plugin id "folder/file" (`.php` extension optional, stripped automatically).',
				),
			),
		);
	}

	public function execute( $args ) {
		$slug = isset( $args['slug'] ) ? sanitize_text_field( $args['slug'] ) : '';
		if ( '' === $slug ) {
			return Response::error( 'Plugin slug is required.' );
		}
		if ( ! preg_match( '#^[a-z0-9][a-z0-9-]*(?:/[a-z0-9._-]+(?:\.php)?)?$#i', $slug ) ) {
			return Response::error( 'Invalid plugin identifier.' );
		}

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

		$plugin_file = PluginResolver::resolve_plugin_file( $slug );
		if ( null === $plugin_file ) {
			return Response::error( sprintf( 'Plugin "%s" is not installed.', $slug ) );
		}

		if ( ! is_plugin_active( $plugin_file ) ) {
			return array(
				'success' => true,
				'message' => sprintf( 'Plugin "%s" is already inactive.', $slug ),
				'data'    => array(
					'slug'        => $slug,
					'plugin_file' => $plugin_file,
					'active'      => false,
				),
			);
		}

		// `deactivate_plugins()` returns void. WP core protects critical
		// plugins via the `pre_deactivate_plugin` action chain — we let
		// any plugin-side guard surface its own error path via WP_Error
		// in `pre_update_option_active_plugins` (filtered into our own
		// Protected_Options_Filter for the brain-essential set).
		deactivate_plugins( array( $plugin_file ), true );

		if ( is_plugin_active( $plugin_file ) ) {
			return Response::error( sprintf( 'Plugin "%s" did not deactivate (still active after deactivate_plugins call).', $slug ) );
		}

		return array(
			'success' => true,
			'message' => sprintf( 'Plugin "%s" deactivated.', $slug ),
			'data'    => array(
				'slug'        => $slug,
				'plugin_file' => $plugin_file,
				'active'      => false,
			),
		);
	}

	public function get_output_schema() {
		return array(
			'type'                 => 'object',
			'required'             => array( 'success' ),
			'additionalProperties' => true,
			'properties'           => array(
				'success' => array( 'type' => 'boolean' ),
				'message' => array( 'type' => 'string' ),
				'data'    => array(
					'type'       => 'object',
					'properties' => array(
						'slug'        => array( 'type' => 'string' ),
						'plugin_file' => array( 'type' => 'string' ),
						'active'      => array( 'type' => 'boolean' ),
					),
				),
			),
		);
	}
}
