<?php
/**
 * Get Taxonomy Details Tool.
 *
 * Retrieve detailed information about a registered taxonomy including labels,
 * capabilities, visibility settings, and associated post types.
 *
 * @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;
}

/**
 * Get_Taxonomy_Details_Tool class.
 */
class Get_Taxonomy_Details_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Get detailed information about a specific taxonomy by name.';
	}

	/**
	 * 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(
				'taxonomy' => array(
					'type'        => 'string',
					'description' => 'The name/slug of the taxonomy (e.g., "category", "post_tag", "product_cat")',
				),
			),
			'required'   => array( 'taxonomy' ),
		);
	}

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

	/**
	 * 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 ) {
		if ( ! isset( $args['taxonomy'] ) || empty( $args['taxonomy'] ) ) {
			return $this->error_response( 'Taxonomy name is required' );
		}

		$taxonomy_name = sanitize_key( $args['taxonomy'] );
		$taxonomy      = get_taxonomy( $taxonomy_name );

		if ( ! $taxonomy ) {
			return $this->error_response( 'Taxonomy not found: ' . $taxonomy_name );
		}

		$term_count = wp_count_terms(
			array(
				'taxonomy'   => $taxonomy_name,
				'hide_empty' => false,
			)
		);

		$taxonomy_data = array(
			'name'               => $taxonomy->name,
			'label'              => $taxonomy->label,
			'labels'             => array(
				'singular_name' => $taxonomy->labels->singular_name,
				'menu_name'     => $taxonomy->labels->menu_name,
				'all_items'     => $taxonomy->labels->all_items,
				'edit_item'     => $taxonomy->labels->edit_item,
				'view_item'     => $taxonomy->labels->view_item,
				'update_item'   => $taxonomy->labels->update_item,
				'add_new_item'  => $taxonomy->labels->add_new_item,
				'new_item_name' => $taxonomy->labels->new_item_name,
				'search_items'  => $taxonomy->labels->search_items,
				'not_found'     => $taxonomy->labels->not_found,
			),
			'description'        => $taxonomy->description,
			'public'             => $taxonomy->public,
			'publicly_queryable' => $taxonomy->publicly_queryable,
			'hierarchical'       => $taxonomy->hierarchical,
			'show_ui'            => $taxonomy->show_ui,
			'show_in_menu'       => $taxonomy->show_in_menu,
			'show_in_nav_menus'  => $taxonomy->show_in_nav_menus,
			'show_tagcloud'      => $taxonomy->show_tagcloud,
			'show_in_quick_edit' => $taxonomy->show_in_quick_edit,
			'show_admin_column'  => $taxonomy->show_admin_column,
			'show_in_rest'       => $taxonomy->show_in_rest,
			'rest_base'          => $taxonomy->rest_base,
			'rest_namespace'     => $taxonomy->rest_namespace ?? 'wp/v2',
			'object_types'       => $taxonomy->object_type,
			'term_count'         => is_wp_error( $term_count ) ? 0 : (int) $term_count,
			'builtin'            => $taxonomy->_builtin,
			'capabilities'       => array(
				'manage_terms' => $taxonomy->cap->manage_terms,
				'edit_terms'   => $taxonomy->cap->edit_terms,
				'delete_terms' => $taxonomy->cap->delete_terms,
				'assign_terms' => $taxonomy->cap->assign_terms,
			),
			'rewrite'            => $taxonomy->rewrite,
		);

		// Get top-level terms (sample)
		$top_terms = get_terms(
			array(
				'taxonomy'   => $taxonomy_name,
				'parent'     => 0,
				'number'     => 10,
				'hide_empty' => false,
			)
		);

		if ( ! is_wp_error( $top_terms ) && ! empty( $top_terms ) ) {
			$taxonomy_data['sample_terms'] = array();
			foreach ( $top_terms as $term ) {
				$taxonomy_data['sample_terms'][] = array(
					'id'    => $term->term_id,
					'name'  => $term->name,
					'slug'  => $term->slug,
					'count' => $term->count,
				);
			}
		}

		return $this->success_response(
			'Taxonomy retrieved successfully',
			array(
				'taxonomy' => $taxonomy_data,
			)
		);
	}
}
