<?php
/**
 * Get Category Details Tool.
 *
 * Retrieve detailed information about a specific category including name,
 * slug, description, parent, post count, and hierarchy.
 *
 * @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_Category_Details_Tool class.
 */
class Get_Category_Details_Tool extends Tool {

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

	/**
	 * Get the tool name.
	 *
	 * @return string The tool name.
	 * @since     1.0.0
	 */
	public function get_name() {
		return __( 'Get Category 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 category by ID or 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(
				'id'   => array(
					'type'        => 'integer',
					'description' => 'The ID of the category to retrieve',
				),
				'slug' => array(
					'type'        => 'string',
					'description' => 'The slug of the category to retrieve',
				),
			),
			'required'   => array(),
		);
	}

	/**
	 * 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 ) {
		$category = null;

		if ( isset( $args['id'] ) && ! empty( $args['id'] ) ) {
			$category = get_term( intval( $args['id'] ), 'category' );
		} elseif ( isset( $args['slug'] ) && ! empty( $args['slug'] ) ) {
			$category = get_term_by( 'slug', sanitize_text_field( $args['slug'] ), 'category' );
		} else {
			return $this->error_response( 'Either category ID or slug is required' );
		}

		if ( ! $category || is_wp_error( $category ) ) {
			return $this->error_response( 'Category not found' );
		}

		$category_data = array(
			'id'          => $category->term_id,
			'name'        => $category->name,
			'slug'        => $category->slug,
			'description' => $category->description,
			'parent'      => $category->parent,
			'count'       => $category->count,
			'url'         => get_term_link( $category ),
		);

		// Get parent info if exists
		if ( $category->parent > 0 ) {
			$parent = get_term( $category->parent, 'category' );
			if ( $parent && ! is_wp_error( $parent ) ) {
				$category_data['parent_info'] = array(
					'id'   => $parent->term_id,
					'name' => $parent->name,
					'slug' => $parent->slug,
				);
			}
		}

		// Get children
		$children = get_terms(
			array(
				'taxonomy'   => 'category',
				'parent'     => $category->term_id,
				'hide_empty' => false,
			)
		);

		if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
			$category_data['children'] = array();
			foreach ( $children as $child ) {
				$category_data['children'][] = array(
					'id'   => $child->term_id,
					'name' => $child->name,
					'slug' => $child->slug,
				);
			}
		}

		return $this->success_response(
			'Category retrieved successfully',
			array(
				'category' => $category_data,
			)
		);
	}
}
