<?php
namespace Core\App\Support;

class ShippingSettings
{
    public static function defaultData(): array
    {
        return [
            'mode'            => 'flat',
            'flat_price'      => null,   // null = unavailable; 0 = free; number = price
            'flat_home_price' => null,
            'flat_desk_price' => null,
            'states'          => [],
            'zones'           => [],
            'provider_price_modes' => [],
            'provider_imported_prices' => [],
            'provider_imported_meta' => [],
            'pricing_provider' => '',
            'pricing_source' => 'manual',
            'pricing_fallback_manual' => 1,
            'shipping_unavailable_text' => '',
            'provider_selection_mode' => 'manual_admin',
            'forced_provider' => '',
            'allow_customer_provider_choice' => 0,
            'frontend_maystro_enabled' => 0,
            'frontend_maystro_allow_express' => 1,
            'frontend_maystro_allow_stopdesk' => 1,
            'frontend_maystro_allow_pickup' => 1,
            'origin' => [
                'state_code' => '',
                'state_name' => '',
                'state_ar_name' => '',
                'city_id' => '',
                'city_name' => '',
                'city_ar_name' => '',
            ],
        ];
    }

    public static function load(): array
    {
        $file = ROOT_PATH . '/storage/settings/shipping.json';
        if (!is_file($file)) {
            return self::defaultData();
        }

        $data = json_decode((string) file_get_contents($file), true);
        if (!is_array($data)) {
            return self::defaultData();
        }

        $defaults = self::defaultData();
        $data['origin'] = array_merge($defaults['origin'], (array) ($data['origin'] ?? []));
        $data['provider_price_modes'] = is_array($data['provider_price_modes'] ?? null) ? $data['provider_price_modes'] : [];
        $data['provider_imported_prices'] = is_array($data['provider_imported_prices'] ?? null) ? $data['provider_imported_prices'] : [];
        $data['provider_imported_meta'] = is_array($data['provider_imported_meta'] ?? null) ? $data['provider_imported_meta'] : [];
        $data['pricing_provider'] = is_string($data['pricing_provider'] ?? null) ? trim((string)$data['pricing_provider']) : '';
        $data['pricing_source'] = in_array($data['pricing_source'] ?? '', ['manual','api','reference'], true) ? (string)$data['pricing_source'] : 'manual';
        $data['pricing_fallback_manual'] = !empty($data['pricing_fallback_manual']) ? 1 : 0;
        $data['shipping_unavailable_text'] = is_string($data['shipping_unavailable_text'] ?? null) ? trim((string)$data['shipping_unavailable_text']) : '';
        $data['provider_selection_mode'] = in_array($data['provider_selection_mode'] ?? '', ['manual_admin','forced_provider','customer_choice'], true) ? (string)$data['provider_selection_mode'] : 'manual_admin';
        $data['forced_provider'] = is_string($data['forced_provider'] ?? null) ? trim((string)$data['forced_provider']) : '';
        $data['allow_customer_provider_choice'] = !empty($data['allow_customer_provider_choice']) ? 1 : 0;
        if (!$data['allow_customer_provider_choice']) {
            $data['provider_selection_mode'] = 'manual_admin';
            $data['forced_provider'] = '';
        }
        $data['frontend_maystro_enabled'] = !empty($data['frontend_maystro_enabled']) ? 1 : 0;
        $data['frontend_maystro_allow_express'] = !empty($data['frontend_maystro_allow_express']) ? 1 : 0;
        $data['frontend_maystro_allow_stopdesk'] = !empty($data['frontend_maystro_allow_stopdesk']) ? 1 : 0;
        $data['frontend_maystro_allow_pickup'] = !empty($data['frontend_maystro_allow_pickup']) ? 1 : 0;
        return array_merge($defaults, $data);
    }
}
