<?php

namespace PostenBringCheckout\checkout\store;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema;

/**
 * Store API model extensions
 *
 * Class responsible for registering our custom data fiels with the WooCommerce checkout schema
 *
 * @since 0.0.1
 */
class CheckoutSchemaExtensions {

	/**
	 * Initializes our custom schema extensions
	 *
	 * @since 0.0.1
	 */
	public static function register(): void {
        woocommerce_store_api_register_endpoint_data(
            [
                'endpoint' => CheckoutSchema::IDENTIFIER,
                'namespace' => 'posten-bring-checkout',
                'schema_callback' => ['PostenBringCheckout\checkout\store\CheckoutSchemaExtensions', 'extend_checkout_schema'],
                'schema_type' => ARRAY_A,
            ]
        );
	}

	/**
	 * Returns our checkout schema extension definitions
	 *
	 * @return array Array of extension definitions
	 * @since 0.0.1
	 */
	public static function extend_checkout_schema(): array {
		return [
			'selectedBringPickupPointId'   => [
				'description' => 'Selected Bring pickup point id',
				'type'        => 'string',
				'context'     => ['view', 'edit'],
				'readonly'    => true,
				'optional'    => true,
				'arg_options' => [
					'validate_callback' => function( $value ) {
						if (!empty($value)) {
							return is_string( $value );
						} else {
							return true;
						}
					},
				]
			],
			'selectedBringDeliveryDate'   => [
				'description' => 'Selected Bring delivery date for home delivery parcels',
				'type'        => 'string',
				'context'     => ['view', 'edit'],
				'readonly'    => true,
				'optional'    => true,
				'arg_options' => [
					'validate_callback' => function( $value ) {
						if (!empty($value)) {
							return is_string( $value );
						} else {
							return true;
						}
					},
				]
			],
			'selectedBringDeliveryShippingDate'   => [
				'description' => 'Shipping date used for calculating selected delivery window alternative',
				'type'        => 'string',
				'context'     => ['view', 'edit'],
				'readonly'    => true,
				'optional'    => true,
				'arg_options' => [
					'validate_callback' => function( $value ) {
						if (!empty($value)) {
							return is_string( $value );
						} else {
							return true;
						}
					},
				]
			]
		];
	}
}
