<?php

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

use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Order\EcoletOrderResponse;
use Alsendo\AlsendoWrapper\Api\Ecolet\Model\Order\EcoletOrderValuation;
use Alsendo\AlsendoWrapper\Json;
use PHPUnit\Framework\TestCase;

class EcoletOrderValuationResponseTest extends TestCase
{
    public function testCreateOrderValuationResponseFromJson()
    {
        $json = '{
  "form" : {
    "statuses" : {
      "fan_marfa" : true,
      "dpd_standard" : true,
      "urgent_cargus_standard" : true,
      "fan_courier_standard" : true,
      "boo_kurier_bucuresti_24" : true,
      "sameday_nextday" : true,
      "boo_kurier_metropolitan" : true,
      "gls_standard" : true
    },
    "additional_services" : {
      "fan_marfa" : {
        "cod" : true,
        "rod" : false,
        "rop" : false,
        "open_package" : false,
        "saturday_delivery" : true,
        "sms_notify" : false,
        "swap" : false,
        "epod" : false
      },
      "dpd_standard" : {
        "cod" : true,
        "rod" : true,
        "rop" : false,
        "open_package" : true,
        "saturday_delivery" : false,
        "sms_notify" : false,
        "swap" : true,
        "epod" : false
      },
      "urgent_cargus_standard" : {
        "cod" : true,
        "rod" : true,
        "rop" : false,
        "open_package" : true,
        "saturday_delivery" : false,
        "sms_notify" : false,
        "swap" : true,
        "epod" : false
      },
      "fan_courier_standard" : {
        "cod" : true,
        "rod" : false,
        "rop" : false,
        "open_package" : true,
        "saturday_delivery" : true,
        "sms_notify" : false,
        "swap" : false,
        "epod" : false
      },
      "boo_kurier_bucuresti_24" : {
        "cod" : true,
        "rod" : true,
        "rop" : false,
        "open_package" : false,
        "saturday_delivery" : false,
        "sms_notify" : false,
        "swap" : false,
        "epod" : false
      },
      "sameday_nextday" : {
        "cod" : true,
        "rod" : true,
        "rop" : false,
        "open_package" : true,
        "saturday_delivery" : false,
        "sms_notify" : false,
        "swap" : true,
        "epod" : false
      },
      "boo_kurier_metropolitan" : {
        "cod" : true,
        "rod" : true,
        "rop" : false,
        "open_package" : false,
        "saturday_delivery" : false,
        "sms_notify" : false,
        "swap" : false,
        "epod" : false
      },
      "gls_standard" : {
        "cod" : true,
        "rod" : true,
        "rop" : false,
        "open_package" : false,
        "saturday_delivery" : false,
        "sms_notify" : false,
        "swap" : false,
        "epod" : false
      }
    },
    "conditions" : {
      "fan_marfa" : {
        "pickup_types" : [ "self", "courier" ]
      },
      "dpd_standard" : {
        "pickup_types" : [ "self", "courier" ]
      },
      "urgent_cargus_standard" : {
        "pickup_types" : [ "courier" ]
      },
      "fan_courier_standard" : {
        "pickup_types" : [ "self", "courier" ]
      },
      "boo_kurier_bucuresti_24" : {
        "pickup_types" : [ "courier" ]
      },
      "sameday_nextday" : {
        "pickup_types" : [ "self", "courier" ]
      },
      "boo_kurier_metropolitan" : {
        "pickup_types" : [ "courier" ]
      },
      "gls_standard" : {
        "pickup_types" : [ "courier" ]
      }
    },
    "pickup_dates" : {
      "fan_marfa" : [ {
        "day" : "Monday",
        "date" : "2025-04-07",
        "hours" : [ "12:00", "13:00", "14:00", "15:00" ]
      }, {
        "day" : "Tuesday",
        "date" : "2025-04-08",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00" ]
      }, {
        "day" : "Wednesday",
        "date" : "2025-04-09",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00" ]
      }, {
        "day" : "Thursday",
        "date" : "2025-04-10",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00" ]
      }, {
        "day" : "Friday",
        "date" : "2025-04-11",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00" ]
      } ],
      "dpd_standard" : [ {
        "day" : "Monday",
        "date" : "2025-04-07",
        "hours" : [ "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Tuesday",
        "date" : "2025-04-08",
        "hours" : [ "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Wednesday",
        "date" : "2025-04-09",
        "hours" : [ "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Thursday",
        "date" : "2025-04-10",
        "hours" : [ "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Friday",
        "date" : "2025-04-11",
        "hours" : [ "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      } ],
      "urgent_cargus_standard" : [ {
        "day" : "Monday",
        "date" : "2025-04-07",
        "hours" : [ "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Tuesday",
        "date" : "2025-04-08",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Wednesday",
        "date" : "2025-04-09",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Thursday",
        "date" : "2025-04-10",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Friday",
        "date" : "2025-04-11",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      } ],
      "fan_courier_standard" : [ {
        "day" : "Monday",
        "date" : "2025-04-07",
        "hours" : [ "12:00", "13:00", "14:00", "15:00", "16:00" ]
      }, {
        "day" : "Tuesday",
        "date" : "2025-04-08",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00" ]
      }, {
        "day" : "Wednesday",
        "date" : "2025-04-09",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00" ]
      }, {
        "day" : "Thursday",
        "date" : "2025-04-10",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00" ]
      }, {
        "day" : "Friday",
        "date" : "2025-04-11",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00" ]
      } ],
      "boo_kurier_bucuresti_24" : [ {
        "day" : "Monday",
        "date" : "2025-04-07",
        "hours" : [ "12:00", "13:00", "14:00", "15:00", "16:00" ]
      } ],
      "sameday_nextday" : [ {
        "day" : "Monday",
        "date" : "2025-04-07",
        "hours" : [ "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Tuesday",
        "date" : "2025-04-08",
        "hours" : [ "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Wednesday",
        "date" : "2025-04-09",
        "hours" : [ "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Thursday",
        "date" : "2025-04-10",
        "hours" : [ "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Friday",
        "date" : "2025-04-11",
        "hours" : [ "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      } ],
      "boo_kurier_metropolitan" : [ {
        "day" : "Monday",
        "date" : "2025-04-07",
        "hours" : [ "12:00", "13:00", "14:00", "15:00", "16:00" ]
      } ],
      "gls_standard" : [ {
        "day" : "Monday",
        "date" : "2025-04-07",
        "hours" : [ "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Tuesday",
        "date" : "2025-04-08",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Wednesday",
        "date" : "2025-04-09",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Thursday",
        "date" : "2025-04-10",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      }, {
        "day" : "Friday",
        "date" : "2025-04-11",
        "hours" : [ "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00" ]
      } ]
    },
    "billing_weight" : {
      "fan_marfa" : 10,
      "dpd_pallet_one" : 10,
      "dpd_standard" : 10,
      "urgent_cargus_standard" : 10,
      "fan_courier_standard" : 10,
      "classic_international_import_zone_1" : 10,
      "classic_international_import_zone_2" : 10,
      "classic_international_import_zone_3" : 10,
      "fan_courier_courier_to_locker" : 10,
      "fan_courier_locker_to_locker" : 10,
      "fan_courier_locker_to_courier" : 10,
      "boo_kurier_bucuresti_24" : 10,
      "sameday_locker" : 10,
      "sameday_nextday" : 10,
      "boo_kurier_metropolitan" : 10,
      "dpd_express_regional_zone_5" : 10,
      "sameday_locker_automatul_automatul" : 10,
      "gls_standard" : 10,
      "dpd_express_regional_czechia" : 10,
      "dpd_express_regional_bulgaria" : 10,
      "dpd_express_regional_hungary" : 10,
      "dpd_express_regional_greece" : 10,
      "dpd_classic_international_zone_1" : 10,
      "dpd_classic_international_zone_2" : 10,
      "dpd_classic_international_zone_3" : 10,
      "dpd_automat_locker_to_locker" : 10,
      "dpd_automat_locker_to_courier" : 10,
      "dpd_automat_courier_to_locker" : 10,
      "dpd_office_point_to_point" : 10,
      "easy_collect_locker_xl" : 10,
      "dpd_office_point_to_courier" : 10,
      "dpd_office_courier_to_point" : 10,
      "cargus_easy_collect" : 10,
      "international_area_1" : 10,
      "international_area_2" : 10,
      "international_area_3" : 10,
      "international_area_4" : 10,
      "international_area_5" : 10,
      "locker_curier_automat" : 10,
      "locker_automat_automat" : 10,
      "locker_automat_curier" : 10,
      "standard_zone_4" : 10,
      "standard_zone_2" : 10,
      "standard_zone_3" : 10,
      "express_worldwide_zone_1" : 10,
      "express_worldwide_zone_2" : 10,
      "express_worldwide_zone_3" : 10,
      "easy_collect_locker_s" : 10,
      "easy_collect_locker_m" : 10,
      "easy_collect_locker_l" : 10,
      "standard_30_plus" : 10,
      "ups_standard_zone_1" : 10,
      "ups_express_zone_1" : 10,
      "ups_express_zone_2" : 10,
      "ups_express_zone_3" : 10,
      "ups_express_zone_4" : 10,
      "standard_zone_1_multipack" : 10,
      "standard_zone_2_multipack" : 10,
      "standard_zone_3_multipack" : 10,
      "standard_zone_4_multipack" : 10,
      "posta_romana_standard" : 10
    },
    "is_standard" : {
      "fan_marfa" : true,
      "dpd_standard" : true,
      "urgent_cargus_standard" : true,
      "fan_courier_standard" : true,
      "boo_kurier_bucuresti_24" : true,
      "sameday_nextday" : true,
      "boo_kurier_metropolitan" : true,
      "gls_standard" : true
    },
    "prices_net" : {
      "fan_marfa" : 81.05,
      "dpd_standard" : 24.880000000000003,
      "urgent_cargus_standard" : 29.94,
      "fan_courier_standard" : 37.06,
      "boo_kurier_bucuresti_24" : 68.2,
      "sameday_nextday" : 34.47,
      "boo_kurier_metropolitan" : 69.2,
      "gls_standard" : 23.65
    },
    "prices_gross" : {
      "fan_marfa" : 96.45,
      "dpd_standard" : 29.61,
      "urgent_cargus_standard" : 35.63,
      "fan_courier_standard" : 44.1,
      "boo_kurier_bucuresti_24" : 81.16,
      "sameday_nextday" : 41.02,
      "boo_kurier_metropolitan" : 82.35,
      "gls_standard" : 28.14
    },
    "fees" : {
      "fan_marfa" : {
        "base" : {
          "value" : 69.36
        },
        "cod" : {
          "value" : 5
        },
        "fuel_surcharge" : {
          "value" : 6.69
        }
      },
      "dpd_standard" : {
        "base" : {
          "value" : 14.3
        },
        "extra_kg" : {
          "value" : 7
        },
        "minimum_wage" : {
          "value" : 0.91
        },
        "fuel_surcharge" : {
          "value" : 2.67
        }
      },
      "urgent_cargus_standard" : {
        "base" : {
          "value" : 15
        },
        "extra_kg" : {
          "value" : 10.8
        },
        "cod" : {
          "value" : 2.8
        },
        "fuel_surcharge" : {
          "value" : 1.34
        }
      },
      "fan_courier_standard" : {
        "base" : {
          "value" : 16.9
        },
        "extra_kg" : {
          "value" : 12.6
        },
        "cod" : {
          "value" : 4.5
        },
        "fuel_surcharge" : {
          "value" : 3.06
        }
      },
      "boo_kurier_bucuresti_24" : {
        "base" : {
          "value" : 9.8
        },
        "extra_kg" : {
          "value" : 58
        },
        "fuel_surcharge" : {
          "value" : 0.4
        }
      },
      "sameday_nextday" : {
        "base" : {
          "value" : 18.36
        },
        "extra_kg" : {
          "value" : 13.5
        },
        "cod" : {
          "value" : 2
        },
        "fuel_surcharge" : {
          "value" : 0.61
        }
      },
      "boo_kurier_metropolitan" : {
        "base" : {
          "value" : 10.8
        },
        "extra_kg" : {
          "value" : 58
        },
        "fuel_surcharge" : {
          "value" : 0.4
        }
      },
      "gls_standard" : {
        "extra_kg" : {
          "value" : 19.7
        },
        "cod" : {
          "value" : 2.5
        },
        "sms_notify" : {
          "value" : 0.75
        },
        "fuel_surcharge" : {
          "value" : 0.7
        }
      }
    },
    "payment_amount" : {
      "fan_marfa" : 96.45,
      "dpd_standard" : 29.61,
      "urgent_cargus_standard" : 35.63,
      "fan_courier_standard" : 44.1,
      "boo_kurier_bucuresti_24" : 81.16,
      "sameday_nextday" : 41.02,
      "boo_kurier_metropolitan" : 82.35,
      "gls_standard" : 28.14
    },
    "vat" : 19,
    "info" : {
      "fan_marfa" : [ ],
      "dpd_standard" : [ ],
      "urgent_cargus_standard" : [ ],
      "fan_courier_standard" : [ ],
      "boo_kurier_bucuresti_24" : [ ],
      "sameday_nextday" : [ ],
      "boo_kurier_metropolitan" : [ ],
      "gls_standard" : [ ]
    },
    "errors" : {
      "fan_marfa" : [ ],
      "dpd_standard" : [ ],
      "urgent_cargus_standard" : [ ],
      "fan_courier_standard" : [ ],
      "boo_kurier_bucuresti_24" : [ ],
      "sameday_nextday" : [ ],
      "boo_kurier_metropolitan" : [ ],
      "gls_standard" : [ ]
    }
  }
}';
        $decodedJson = json_decode($json, true, 12, JSON_THROW_ON_ERROR);

        $form = $decodedJson['form'];


        $slugs = array_keys($form['statuses'] ?? []);

        $orderValuations = [];

        foreach ($slugs as $slug) {
            $mappedData = [
                'service_id' => $slug,
                'status' => $form['statuses'][$slug] ?? false,
                'additional_services' => $form['additional_services'][$slug] ?? [],
                'pickup_dates' => $form['pickup_dates'][$slug] ?? [],
                'is_standard' => $form['is_standard'][$slug] ?? false,
                'price' => [
                    'net' => $form['prices_net'][$slug] ?? '0,00',
                    'gross' => $form['prices_gross'][$slug] ?? '0,00'
                ],
                'fees' => $form['fees'][$slug] ?? [],
                'vat' => $form['vat'] ?? null,
                'info' => $form['info'][$slug] ?? [],
                'errors' => $form['errors'][$slug] ?? [],
            ];
            $orderValuations[$slug] = Json::mapArrayToObject($mappedData, EcoletOrderValuation::class);
        }


        $this->assertIsArray($orderValuations);

        $this->assertInstanceOf(EcoletOrderValuation::class, reset($orderValuations));
    }
}