<?php

namespace Alsendo\AlsendoWrapper\Api\Ecolet\Wrapper;

use Alsendo\AlsendoWrapper\Model\Order\SendOrderResponse;
use DateTimeImmutable;

class SendOrderResponseWrapper
{
    /**
     * @param array $orderToSend The raw 'order_to_send' object from Ecolet's response
     */
    public static function wrap(array $orderToSend): SendOrderResponse
    {
        $response = new SendOrderResponse();
        // order_id to prawdziwy ID zamówienia (dostępny po przetworzeniu),
        // id to order_to_send_id (dostępny od razu) — fallback
        $response->id = isset($orderToSend['order_id'])
            ? (string) $orderToSend['order_id']
            : (isset($orderToSend['id']) ? (string) $orderToSend['id'] : null);
        $response->status = $orderToSend['status'] ?? null;
        $response->serviceId = isset($orderToSend['order']['service_id']) ? (string) $orderToSend['order']['service_id'] : null;
        $response->serviceName = $orderToSend['order']['data']['courier']['service'] ?? null;
        $response->created = !empty($orderToSend['created_at']) ? new DateTimeImmutable($orderToSend['created_at']) : null;

        return $response;
    }
}
