<?php
/**
 * Get Post Type Details Tool.
 *
 * Allows the agent to retrieve detailed information about a specific post type
 * including labels, capabilities, supports, and taxonomies.
 *
 * @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_Post_Type_Details_Tool class.
 */
class Get_Post_Type_Details_Tool extends Tool {

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

	/**
	 * Get the tool name.
	 *
	 * @return string The tool name.
	 * @since     1.0.0
	 */
	public function get_name() {
		return __( 'Get Post Type 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 post type by name.';
	}

	/**
	 * 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(
				'post_type' => array(
					'type'        => 'string',
					'description' => 'The name/slug of the post type (e.g., "post", "page", "product")',
				),
			),
			'required'   => array( 'post_type' ),
		);
	}

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

		$post_type_name = sanitize_key( $args['post_type'] );
		$post_type      = get_post_type_object( $post_type_name );

		if ( ! $post_type ) {
			return $this->error_response( 'Post type not found: ' . $post_type_name );
		}

		$count = wp_count_posts( $post_type_name );

		$post_type_data = array(
			'name'               => $post_type->name,
			'label'              => $post_type->label,
			'labels'             => array(
				'singular_name' => $post_type->labels->singular_name,
				'menu_name'     => $post_type->labels->menu_name,
				'all_items'     => $post_type->labels->all_items,
				'add_new'       => $post_type->labels->add_new,
				'add_new_item'  => $post_type->labels->add_new_item,
				'edit_item'     => $post_type->labels->edit_item,
				'new_item'      => $post_type->labels->new_item,
				'view_item'     => $post_type->labels->view_item,
				'search_items'  => $post_type->labels->search_items,
				'not_found'     => $post_type->labels->not_found,
			),
			'description'        => $post_type->description,
			'public'             => $post_type->public,
			'publicly_queryable' => $post_type->publicly_queryable,
			'hierarchical'       => $post_type->hierarchical,
			'show_ui'            => $post_type->show_ui,
			'show_in_menu'       => $post_type->show_in_menu,
			'show_in_nav_menus'  => $post_type->show_in_nav_menus,
			'show_in_admin_bar'  => $post_type->show_in_admin_bar,
			'show_in_rest'       => $post_type->show_in_rest,
			'rest_base'          => $post_type->rest_base,
			'rest_namespace'     => $post_type->rest_namespace ?? 'wp/v2',
			'menu_position'      => $post_type->menu_position,
			'menu_icon'          => $post_type->menu_icon,
			'supports'           => get_all_post_type_supports( $post_type_name ),
			'taxonomies'         => get_object_taxonomies( $post_type_name ),
			'has_archive'        => $post_type->has_archive,
			'builtin'            => $post_type->_builtin,
			'capability_type'    => $post_type->capability_type,
			'capabilities'       => (array) $post_type->cap,
			'rewrite'            => $post_type->rewrite,
			'counts'             => array(
				'publish' => isset( $count->publish ) ? (int) $count->publish : 0,
				'draft'   => isset( $count->draft ) ? (int) $count->draft : 0,
				'pending' => isset( $count->pending ) ? (int) $count->pending : 0,
				'private' => isset( $count->private ) ? (int) $count->private : 0,
				'trash'   => isset( $count->trash ) ? (int) $count->trash : 0,
			),
		);

		// Get taxonomy details for associated taxonomies
		$taxonomies = get_object_taxonomies( $post_type_name, 'objects' );
		if ( ! empty( $taxonomies ) ) {
			$post_type_data['taxonomy_details'] = array();
			foreach ( $taxonomies as $taxonomy ) {
				$post_type_data['taxonomy_details'][] = array(
					'name'         => $taxonomy->name,
					'label'        => $taxonomy->label,
					'hierarchical' => $taxonomy->hierarchical,
				);
			}
		}

		// Get recent posts of this type
		$recent_posts = get_posts(
			array(
				'post_type'      => $post_type_name,
				'posts_per_page' => 5,
				'post_status'    => 'publish',
			)
		);

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

		return $this->success_response(
			'Post type retrieved successfully',
			array(
				'post_type' => $post_type_data,
			)
		);
	}
}
