<?php
// @generated by build-free-packages.mjs — DO NOT EDIT.
/**
 * Create 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'            => 'create_post',
	'label'           => __( 'Create Post', 'sequensy' ),
	'description'     => __( 'Create a new post or page. Supports categories, tags, and custom fields.', 'sequensy' ),
	'category'        => 'post',
	'required_fields' => array( 'post_title' ),
	'schema'          => array(
		array(
			'name'        => 'post_title',
			'type'        => 'string',
			'label'       => __( 'Title', 'sequensy' ),
			'description' => __( 'Post title', 'sequensy' ),
			'required'    => true,
			'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,
		),
		array(
			'name'        => 'post_type',
			'type'        => 'button_group',
			'label'       => __( 'Post Type', 'sequensy' ),
			'description' => __( 'Type of post to create', 'sequensy' ),
			'options'     => array(
				array(
					'value' => 'post',
					'label' => __( 'Post', 'sequensy' ),
				),
				array(
					'value' => 'page',
					'label' => __( 'Page', 'sequensy' ),
				),
			),
			'default'     => 'post',
			'order'       => 5,
		),
	),
	'output_fields'   => array(
		array(
			'name'        => 'post_id',
			'type'        => 'integer',
			'description' => __( 'ID of the created post', 'sequensy' ),
			'example'     => '456',
		),
		array(
			'name'        => 'post_title',
			'type'        => 'string',
			'description' => __( 'Title of the post', 'sequensy' ),
			'example'     => 'Welcome Post',
		),
		array(
			'name'        => 'post_type',
			'type'        => 'string',
			'description' => __( 'Type of the post (post, page, etc.)', '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/welcome-post/',
		),
		array(
			'name'        => 'meta_updated',
			'type'        => 'boolean',
			'description' => __( 'Whether post meta was successfully updated', 'sequensy' ),
			'example'     => 'true',
		),
	),
	'handler'         => function ( array $config, array $context ): array {
		// Validate post type.
		$post_type = ! empty( $config['post_type'] ) ? sanitize_key( $config['post_type'] ) : 'post';

		if ( ! post_type_exists( $post_type ) ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %s: post type slug */
					esc_html__( 'Invalid post type: %s', 'sequensy' ),
					esc_attr( $post_type )
				)
			);
		}

		// Validate post status.
		$valid_statuses = array( 'publish', 'draft', 'pending', 'private', 'future' );
		$post_status    = ! empty( $config['post_status'] ) ? sanitize_key( $config['post_status'] ) : 'draft';

		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 )
				)
			);
		}

		// Validate 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 )
					)
				);
			}
		}

		// Build post data.
		$post_data = array(
			'post_title'  => sanitize_text_field( $config['post_title'] ),
			'post_type'   => $post_type,
			'post_status' => $post_status,
		);

		if ( ! empty( $config['post_content'] ) ) {
			$post_data['post_content'] = wp_kses_post( $config['post_content'] );
		}

		if ( ! empty( $config['post_excerpt'] ) ) {
			$post_data['post_excerpt'] = sanitize_textarea_field( $config['post_excerpt'] );
		}

		if ( ! empty( $config['post_author'] ) ) {
			$post_data['post_author'] = absint( $config['post_author'] );
		}

		// Insert post.
		$post_id = wp_insert_post( $post_data, true );

		if ( is_wp_error( $post_id ) ) {
			throw new \RuntimeException( esc_html( $post_id->get_error_message() ) );
		}

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

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

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