<?php

namespace Alsendo\AlsendoWrapper\Tests\Unit\Api\Ecolet;

use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Order\Created\EcoletOrderCreatedResponse;
use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Order\EcoletOrderResponse;
use Alsendo\AlsendoWrapper\Api\Ecolet\Wrapper\OrderResponseWrapper;
use Alsendo\AlsendoWrapper\Json;
use JsonException;
use PHPUnit\Framework\TestCase;

class EcoletOrderToSendResponseTest extends TestCase
{
    /**
     * @throws JsonException
     */
    public function testCreateOrderToSendResponseFromJson(): void
    {
        $json = '{
  "order_to_send" : {
  "id": 3080906,
  "status": "ordered",
  "error": "",
  "order_id": 2526443,
  "order": {
    "data": {
      "sender": {
        "name": "Test Ecolet Company",
        "postal_code": 800001,
        "contact_person": "-",
        "email": "testowy-ecolet-test@test-ecolet.pl",
        "phone": "0725942215",
        "country": "ro",
        "locality_id": 4980,
        "street_name": "or. GALATI [800275] str. PODUL INALT Nr. 1",
        "street_number": 1,
        "block": "-",
        "entrance": "-",
        "floor": "-",
        "flat": "-",
        "has_map_point": 1,
        "map_point_id": 15650,
        "county": "Galati",
        "locality": "Galati",
        "company": ""
      },
      "receiver": {
        "name": "Test Rumunia",
        "postal_code": 905900,
        "contact_person": "-",
        "email": "test@ecolet-testowy.pl",
        "phone": "0737659039",
        "country": "ro",
        "locality_id": 8409,
        "street_name": "or. CONSTANTA [900330] str. INTERIOARA intr. 3",
        "street_number": 1,
        "block": "-",
        "entrance": "-",
        "floor": "-",
        "flat": "-",
        "has_map_point": 1,
        "map_point_id": 2303,
        "county": "Constanta",
        "locality": "Ovidiu",
        "company": ""
      },
      "parcel": {
        "type": "package",
        "shape": "standard",
        "amount": "",
        "observations": ""
      },
      "parcels": [
        {
          "weight": 1,
          "dimensions": {
            "length": 9,
            "width": 12,
            "height": 5
          },
          "declared_value": "",
          "content": "books box"
        }
      ],
      "additional_services": {
        "cod": {
          "status": 1,
          "amount": 0
        }
      },
      "courier": {
        "service": "dpd_office_point_to_point",
        "pickup": {
          "type": "self",
          "date": "2025-06-17",
          "time": "15:00"
        }
      }
    },
    "shipment_type": "primary",
    "user_id": 80926,
    "user_bank_account_id": 20807,
    "courier_id": 2,
    "service_id": 46,
    "api_id": 2505,
    "sender_locality": {
      "id": 1542,
      "county_id": 8,
      "name": "Braila",
      "municipality": "Braila",
      "keywords": "Braila Braila",
      "postal_code": 810001,
      "has_streets": 1,
      "population": 180302,
      "latitude": 27,
      "longitude": 45,
      "dpd_site_id": 642142691,
      "dpd_serving_days": "1111110",
      "sameday_city_id": 8705,
      "sameday_county_id": 9,
      "courier_locations": [
        {
          "id": 3148,
          "courier_id": 1,
          "locality_id": 1542,
          "locality_alias": "Braila",
          "distance": 0,
          "zone": ""
        },
        {
          "id": 16691,
          "courier_id": 3,
          "locality_id": 1542,
          "locality_alias": "BRAILA",
          "distance": 0,
          "zone": ""
        },
        {
          "id": 30251,
          "courier_id": 6,
          "locality_id": 1542,
          "locality_alias": "",
          "distance": 0,
          "zone": ""
        },
        {
          "id": 47542,
          "courier_id": 8,
          "locality_id": 1542,
          "locality_alias": "",
          "distance": 0,
          "zone": ""
        },
        {
          "id": 62040,
          "courier_id": 9,
          "locality_id": 1542,
          "locality_alias": "",
          "distance": 0,
          "zone": "0"
        },
        {
          "id": 78922,
          "courier_id": 10,
          "locality_id": 1542,
          "locality_alias": "",
          "distance": 0,
          "zone": ""
        }
      ]
    },
    "receiver_locality": {
      "id": 3150,
      "county_id": 15,
      "name": "Constanta",
      "municipality": "Constanta",
      "keywords": "Constanta Constanta",
      "postal_code": 900003,
      "has_streets": 1,
      "population": 283872,
      "latitude": 28,
      "longitude": 44,
      "dpd_site_id": 642160428,
      "dpd_serving_days": "1111110",
      "sameday_city_id": 9384,
      "sameday_county_id": 15,
      "courier_locations": [
        {
          "id": 4851,
          "courier_id": 1,
          "locality_id": 3150,
          "locality_alias": "Constanta",
          "distance": 0,
          "zone": ""
        },
        {
          "id": 18396,
          "courier_id": 3,
          "locality_id": 3150,
          "locality_alias": "CONSTANTA",
          "distance": 0,
          "zone": ""
        },
        {
          "id": 31973,
          "courier_id": 6,
          "locality_id": 3150,
          "locality_alias": "",
          "distance": 0,
          "zone": ""
        },
        {
          "id": 47775,
          "courier_id": 8,
          "locality_id": 3150,
          "locality_alias": "",
          "distance": 0,
          "zone": ""
        },
        {
          "id": 62289,
          "courier_id": 9,
          "locality_id": 3150,
          "locality_alias": "",
          "distance": 0,
          "zone": "0"
        },
        {
          "id": 77430,
          "courier_id": 10,
          "locality_id": 3150,
          "locality_alias": "",
          "distance": 0,
          "zone": ""
        }
      ]
    },
    "sender_map_point": {
      "id": 15650,
      "name": "GALATI - PODUL INALT (DPD SHOP)",
      "locality_id": 4980,
      "address": "or. GALATI [800275] str. PODUL INALT Nr. 1",
      "type": "point",
      "image": "/images/couriers/dpd_office.png",
      "latitude": 45,
      "longitude": 28,
      "is_active": 1,
      "is_cod_available": 1,
      "is_for_sender": 1,
      "is_for_receiver": 1,
      "monday_start_hour": "09:00",
      "monday_end_hour": "19:00",
      "tuesday_start_hour": "09:00",
      "tuesday_end_hour": "19:00",
      "wednesday_start_hour": "09:00",
      "wednesday_end_hour": "19:00",
      "thursday_start_hour": "09:00",
      "thursday_end_hour": "19:00",
      "friday_start_hour": "09:00",
      "friday_end_hour": "19:00",
      "saturday_start_hour": "",
      "saturday_end_hour": "",
      "sunday_start_hour": "",
      "sunday_end_hour": "",
      "created_at": "2024-07-05T23:00:32.000000Z",
      "updated_at": "2025-06-16T23:00:17.000000Z",
      "courier_map_points": [
        {
          "id": 15650,
          "map_point_id": 15650,
          "courier_id": 2,
          "courier_point_api_key": 20955,
          "created_at": "",
          "updated_at": ""
        }
      ]
    },
    "receiver_map_point": {
      "id": 2303,
      "name": "CONSTANTA",
      "locality_id": 8409,
      "address": "or. CONSTANTA [900330] str. INTERIOARA intr. 3",
      "type": "point",
      "image": "/images/couriers/dpd_office.png",
      "latitude": 44,
      "longitude": 28,
      "is_active": 1,
      "is_cod_available": 1,
      "is_for_sender": 1,
      "is_for_receiver": 1,
      "monday_start_hour": "14:30",
      "monday_end_hour": "17:00",
      "tuesday_start_hour": "14:30",
      "tuesday_end_hour": "17:00",
      "wednesday_start_hour": "14:30",
      "wednesday_end_hour": "17:00",
      "thursday_start_hour": "14:30",
      "thursday_end_hour": "17:00",
      "friday_start_hour": "14:30",
      "friday_end_hour": "17:00",
      "saturday_start_hour": "00:00",
      "saturday_end_hour": "00:00",
      "sunday_start_hour": "",
      "sunday_end_hour": "",
      "created_at": "2022-05-17T13:08:10.000000Z",
      "updated_at": "2025-06-16T23:00:16.000000Z",
      "courier_map_points": [
        {
          "id": 2303,
          "map_point_id": 2303,
          "courier_id": 2,
          "courier_point_api_key": 913,
          "created_at": "",
          "updated_at": ""
        }
      ]
    },
    "volumetric_weight_divider": 5000,
    "billing_weight": 1,
    "price_net": 11,
    "price_gross": 13,
    "fees": {
      "base": {
        "value": 8
      },
      "extra_kg": {
        "value": 0
      },
      "minimum_wage": {
        "value": 0
      },
      "fuel_surcharge": {
        "value": 1
      }
    },
    "payment_amount": 13
  },
  "source": "external_api",
  "source_order_id": "",
  "created_at": "2025-06-17T07:46:16.000000Z",
  "imported_order_created_at": "2025-06-17T07:46:16.000000Z"
}
}';
        $decodedJson = json_decode($json, true, 12, JSON_THROW_ON_ERROR);
        $decodedJson = $decodedJson['order_to_send'];


        $parsedOrder = OrderResponseWrapper::wrap($decodedJson);
        $ecoletOrderResponse = Json::mapArrayToObject($parsedOrder, EcoletOrderResponse::class);
        $ecoletOrderCreatedResponse = Json::mapArrayToObject($parsedOrder, EcoletOrderCreatedResponse::class);

        $this->assertInstanceOf(EcoletOrderResponse::class, $ecoletOrderResponse);
        $this->assertInstanceOf(EcoletOrderCreatedResponse::class, $ecoletOrderCreatedResponse);
    }
}