<?php
/**
 * Get Tag Details Tool.
 *
 * Retrieve detailed information about a specific tag including name, slug,
 * description, and post count.
 *
 * @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://maximum.software/
 *
 * 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' ) ) {
	return;
}

/**
 * Get_Tag_Details_Tool class.
 */
class Get_Tag_Details_Tool extends Tool {

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

	/**
	 * Get the tool name.
	 *
	 * @return string The tool name.
	 * @since     1.0.0
	 */
	public function get_name() {
		return __( 'Get Tag 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 tag 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 tag to retrieve',
				),
				'slug' => array(
					'type'        => 'string',
					'description' => 'The slug of the tag 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 ) {
		$tag = null;

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

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

		$tag_data = array(
			'id'          => $tag->term_id,
			'name'        => $tag->name,
			'slug'        => $tag->slug,
			'description' => $tag->description,
			'count'       => $tag->count,
			'url'         => get_term_link( $tag ),
		);

		// Get recent posts with this tag
		$recent_posts = get_posts(
			array(
				'tag_id'         => $tag->term_id,
				'posts_per_page' => 5,
				'post_status'    => 'publish',
			)
		);

		if ( ! empty( $recent_posts ) ) {
			$tag_data['recent_posts'] = array();
			foreach ( $recent_posts as $post ) {
				$tag_data['recent_posts'][] = array(
					'id'    => $post->ID,
					'title' => $post->post_title,
					'date'  => $post->post_date,
				);
			}
		}

		return $this->success_response(
			'Tag retrieved successfully',
			array(
				'tag' => $tag_data,
			)
		);
	}
}
