<?php

namespace Alsendo\AlsendoWrapper\Api\Ecolet\Wrapper;

class OrderResponseWrapper
{
    /**
     * Wrap the response from Ecolet API into a format that is used by the Alsendo Wrapper.
     *
     * @param array $decodedJson The decoded JSON from the Ecolet API
     * @return array The parsed Order
     */
    public static function wrap(array $decodedJson): array
    {
        $parsedOrder = [
            'id' => $decodedJson['order_id'],
            'status' => $decodedJson['status'],
            'sender' => $decodedJson['order']['data']['sender'],
            'receiver' => $decodedJson['order']['data']['receiver'],
            'service' => [
                'slug' => $decodedJson['order']['data']['courier']['service']
            ],
            'service_id' => $decodedJson['order']['data']['service_id'],
            'pickup_type' => $decodedJson['order']['data']['courier']['pickup']['type'],
            'pickup_date' => $decodedJson['order']['data']['courier']['pickup']['date'],
            'pickup_hour' => $decodedJson['order']['data']['courier']['pickup']['time'],
            'type' => $decodedJson['order']['data']['parcel']['type'],
            'weight' => $decodedJson['order']['data']['parcels'][0]['weight'],
            'length' => $decodedJson['order']['data']['parcels'][0]['dimensions']['length'],
            'height' => $decodedJson['order']['data']['parcels'][0]['dimensions']['height'],
            'width' => $decodedJson['order']['data']['parcels'][0]['dimensions']['width'],
            'shape' => $decodedJson['order']['data']['parcel']['shape'],
            'declared_value' => $decodedJson['order']['data']['parcels'][0]['declared_value'],
            'amount' => $decodedJson['order']['data']['parcel']['amount'],
            'content' => $decodedJson['order']['data']['parcels'][0]['content'],
            'observations' => $decodedJson['order']['data']['parcel']['observations'],
            'created_at' => $decodedJson['created_at'],
        ];
        if (isset($decodedJson['order']['data']['additional_services']['cod']) && $decodedJson['order']['data']['additional_services']['cod']['status']) {
            $parsedOrder['cod'] = $decodedJson['order']['data']['additional_services']['cod']['amount'];
        }
        return $parsedOrder;
    }

}