<?php
/**
 * Set Featured Image ability.
 *
 * @package rtCamp\Publishio\Modules\MCP\Abilities\Posts
 */

declare( strict_types = 1 );

namespace rtCamp\Publishio\Modules\MCP\Abilities\Posts;

/**
 * Class - Set_Featured_Image
 */
class Set_Featured_Image {
	/**
	 * Register the ability.
	 */
	public function register(): void {
		wp_register_ability(
			'publishio/set-featured-image',
			[
				'label'               => __( 'Set Featured Image for Post or Page', 'publishio' ),
				'category'            => \rtCamp\Publishio\Modules\MCP\Abilities\Categories\Posts::SLUG,
				'description'         => __( 'Sets or removes the featured image (post thumbnail) for a post or page. Pass an attachment ID to set, or 0 to remove.', 'publishio' ),
				'input_schema'        => [
					'type'                 => 'object',
					'required'             => [ 'post_id', 'attachment_id' ],
					'properties'           => [
						'post_id'       => [
							'type'        => 'integer',
							'minimum'     => 1,
							'description' => 'ID of the post or page.',
						],
						'attachment_id' => [
							'type'        => 'integer',
							'minimum'     => 0,
							'description' => 'Attachment ID to set as featured image. Use 0 to remove.',
						],
					],
					'additionalProperties' => false,
				],
				'output_schema'       => [
					'type'       => 'object',
					'required'   => [ 'success', 'url' ],
					'properties' => [
						'success' => [ 'type' => 'boolean' ],
						'url'     => [
							'type'        => 'string',
							'description' => 'URL of the featured image (empty if removed).',
						],
					],
				],
				'permission_callback' => static function () {
					return current_user_can( 'edit_posts' );
				},
				'execute_callback'    => static function ( array $input ) {
					$post_id       = (int) ( $input['post_id'] ?? 0 );
					$attachment_id = (int) ( $input['attachment_id'] ?? -1 );

					if ( ! get_post( $post_id ) ) {
						return new \WP_Error( 'invalid_post', __( 'Post not found.', 'publishio' ) );
					}

					if ( ! current_user_can( 'edit_post', $post_id ) ) {
						return new \WP_Error( 'forbidden', __( 'You do not have permission to edit this post.', 'publishio' ) );
					}

					if ( 0 === $attachment_id ) {
						delete_post_thumbnail( $post_id );
						return [
							'success' => true,
							'url'     => '',
						];
					}

					if ( ! get_post( $attachment_id ) || get_post_type( $attachment_id ) !== 'attachment' ) {
						return new \WP_Error( 'invalid_attachment', __( 'Attachment not found.', 'publishio' ) );
					}

					if ( ! current_user_can( 'read_post', $attachment_id ) ) {
						return new \WP_Error( 'forbidden_attachment', __( 'You do not have permission to use this attachment.', 'publishio' ) );
					}

					$result = set_post_thumbnail( $post_id, $attachment_id );

					if ( ! $result ) {
						return new \WP_Error( 'failed', __( 'Could not set featured image.', 'publishio' ) );
					}

					return [
						'success' => true,
						'url'     => wp_get_attachment_url( $attachment_id ) ?: '',
					];
				},
				'meta'                => [
					'show_in_rest' => true,
					'annotations'  => [
						'readonly'    => false,
						'destructive' => false,
						'idempotent'  => true,
					],
					'mcp'          => [
						'public' => true,
					],
				],
			]
		);
	}
}
