<?php
/**
 * Get Site Info Tool.
 *
 * Get WordPress, PHP, and server information including versions, URLs,
 * timezone, active theme/plugins, and environment details.
 *
 * @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;
}

/**
 * Get_Site_Info_Tool class.
 */
class Get_Site_Info_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Get WordPress site information including WordPress version, PHP version, server software, and key configuration values.';
	}

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

	/**
	 * 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' => new \stdClass(),
			'required'   => array(),
		);
	}

	/**
	 * Get the default enabled status for this tool.
	 *
	 * @return bool True if enabled by default, false otherwise.
	 * @since     1.0.0
	 */
	public function get_default_enabled_status() {
		return true;
	}

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

	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- required by abstract Tool::execute()
	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param array $args The tool arguments.
	 *
	 * @return array The tool execution result.
	 * @since     1.0.0
	 */
	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Required by abstract method signature.
	protected function execute( $args ) {
		global $wpdb;

		$info = array(
			'site'      => array(
				'name'         => get_bloginfo( 'name' ),
				'description'  => get_bloginfo( 'description' ),
				'url'          => home_url(),
				'admin_url'    => admin_url(),
				'language'     => get_bloginfo( 'language' ),
				'timezone'     => wp_timezone_string(),
				'is_multisite' => is_multisite(),
			),
			'wordpress' => array(
				'version'          => get_bloginfo( 'version' ),
				'db_version'       => $wpdb->db_version(),
				'abspath'          => ABSPATH,
				'content_dir'      => WP_CONTENT_DIR,
				'plugin_dir'       => WP_PLUGIN_DIR,
				'uploads_dir'      => wp_upload_dir()['basedir'],
				'debug_mode'       => defined( 'WP_DEBUG' ) && WP_DEBUG,
				'debug_log'        => defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG,
				'script_debug'     => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
				'memory_limit'     => WP_MEMORY_LIMIT,
				'max_memory_limit' => defined( 'WP_MAX_MEMORY_LIMIT' ) ? WP_MAX_MEMORY_LIMIT : WP_MEMORY_LIMIT,
			),
			'php'       => array(
				'version'             => phpversion(),
				'memory_limit'        => ini_get( 'memory_limit' ),
				'max_execution_time'  => ini_get( 'max_execution_time' ),
				'upload_max_filesize' => ini_get( 'upload_max_filesize' ),
				'post_max_size'       => ini_get( 'post_max_size' ),
				'max_input_vars'      => ini_get( 'max_input_vars' ),
				'sapi'                => php_sapi_name(),
				'extensions'          => get_loaded_extensions(),
			),
			'server'    => array(
				'software'     => isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : 'Unknown',
				'protocol'     => isset( $_SERVER['SERVER_PROTOCOL'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_PROTOCOL'] ) ) : 'Unknown',
				'os'           => PHP_OS,
				'architecture' => PHP_INT_SIZE * 8 . '-bit',
			),
			'database'  => array(
				'server'  => $wpdb->db_server_info(),
				'name'    => $wpdb->dbname,
				'prefix'  => $wpdb->prefix,
				'charset' => $wpdb->charset,
				'collate' => $wpdb->collate,
			),
		);

		return $this->success_response( 'Site info retrieved', $info );
	}
}
