<?php
/**
 * Schema for input_fields.
 *
 * @package Fast-Events/Schema
 * @since 1.0
 */

declare(strict_types=1);

/**
 * Return the input_fields schema.
 *
 * @since 2.4.0 - Added 'remain'.
 *              - Changed regex for 'roles'.
 *
 * @return array
 */
function fast_events_schema_input_fields(): array {
	return array(
		'$schema'     => 'http =>//json-schema.org/draft-04/schema#',
		'title'       => 'All input fields for ordering tickets',
		'description' => 'Input fields for events of the Fast Events WordPress plugin',
		'type'        => 'object',
		'properties'  => array(
			'fields'       => array(
				'description' => 'Array of extra input fields',
				'type'        => 'array',
				'minItems'    => 0,
				'maxItems'    => 100,
				'items'       => array(
					'type'       => 'object',
					'properties' => array(
						'name'        => array(
							'description' => 'Input label of the extra input fields',
							'type'        => 'string',
							'pattern'     => '^[^,]{3,30}$',
						),
						'id'          => array(
							'description' => 'The id of the extra input field',
							'type'        => 'string',
							'pattern'     => '^v\d+.{0,4}$$',
						),
						'type'        => array(
							'description' => 'The type of field',
							'type'        => 'string',
							'enum'        => array( 'text', 'number', 'select', 'password' ),
						),
						'value'       => array(
							'description' => 'Default value of the extra input field',
							'type'        => array( 'string', 'null' ),
						),
						'min'         => array(
							'description' => 'The minimum value or length',
							'type'        => array( 'string', 'null' ),
						),
						'max'         => array(
							'description' => 'The maximum value or length',
							'type'        => array( 'string', 'null' ),
						),
						'personalise' => array(
							'Description' => 'Whether or not the field requires personalisation',
							'type'        => 'integer',
							'enum'        => array( 0, 1 ),
						),
						'required'    => array(
							'Description' => 'Whether or not the field is required',
							'type'        => 'integer',
							'enum'        => array( 0, 1 ),
						),
					),
					'required'   => array( 'name', 'id', 'type', 'value', 'required' ),
				),
			),
			'tickets'      => array(
				'description' => 'Array of ticket types',
				'type'        => 'array',
				'minItems'    => 0,
				'maxItems'    => 100,
				'items'       => array(
					'type'       => 'object',
					'properties' => array(
						'name'   => array(
							'description' => 'Input label of the ticket fields',
							'type'        => 'string',
							'pattern'     => '^[^,]{3,30}$',
						),
						'stock'  => array(
							'Description' => 'Stock for this ticket',
							'type'        => array( 'string', 'integer' ),
							'pattern'     => '^\d*',
						),
						'sold'   => array(
							'Description' => 'Sold tickets',
							'type'        => 'integer',
							'minimum'     => 0,
						),
						'id'     => array(
							'description' => 'The id of the extra input field',
							'type'        => 'string',
							'pattern'     => '^v\d+.{0,4}$',
						),
						'price'  => array(
							'Description' => 'The price of a single ticket',
							'type'        => 'string',
							'pattern'     => '^\d+\\.?\d{0,4}$',
						),
						'min'    => array(
							'Description' => 'Minimum # of tickets to order',
							'type'        => 'integer',
							'minimum'     => 0,
						),
						'max'    => array(
							'Description' => 'Maximum # of tickets to order',
							'type'        => 'integer',
							'minimum'     => 0,
						),
						'vat'    => array(
							'Description' => 'VAT percentage of the ticket',
							'type'        => 'string',
							'pattern'     => '^\d+\\.?\d{0,4}$',
						),
						'cnt'    => array(
							'Description' => 'Whether or not to subtract if from the stock',
							'type'        => 'integer',
							'enum'        => array( 0, 1 ),
						),
						'remain' => array(
							'description' => 'Show the remaining tickets in the order form.',
							'type'        => 'integer',
							'enum'        => array( 0, 1 ),
						),
					),
					'required'   => array( 'name', 'stock', 'sold', 'id', 'price', 'min', 'max', 'vat', 'cnt', 'remain' ),
				),
			),
			'submit'       => array(
				'description' => 'Submit label',
				'type'        => 'object',
				'properties'  => array(
					'name' => array(
						'description' => 'Label of payment button',
						'type'        => 'string',
						'minLength'   => 1,
						'maxLength'   => 30,
					),
				),
				'required'    => array( 'name' ),
			),
			'unique_users' => array(
				'description' => 'Unique users object',
				'type'        => 'object',
				'properties'  => array(
					'master_eventids' => array(
						'description' => 'String of event_ids separated by a comma',
						'type'        => array( 'string', 'null' ),
						'pattern'     => '^(\d+(\\,\d+)*)*$',
					),
				),
				'required'    => array( 'master_eventids' ),
			),
			'user_group'   => array(
				'description' => 'User group object',
				'type'        => 'object',
				'properties'  => array(
					'id'          => array(
						'description' => 'Type of user group',
						'type'        => 'integer',
						'enum'        => array( 0, 1, 2, 3 ),
					),
					'eventid'     => array(
						'description' => 'Eventid for closed user group',
						'type'        => array( 'string', 'null' ),
						'pattern'     => '^([1-9]\d*)*$',
					),
					'roles'       => array(
						'description' => 'WordPress roles separated by a comma',
						'type'        => array( 'string', 'null' ),
						'pattern'     => '^([a-z_-]*(\\,[a-z_-]+)*)*$',
					),
					'rest_header' => array(
						'description' => 'HTTP header',
						'type'        => array( 'string', 'null' ),
						'pattern'     => '^([^:]+:[^:]+)?$',
						'maxLength'   => 128,
					),
					'rest_url'    => array(
						'description' => 'REST API url',
						'type'        => array( 'string', 'null' ),
						'pattern'     => '^((((https:(?:\\/\\/)?)(?:[\\-;:&=\\+\\$,\\w]+@)?[A-Za-z0-9\\.\\-]+|(?:www\\.|[\\-;:&=\\+\\$,\\w]+@)[A-Za-z0-9\\.\\-]+)((?:\\/[\\+~%\\/\\.\\w\\-_]*)?\\??(?:[\\-\\+=&;%@\\.\\w_]*)#?(?:[\\.\\!\\/\\\\\\w]*))?))*$',
					),
				),
				'required'    => array( 'id', 'eventid', 'roles', 'rest_url', 'rest_header' ),
			),
		),
		'required'    => array( 'fields', 'tickets', 'submit', 'unique_users', 'user_group' ),
	);
}
