<?php

namespace Core\App\Support;

final class OrderPricing
{
    public static function extra(array $order): array
    {
        $extra = $order['extra'] ?? [];
        if (is_string($extra)) {
            $extra = json_decode($extra, true) ?: [];
        }
        return is_array($extra) ? $extra : [];
    }

    public static function pricing(array $order): array
    {
        $extra = self::extra($order);
        $pricing = is_array($extra['pricing'] ?? null) ? $extra['pricing'] : [];
        $shippingPrice = isset($pricing['shipping_price']) ? (float)$pricing['shipping_price'] : 0.0;
        $effectiveTotal = self::effectiveTotal($order);

        if (array_key_exists('negotiated_price', $pricing) || ($effectiveTotal > 0 && abs($effectiveTotal - (float)($pricing['total'] ?? 0)) > 0.009)) {
            $pricing['total'] = $effectiveTotal;
            $pricing['products_total'] = max(0, $effectiveTotal - $shippingPrice);
        } else {
            $pricing['total'] = isset($pricing['total']) ? (float)$pricing['total'] : (float)($order['total_amount'] ?? 0);
            $pricing['products_total'] = isset($pricing['products_total']) ? (float)$pricing['products_total'] : max(0, $pricing['total'] - $shippingPrice);
        }

        $pricing['shipping_price'] = $shippingPrice;
        return $pricing;
    }

    public static function effectiveTotal(array $order): float
    {
        $extra = self::extra($order);
        $pricing = is_array($extra['pricing'] ?? null) ? $extra['pricing'] : [];

        if (isset($pricing['negotiated_price']) && $pricing['negotiated_price'] !== '' && $pricing['negotiated_price'] !== null) {
            return (float)$pricing['negotiated_price'];
        }
        if (isset($order['total_amount']) && $order['total_amount'] !== '' && $order['total_amount'] !== null) {
            return (float)$order['total_amount'];
        }
        if (isset($pricing['total']) && $pricing['total'] !== '' && $pricing['total'] !== null) {
            return (float)$pricing['total'];
        }
        if (isset($order['total']) && $order['total'] !== '' && $order['total'] !== null) {
            return (float)$order['total'];
        }
        return 0.0;
    }

    public static function applyNegotiatedTotal(array $order, array $extra, float $newTotal, string $note = ''): array
    {
        $pricing = is_array($extra['pricing'] ?? null) ? $extra['pricing'] : [];
        $shippingPrice = isset($pricing['shipping_price']) ? (float)$pricing['shipping_price'] : 0.0;
        $originalTotal = self::effectiveTotal($order);

        $pricing['negotiated_price'] = $newTotal;
        $pricing['total'] = $newTotal;
        $pricing['products_total'] = max(0, $newTotal - $shippingPrice);
        $pricing['negotiated_note'] = trim($note) !== '' ? trim($note) : ('Price changed at shipment from ' . $originalTotal . ' to ' . $newTotal);
        $pricing['negotiated_at'] = date('Y-m-d H:i:s');
        $extra['pricing'] = $pricing;
        return $extra;
    }
}
