<?php
// @generated by build-free-packages.mjs — DO NOT EDIT.
/**
 * Create User 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;
}

return array(
	'type'            => 'create_user',
	'label'           => __( 'Create User', 'sequensy' ),
	'description'     => __( 'Create a new WordPress user with specified role and profile information.', 'sequensy' ),
	'category'        => 'user',
	'required_fields' => array( 'email', 'username' ),
	'schema'          => array(
		array(
			'name'        => 'email',
			'type'        => 'string',
			'label'       => __( 'Email', 'sequensy' ),
			'description' => __( 'User email address', 'sequensy' ),
			'required'    => true,
			'order'       => 2,
		),
		array(
			'name'        => 'username',
			'type'        => 'string',
			'label'       => __( 'Username', 'sequensy' ),
			'description' => __( 'Login username', 'sequensy' ),
			'required'    => true,
			'order'       => 3,
		),
		array(
			'name'        => 'role',
			'type'        => 'select',
			'label'       => __( 'Role', 'sequensy' ),
			'description' => __( 'User role', 'sequensy' ),
			'options'     => array(
				array(
					'value' => 'subscriber',
					'label' => __( 'Subscriber', 'sequensy' ),
				),
				array(
					'value' => 'contributor',
					'label' => __( 'Contributor', 'sequensy' ),
				),
				array(
					'value' => 'author',
					'label' => __( 'Author', 'sequensy' ),
				),
				array(
					'value' => 'editor',
					'label' => __( 'Editor', 'sequensy' ),
				),
				array(
					'value' => 'administrator',
					'label' => __( 'Administrator', 'sequensy' ),
				),
			),
			'default'     => 'subscriber',
			'order'       => 6,
		),
	),
	'output_fields'   => array(
		array(
			'name'        => 'user_id',
			'type'        => 'integer',
			'description' => __( 'ID of the created user', 'sequensy' ),
			'example'     => '42',
		),
		array(
			'name'        => 'username',
			'type'        => 'string',
			'description' => __( 'Username of the user', 'sequensy' ),
			'example'     => 'johndoe',
		),
		array(
			'name'        => 'email',
			'type'        => 'string',
			'description' => __( 'Email address of the user', 'sequensy' ),
			'example'     => 'john@example.com',
		),
		array(
			'name'        => 'notification_sent',
			'type'        => 'boolean',
			'description' => __( 'Whether notification email was sent', 'sequensy' ),
			'example'     => 'true',
		),
		array(
			'name'        => 'meta_updated',
			'type'        => 'boolean',
			'description' => __( 'Whether user meta was successfully updated', 'sequensy' ),
			'example'     => 'true',
		),
	),
	'handler'         => function ( array $config, array $context ): array {
		// Validate and sanitize email.
		$email = sanitize_email( $config['email'] );

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

		if ( email_exists( $email ) ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %s: email address */
					esc_html__( 'Email already exists: %s', 'sequensy' ),
					esc_attr( $email )
				)
			);
		}

		// Validate username availability.
		$username = sanitize_user( $config['username'] );

		if ( username_exists( $username ) ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %s: username */
					esc_html__( 'Username already exists: %s', 'sequensy' ),
					esc_attr( $username )
				)
			);
		}

		// Validate role.
		$role           = ! empty( $config['role'] ) ? sanitize_key( $config['role'] ) : 'subscriber';
		$editable_roles = array_keys( get_editable_roles() );

		if ( ! in_array( $role, $editable_roles, true ) ) {
			throw new \RuntimeException(
				sprintf(
					/* translators: %s: user role */
					esc_html__( 'Invalid role: %s', 'sequensy' ),
					esc_attr( $role )
				)
			);
		}

		// Build user data.
		$user_data = array(
			'user_login' => $username,
			'user_email' => $email,
			'user_pass'  => wp_generate_password( 12, true, true ),
			'role'       => $role,
		);

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

		if ( ! empty( $config['last_name'] ) ) {
			$user_data['last_name'] = sanitize_text_field( $config['last_name'] );
		}

		// Insert user.
		$user_id = wp_insert_user( $user_data );

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

		// Set user meta.
		$meta_result = array( 'success' => true );
		if ( ! empty( $config['user_meta'] ) && is_array( $config['user_meta'] ) ) {
			$updated_count = 0;
			foreach ( $config['user_meta'] as $meta_key => $meta_value ) {
				$meta_key   = sanitize_key( $meta_key );
				$meta_value = sanitize_text_field( $meta_value );
				update_user_meta( $user_id, $meta_key, $meta_value );
				++$updated_count;
			}
			$meta_result = array(
				'success'       => true,
				'updated_count' => $updated_count,
				'failed_count'  => 0,
			);
		}

		// Send notification.
		$send_notification = isset( $config['send_notification'] ) ? (bool) $config['send_notification'] : false;
		$notification_sent = false;

		if ( $send_notification ) {
			wp_new_user_notification( $user_id, null, 'both' );
			$notification_sent = true;
		}

		return array(
			'user_id'           => $user_id,
			'username'          => $username,
			'email'             => $email,
			'notification_sent' => $notification_sent,
			'meta_updated'      => $meta_result['success'],
			'created_at'        => current_time( 'mysql' ),
		);
	},
);
