<?php
// @generated by build-free-packages.mjs — DO NOT EDIT.
/**
 * Update Post Action Definition
 *
 * @package Sequensy\Integrations\WordPress
 * @since   1.0.0
 */

declare(strict_types=1);

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

use Sequensy\Integrations\WordPress\Helpers\PostHelpers;

return array(
	'type'            => 'update_post',
	'label'           => __( 'Update Post', 'sequensy' ),
	'description'     => __( 'Update an existing post or page', 'sequensy' ),
	'category'        => 'post',
	'required_fields' => array( 'post_id' ),
	'schema'          => array(
		array(
			'name'        => 'post_id',
			'type'        => 'number',
			'label'       => __( 'Post ID', 'sequensy' ),
			'description' => __( 'ID of the post to update', 'sequensy' ),
			'required'    => true,
			'order'       => 1,
		),
		array(
			'name'        => 'post_title',
			'type'        => 'string',
			'label'       => __( 'Title', 'sequensy' ),
			'description' => __( 'Post title', 'sequensy' ),
			'order'       => 2,
		),
		array(
			'name'        => 'post_content',
			'type'        => 'richtext',
			'label'       => __( 'Content', 'sequensy' ),
			'description' => __( 'Post content (supports HTML)', 'sequensy' ),
			'order'       => 3,
		),
		array(
			'name'        => 'post_status',
			'type'        => 'button_group',
			'label'       => __( 'Status', 'sequensy' ),
			'description' => __( 'Post status', 'sequensy' ),
			'options'     => array(
				array(
					'value' => 'publish',
					'label' => __( 'Published', 'sequensy' ),
				),
				array(
					'value' => 'draft',
					'label' => __( 'Draft', 'sequensy' ),
				),
				array(
					'value' => 'pending',
					'label' => __( 'Pending Review', 'sequensy' ),
				),
				array(
					'value' => 'private',
					'label' => __( 'Private', 'sequensy' ),
				),
			),
			'default'     => 'draft',
			'order'       => 4,
		),
	),
	'output_fields'   => array(
		array(
			'name'        => 'post_id',
			'type'        => 'integer',
			'description' => __( 'ID of the updated post', 'sequensy' ),
			'example'     => '456',
		),
		array(
			'name'        => 'post_title',
			'type'        => 'string',
			'description' => __( 'Title of the post', 'sequensy' ),
			'example'     => 'Updated Post Title',
		),
		array(
			'name'        => 'post_type',
			'type'        => 'string',
			'description' => __( 'Type of the post', 'sequensy' ),
			'example'     => 'post',
		),
		array(
			'name'        => 'post_status',
			'type'        => 'string',
			'description' => __( 'Publication status of the post', 'sequensy' ),
			'example'     => 'publish',
		),
		array(
			'name'        => 'permalink',
			'type'        => 'string',
			'description' => __( 'URL to view the post', 'sequensy' ),
			'example'     => 'https://example.com/updated-post/',
		),
		array(
			'name'        => 'meta_updated',
			'type'        => 'boolean',
			'description' => __( 'Whether post meta was successfully updated', 'sequensy' ),
			'example'     => 'true',
		),
	),
	'handler'         => function ( array $config, array $context ): array {
		$post_id = absint( $config['post_id'] );

		// Verify post exists.
		$post = get_post( $post_id );
		if ( ! $post ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %d: post ID */
					esc_html__( 'Post not found: %d', 'sequensy' ),
					intval( $post_id )
				)
			);
		}

		// Build update data array.
		$post_data = array(
			'ID' => $post_id,
		);

		// Update title if provided.
		if ( ! empty( $config['post_title'] ) ) {
			$post_data['post_title'] = sanitize_text_field( $config['post_title'] );
		}

		// Update content if provided.
		if ( isset( $config['post_content'] ) ) {
			$post_data['post_content'] = wp_kses_post( $config['post_content'] );
		}

		// Update excerpt if provided.
		if ( isset( $config['post_excerpt'] ) ) {
			$post_data['post_excerpt'] = sanitize_textarea_field( $config['post_excerpt'] );
		}

		// Validate and update status if provided.
		if ( ! empty( $config['post_status'] ) ) {
			$valid_statuses = array( 'publish', 'draft', 'pending', 'private', 'future' );
			$post_status    = sanitize_key( $config['post_status'] );

			if ( ! in_array( $post_status, $valid_statuses, true ) ) {
				throw new \RuntimeException(
					sprintf(
						/* translators: %s: post status value */
						esc_html__( 'Invalid post status: %s', 'sequensy' ),
						esc_attr( $post_status )
					)
				);
			}

			$post_data['post_status'] = $post_status;
		}

		// Validate and update author if provided.
		if ( ! empty( $config['post_author'] ) ) {
			$author_id = absint( $config['post_author'] );
			if ( ! get_userdata( $author_id ) ) {
				throw new \RuntimeException(
					sprintf(
						/* translators: %d: user ID */
						esc_html__( 'Invalid author ID: %d', 'sequensy' ),
						intval( $author_id )
					)
				);
			}
			$post_data['post_author'] = $author_id;
		}

		// Update post.
		$result = wp_update_post( $post_data, true );

		// Check for errors.
		if ( is_wp_error( $result ) ) {
			throw new \RuntimeException( esc_html( $result->get_error_message() ) );
		}

		if ( ! $result ) {
			throw new \RuntimeException( esc_html__( 'Failed to update post', 'sequensy' ) );
		}

		// Set categories and tags if provided.
		PostHelpers::set_taxonomies( $post_id, $config );

		// Set post meta.
		$meta_result = PostHelpers::set_meta( $post_id, $config );

		// Get updated post object for return data.
		$post = get_post( $post_id );

		return array(
			'post_id'      => $post_id,
			'post_title'   => $post->post_title,
			'post_type'    => $post->post_type,
			'post_status'  => $post->post_status,
			'permalink'    => get_permalink( $post_id ),
			'meta_updated' => $meta_result['success'],
			'updated_at'   => current_time( 'mysql' ),
		);
	},
);
