<?php
/**
 * Update Category Tool.
 *
 * Update existing WordPress categories using wp_update_category() with support
 * for modifying name, slug, description, and parent category.
 *
 * @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;
}

/**
 * Update_Category_Tool class.
 */
class Update_Category_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Update an existing category.';
	}

	/**
	 * 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 update',
				),
				'name'        => array(
					'type'        => 'string',
					'description' => 'New name for the category',
				),
				'slug'        => array(
					'type'        => 'string',
					'description' => 'New slug for the category',
				),
				'description' => array(
					'type'        => 'string',
					'description' => 'New description for the category in plain text format',
				),
				'parent'      => array(
					'type'        => 'integer',
					'description' => 'New parent category ID (0 to make top-level)',
				),
			),
			'required'   => array( 'id' ),
		);
	}

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

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

		$category_id = intval( $args['id'] );
		$category    = get_term( $category_id, 'category' );

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

		$term_args = array();

		if ( isset( $args['name'] ) && ! empty( $args['name'] ) ) {
			$term_args['name'] = sanitize_text_field( $args['name'] );
		}

		if ( isset( $args['slug'] ) && ! empty( $args['slug'] ) ) {
			$term_args['slug'] = sanitize_title( $args['slug'] );
		}

		if ( isset( $args['description'] ) ) {
			$term_args['description'] = sanitize_textarea_field( $args['description'] );
		}

		if ( isset( $args['parent'] ) ) {
			$parent_id = intval( $args['parent'] );
			if ( $parent_id > 0 ) {
				$parent = get_term( $parent_id, 'category' );
				if ( ! $parent || is_wp_error( $parent ) ) {
					return $this->error_response( 'Parent category not found' );
				}
				if ( $parent_id === $category_id ) {
					return $this->error_response( 'Category cannot be its own parent' );
				}
			}
			$term_args['parent'] = $parent_id;
		}

		if ( empty( $term_args ) ) {
			return $this->error_response( 'No updates provided' );
		}

		$result = wp_update_term( $category_id, 'category', $term_args );

		if ( is_wp_error( $result ) ) {
			return $this->error_response( 'Failed to update category: ' . $result->get_error_message() );
		}

		$updated = get_term( $category_id, 'category' );

		return $this->success_response(
			'Category updated successfully',
			array(
				'category_id' => $category_id,
				'name'        => $updated->name,
				'slug'        => $updated->slug,
				'url'         => get_term_link( $updated ),
			)
		);
	}
}
