<?php

namespace PostenBringCheckout\admin\bring\models\booking;

use PostenBringCheckout\admin\api\models\OrderMapper;
use PostenBringCheckout\admin\api\models\OrderRequest;

defined( 'ABSPATH' ) || exit;

class ValidationRequest {
	private $bookingRequests;

	public function __construct( array $wc_orders ) {
		$this->bookingRequests = array_map(
			function ( $wc_order ) {
				$order_request = new OrderRequest(
					array(
						'orderRef'               => $wc_order->get_id(),
						'testIndicator'          => false,
						'selectedShippingOption' => OrderMapper::getOrderShippingOption( $wc_order ) ?: 'standard',
						'customerNumber'         => '1234',
						'packages'               => array(
							array(
								'weightInKg'    => 0.1,
								'volumeInDm3'   => 1.0,
								'dimensions'    => array(
									'heightInCm' => 1,
									'lengthInCm' => 1,
									'widthInCm'  => 1,
								),
								'correlationId' => '1',
							),
						),
					)
				);

				return WebshopBookingMapper::mapToWebshopBooking( $wc_order, $order_request );
			},
			$wc_orders
		);
	}

	public function toRequestObject(): array {
		$requestData = array();

		// Check if there are any booking requests
		if ( ! empty( $this->bookingRequests ) ) {
			$requestData['bookingRequests'] = array();

			// Loop and add booking requests to the array
			if ( is_array( $this->bookingRequests ) ) {
				foreach ( $this->bookingRequests as $bookingRequest ) {
					$requestData['bookingRequests'][] = $bookingRequest;
				}
			} else {
				// Handle single booking request
				$requestData['bookingRequests'][] = $this->bookingRequests;
			}
		}

		return $requestData;
	}
}
