<?php
/**
 * Create Subscriber Action Definition
 *
 * @package Sequensy\Integrations\Kit
 * @since 1.0.0
 */

declare(strict_types=1);

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

return array(
	'type'            => 'create_subscriber',
	'label'           => __( 'Create Subscriber', 'sequensy' ),
	'description'     => __( 'Create a new subscriber or update an existing one by email address.', 'sequensy' ),
	'category'        => 'subscriber',
	'required_fields' => array( 'email_address' ),
	'schema'          => array(
		array(
			'name'        => 'email_address',
			'type'        => 'string',
			'label'       => __( 'Email Address', 'sequensy' ),
			'description' => __( 'Subscriber email address', 'sequensy' ),
			'required'    => true,
			'order'       => 1,
			'width'       => '1/2',
		),
		array(
			'name'        => 'first_name',
			'type'        => 'string',
			'label'       => __( 'First Name', 'sequensy' ),
			'order'       => 2,
			'width'       => '1/2',
		),
		array(
			'name'        => 'custom_fields',
			'type'        => 'key_value',
			'label'       => __( 'Custom Fields', 'sequensy' ),
			'description' => __( 'Custom field names and values', 'sequensy' ),
			'order'       => 3,
		),
	),
	'output_fields'   => array(
		array(
			'name'        => 'subscriber_id',
			'type'        => 'string',
			'description' => __( 'Subscriber ID', 'sequensy' ),
			'example'     => '12345',
		),
		array(
			'name'        => 'email_address',
			'type'        => 'string',
			'description' => __( 'Email address of the subscriber', 'sequensy' ),
			'example'     => 'john@example.com',
		),
		array(
			'name'        => 'first_name',
			'type'        => 'string',
			'description' => __( 'First name of the subscriber', 'sequensy' ),
			'example'     => 'John',
		),
		array(
			'name'        => 'state',
			'type'        => 'string',
			'description' => __( 'Subscriber state', 'sequensy' ),
			'example'     => 'active',
		),
		array(
			'name'        => 'created_at',
			'type'        => 'string',
			'description' => __( 'Date the subscriber was created', 'sequensy' ),
			'example'     => '2024-01-15T10:30:00Z',
		),
	),
	'handler'         => function ( array $config, array $context ): array {
		$email = sanitize_email( $config['email_address'] );

		if ( ! is_email( $email ) ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %s: email address */
					esc_html__( 'Invalid email address: %s', 'sequensy' ),
					esc_attr( $config['email_address'] )
				)
			);
		}

		$data = array(
			'email_address' => $email,
		);

		if ( ! empty( $config['first_name'] ) ) {
			$data['first_name'] = sanitize_text_field( $config['first_name'] );
		}

		if ( ! empty( $config['custom_fields'] ) && is_array( $config['custom_fields'] ) ) {
			$fields = array();
			foreach ( $config['custom_fields'] as $key => $value ) {
				$fields[ sanitize_text_field( $key ) ] = sanitize_text_field( $value );
			}
			$data['fields'] = $fields;
		}

		$result = sequensy_api( 'kit' )->create_subscriber( $data );

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

		$subscriber = $result['data']['subscriber'] ?? $result['data'] ?? array();

		return array(
			'subscriber_id' => (string) ( $subscriber['id'] ?? '' ),
			'email_address'  => $subscriber['email_address'] ?? $email,
			'first_name'    => $subscriber['first_name'] ?? '',
			'state'         => $subscriber['state'] ?? '',
			'created_at'    => $subscriber['created_at'] ?? '',
		);
	},
);
