<?php
/**
 * Delete Page Tool.
 *
 * Allows the agent to delete WordPress pages by ID or slug. Includes
 * validation, permission checks, and trash/permanent deletion options.
 *
 * @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_Page_Tool class.
 */
class Delete_Page_Tool extends Tool {

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

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

	/**
	 * Get the description of the tool.
	 *
	 * @return     string The description of the tool.
	 * @since     1.0.0
	 */
	public function get_description() {
		return 'Delete a WordPress page.';
	}

	/**
	 * Get the parameters 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 page to delete',
				),
				'force_delete' => array(
					'type'        => 'boolean',
					'description' => 'Whether to permanently delete the page instead of moving to trash',
					'default'     => false,
				),
			),
			'required'   => array( 'id' ),
		);
	}

	/**
	 * Check if this tool is read-only.
	 *
	 * @return     bool False - this tool deletes content.
	 * @since     1.0.0
	 */
	public function is_read_only() {
		return false;
	}

	/**
	 * Check if the current user has permission to execute this tool.
	 *
	 * @return     bool Whether the user has permission.
	 * @since     1.0.0
	 */
	public function current_user_can_execute() {
		return current_user_can( 'delete_pages' );
	}

	/**
	 * Execute the tool with the given arguments.
	 *
	 * @param      array $args The arguments for the tool.
	 * @return     array The result of executing the tool.
	 * @since     1.0.0
	 */
	protected function execute( $args ) {
		if ( ! isset( $args['id'] ) || empty( $args['id'] ) ) {
			return $this->error_response( 'Page ID is required' );
		}

		$page_id = intval( $args['id'] );
		$page    = get_post( $page_id );

		if ( ! $page ) {
			return $this->error_response( 'Page not found' );
		}

		// Check if post type is 'page'
		if ( 'page' !== $page->post_type ) {
			return $this->error_response( 'The specified ID is not a page' );
		}

		// Check if user can delete this page
		if ( ! current_user_can( 'delete_page', $page_id ) ) {
			return $this->error_response( 'You do not have permission to delete this page' );
		}

		// Check if the page has child pages
		$children = get_pages(
			array(
				'child_of'    => $page_id,
				'post_status' => array( 'publish', 'draft', 'pending', 'private' ),
			)
		);

		if ( ! empty( $children ) ) {
			return $this->error_response( 'This page has child pages. Please delete or reassign the child pages first.' );
		}

		// Determine if we should force delete or move to trash
		$force_delete = isset( $args['force_delete'] ) && true === $args['force_delete'];

		// Store page info for response before deletion
		$page_title = $page->post_title;

		// Delete the page
		$deleted = wp_delete_post( $page_id, $force_delete );

		if ( ! $deleted ) {
			return $this->error_response( 'Failed to delete page' );
		}

		if ( $force_delete ) {
			$message = 'Page permanently deleted';
		} else {
			$message = 'Page moved to trash';
		}

		return $this->success_response(
			$message,
			array(
				'page_id'       => $page_id,
				'page_title'    => $page_title,
				'force_deleted' => $force_delete,
			)
		);
	}
}
