@php
    /** @var \App\Models\Order $order */

    $items = [];
    $totalValue = (float) $order->total;

    foreach ($order->items as $row) {
        $price = $row->product_price ?? 0;
        $qty   = $row->qty ?? 1;

        $items[] = [
            'item_id'       => (string) $row->product_id,
            'item_name'     => (string) ($row->product_name ?? $row->product->name ?? 'Product'),
            'affiliation'   => config('app.name', 'MyCommerce'),
            'item_brand'    => config('app.name', 'MyCommerce'),
            'item_category' => (string) ($row->product->category->name ?? ''),
            'price'         => (float) $price,
            'quantity'      => (int) $qty,
        ];
    }

    $shipping = (float) ($order->delivery_charge ?? 0);
    $tax      = 0; // jodi tax column thake, sekhan theke nao
@endphp

<script>
    window.dataLayer = window.dataLayer || [];

    function pushEcomEvent(eventName, ecommerceData) {
        window.dataLayer.push({ ecommerce: null });
        window.dataLayer.push({
            event: eventName,
            ecommerce: ecommerceData
        });
    }

    (function () {
        var ecommerceData = {
            transaction_id: "{{ $order->id }}",
            value: {{ $totalValue }},
            currency: "BDT",
            shipping: {{ $shipping }},
            tax: {{ $tax }},
            coupon: "",
            customer_type: "{{ auth()->check() ? 'logged_in' : 'guest' }}",
            items: @json($items)
        };

        pushEcomEvent('purchase', ecommerceData);
    })();
</script>
