<?php
/**
 * Delete Category Tool.
 *
 * Delete WordPress categories by ID using wp_delete_category(). Posts
 * assigned to deleted categories are reassigned to the default 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;
}

/**
 * Delete_Category_Tool class.
 */
class Delete_Category_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Delete a 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 delete',
				),
			),
			'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' );
		}

		// Check if this is the default category
		$default_category = get_option( 'default_category' );
		if ( $category_id === (int) $default_category ) {
			return $this->error_response( 'Cannot delete the default category' );
		}

		$name  = $category->name;
		$count = $category->count;

		$deleted = wp_delete_term( $category_id, 'category' );

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

		if ( false === $deleted ) {
			return $this->error_response( 'Failed to delete category' );
		}

		return $this->success_response(
			'Category deleted successfully',
			array(
				'category_id'    => $category_id,
				'name'           => $name,
				'posts_affected' => $count,
			)
		);
	}
}
