<?php
// @generated by build-free-packages.mjs — DO NOT EDIT.
/**
 * Create Coupon 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'            => 'create_coupon',
	'label'           => __( 'Create Coupon', 'sequensy' ),
	'description'     => __( 'Create a new WooCommerce discount coupon', 'sequensy' ),
	'category'        => 'coupon',
	'required_fields' => array( 'code', 'amount' ),
	'schema'          => array(
		array(
			'name'        => 'code',
			'type'        => 'string',
			'label'       => __( 'Coupon Code', 'sequensy' ),
			'description' => __( 'Unique coupon code', 'sequensy' ),
			'required'    => true,
			'order'       => 1,
		),
		array(
			'name'        => 'discount_type',
			'type'        => 'select',
			'label'       => __( 'Discount Type', 'sequensy' ),
			'description' => __( 'Type of discount to apply', 'sequensy' ),
			'options'     => array(
				array(
					'value' => 'percent',
					'label' => __( 'Percentage Discount', 'sequensy' ),
				),
				array(
					'value' => 'fixed_cart',
					'label' => __( 'Fixed Cart Discount', 'sequensy' ),
				),
				array(
					'value' => 'fixed_product',
					'label' => __( 'Fixed Product Discount', 'sequensy' ),
				),
			),
			'default'     => 'percent',
			'order'       => 2,
		),
		array(
			'name'        => 'amount',
			'type'        => 'number',
			'label'       => __( 'Amount', 'sequensy' ),
			'description' => __( 'Discount amount (percentage or fixed value)', 'sequensy' ),
			'required'    => true,
			'order'       => 3,
		),
	),
	'output_fields'   => array(
		array(
			'name'        => 'coupon_id',
			'type'        => 'integer',
			'description' => __( 'ID of the created coupon', 'sequensy' ),
			'example'     => '789',
		),
		array(
			'name'        => 'code',
			'type'        => 'string',
			'description' => __( 'The coupon code', 'sequensy' ),
			'example'     => 'SUMMER20',
		),
		array(
			'name'        => 'discount_type',
			'type'        => 'string',
			'description' => __( 'Type of discount applied', 'sequensy' ),
			'example'     => 'percent',
		),
		array(
			'name'        => 'amount',
			'type'        => 'string',
			'description' => __( 'Discount amount', 'sequensy' ),
			'example'     => '20',
		),
	),
	'handler'         => function ( array $config, array $context ): array {
		$code = sanitize_text_field( $config['code'] );

		// Check for duplicate coupon code.
		$existing_id = wc_get_coupon_id_by_code( $code );
		if ( $existing_id ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %s: coupon code */
					esc_html__( 'Coupon code already exists: %s', 'sequensy' ),
					esc_html( $code )
				)
			);
		}

		$valid_types   = array( 'percent', 'fixed_cart', 'fixed_product' );
		$discount_type = ! empty( $config['discount_type'] ) ? sanitize_key( $config['discount_type'] ) : 'percent';

		if ( ! in_array( $discount_type, $valid_types, true ) ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %s: discount type */
					esc_html__( 'Invalid discount type: %s', 'sequensy' ),
					esc_html( $discount_type )
				)
			);
		}

		$amount = (float) $config['amount'];
		if ( $amount <= 0 ) {
			throw new \RuntimeException( esc_html__( 'Coupon amount must be greater than zero', 'sequensy' ) );
		}

		$coupon = new \WC_Coupon();
		$coupon->set_code( $code );
		$coupon->set_discount_type( $discount_type );
		$coupon->set_amount( $amount );

		if ( ! empty( $config['description'] ) ) {
			$coupon->set_description( sanitize_text_field( $config['description'] ) );
		}

		if ( isset( $config['individual_use'] ) ) {
			$coupon->set_individual_use( (bool) $config['individual_use'] );
		}

		if ( ! empty( $config['usage_limit'] ) ) {
			$coupon->set_usage_limit( absint( $config['usage_limit'] ) );
		}

		if ( ! empty( $config['expiry_date'] ) ) {
			$coupon->set_date_expires( sanitize_text_field( $config['expiry_date'] ) );
		}

		if ( isset( $config['free_shipping'] ) ) {
			$coupon->set_free_shipping( (bool) $config['free_shipping'] );
		}

		$coupon_id = $coupon->save();

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

		return array(
			'coupon_id'     => $coupon_id,
			'code'          => $code,
			'discount_type' => $discount_type,
			'amount'        => (string) $amount,
		);
	},
);
