<?php
/**
 * Create Tag Tool.
 *
 * Create new post tags with name, slug, and description using wp_insert_term().
 *
 * @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;
}

/**
 * Create_Tag_Tool class.
 */
class Create_Tag_Tool extends Tool {

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

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

	/**
	 * Get the tool description.
	 *
	 * @return string The tool description.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Create a new tag.';
	}

	/**
	 * 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(
				'name'        => array(
					'type'        => 'string',
					'description' => 'The name of the tag',
				),
				'slug'        => array(
					'type'        => 'string',
					'description' => 'The slug for the tag (auto-generated if not provided)',
				),
				'description' => array(
					'type'        => 'string',
					'description' => 'The description of the tag in plain text format',
				),
			),
			'required'   => array( 'name' ),
		);
	}

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

		$term_args = array();

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

		$result = wp_insert_term(
			sanitize_text_field( $args['name'] ),
			'post_tag',
			$term_args
		);

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

		$tag = get_term( $result['term_id'], 'post_tag' );

		return $this->success_response(
			'Tag created successfully',
			array(
				'tag_id' => $result['term_id'],
				'name'   => $tag->name,
				'slug'   => $tag->slug,
				'url'    => get_term_link( $tag ),
			)
		);
	}
}
