<?php
/**
 * Term Exists Tool.
 *
 * Check if a term exists in a taxonomy by name, slug, or ID using term_exists().
 * Returns term ID and taxonomy term ID if found.
 *
 * @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;
}

/**
 * Term_Exists_Tool class.
 */
class Term_Exists_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Check if a term exists in a specific taxonomy by name, slug, or ID.';
	}

	/**
	 * 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 taxonomy to check in (e.g., "category", "post_tag", "product_cat")',
				),
				'term'     => array(
					'type'        => 'string',
					'description' => 'The term name, slug, or ID to check for',
				),
			),
			'required'   => array( 'taxonomy', 'term' ),
		);
	}

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

		if ( ! isset( $args['term'] ) || empty( $args['term'] ) ) {
			return $this->error_response( 'Term name, slug, or ID is required' );
		}

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

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

		$term_input = $args['term'];

		// Check if it's a numeric ID
		if ( is_numeric( $term_input ) ) {
			$result = term_exists( (int) $term_input, $taxonomy_name );
		} else {
			$result = term_exists( sanitize_text_field( $term_input ), $taxonomy_name );
		}

		if ( null === $result || 0 === $result ) {
			return $this->success_response(
				'Term does not exist',
				array(
					'exists'       => false,
					'taxonomy'     => $taxonomy_name,
					'searched_for' => $term_input,
				)
			);
		}

		// Get term details
		$term_id = is_array( $result ) ? $result['term_id'] : $result;
		$term    = get_term( $term_id, $taxonomy_name );

		if ( ! $term || is_wp_error( $term ) ) {
			return $this->success_response(
				'Term does not exist',
				array(
					'exists'       => false,
					'taxonomy'     => $taxonomy_name,
					'searched_for' => $term_input,
				)
			);
		}

		return $this->success_response(
			'Term exists',
			array(
				'exists'   => true,
				'taxonomy' => $taxonomy_name,
				'term'     => array(
					'id'    => $term->term_id,
					'name'  => $term->name,
					'slug'  => $term->slug,
					'count' => $term->count,
				),
			)
		);
	}
}
