<?php
/**
 * Update Order Status Action Definition
 *
 * @package Sequensy\Integrations\WooCommerce
 * @since   1.0.0
 */

declare(strict_types=1);

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

return array(
	'type'            => 'update_order_status',
	'label'           => __( 'Update Order Status', 'sequensy' ),
	'description'     => __( 'Change the status of a WooCommerce order', 'sequensy' ),
	'category'        => 'order',
	'required_fields' => array( 'order_id', 'status' ),
	'schema'          => array(
		array(
			'name'        => 'order_id',
			'type'        => 'number',
			'label'       => __( 'Order ID', 'sequensy' ),
			'description' => __( 'ID of the order to update', 'sequensy' ),
			'required'    => true,
			'order'       => 1,
		),
		array(
			'name'        => 'status',
			'type'        => 'select',
			'label'       => __( 'Status', 'sequensy' ),
			'description' => __( 'New status for the order', 'sequensy' ),
			'required'    => true,
			'options'     => array(
				array(
					'value' => 'pending',
					'label' => __( 'Pending Payment', 'sequensy' ),
				),
				array(
					'value' => 'processing',
					'label' => __( 'Processing', 'sequensy' ),
				),
				array(
					'value' => 'on-hold',
					'label' => __( 'On Hold', 'sequensy' ),
				),
				array(
					'value' => 'completed',
					'label' => __( 'Completed', 'sequensy' ),
				),
				array(
					'value' => 'cancelled',
					'label' => __( 'Cancelled', 'sequensy' ),
				),
				array(
					'value' => 'refunded',
					'label' => __( 'Refunded', 'sequensy' ),
				),
				array(
					'value' => 'failed',
					'label' => __( 'Failed', 'sequensy' ),
				),
			),
			'order'       => 2,
		),
		array(
			'name'        => 'note',
			'type'        => 'text',
			'label'       => __( 'Note', 'sequensy' ),
			'description' => __( 'Optional note to add when changing status', 'sequensy' ),
			'order'       => 3,
		),
	),
	'output_fields'   => array(
		array(
			'name'        => 'order_id',
			'type'        => 'integer',
			'description' => __( 'ID of the updated order', 'sequensy' ),
			'example'     => '1234',
		),
		array(
			'name'        => 'old_status',
			'type'        => 'string',
			'description' => __( 'Previous status of the order', 'sequensy' ),
			'example'     => 'processing',
		),
		array(
			'name'        => 'new_status',
			'type'        => 'string',
			'description' => __( 'New status of the order', 'sequensy' ),
			'example'     => 'completed',
		),
		array(
			'name'        => 'order_total',
			'type'        => 'string',
			'description' => __( 'Total amount of the order', 'sequensy' ),
			'example'     => '99.99',
		),
	),
	'handler'         => function ( array $config, array $context ): array {
		$order_id = absint( $config['order_id'] );
		$order    = wc_get_order( $order_id );

		if ( ! $order ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %d: order ID */
					esc_html__( 'Order not found: %d', 'sequensy' ),
					esc_html( $order_id )
				)
			);
		}

		$valid_statuses = array( 'pending', 'processing', 'on-hold', 'completed', 'cancelled', 'refunded', 'failed' );
		$new_status     = sanitize_key( $config['status'] );

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

		$old_status = $order->get_status();
		$note       = ! empty( $config['note'] ) ? sanitize_text_field( $config['note'] ) : '';

		$order->update_status( $new_status, $note );

		return array(
			'order_id'    => $order->get_id(),
			'old_status'  => $old_status,
			'new_status'  => $new_status,
			'order_total' => $order->get_total(),
		);
	},
);
