<?php

namespace App\Http\Controllers\Api\GoPoints;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Support\Carbon;

class StatusController extends Controller
{
    public function show($campaign, $providerOrderId)
    {
        // 1) Validación de campaña
        if (strtolower($campaign) !== 'gopoints') {
            return response()->json([
                'Status'     => 400,
                'Message'    => 'Campaña no reconocida',
                'CodeError'  => 'INVALID_CAMPAIGN',
                'Exception'  => null,
                'Parameters' => [
                    'Campaign'         => $campaign,
                    'ProviderOrderId'  => $providerOrderId,
                ],
            ], 400, [], JSON_UNESCAPED_UNICODE);
        }

        // 2) Buscar orden por ProviderOrderId
        $order = Order::where('order_id', $providerOrderId)->first();

        if (!$order) {
            return response()->json([
                'Status'     => 404,
                'Message'    => 'Order not found',
                'CodeError'  => 'ORDER_NOT_FOUND',
                'Exception'  => null,
                'Parameters' => [
                    'Campaign'         => 'GoPoints',
                    'ProviderOrderId'  => $providerOrderId,
                ],
                'Order'      => [
                    'ProviderOrderId' => (string) $providerOrderId,
                    'Shipments'       => [],
                ],
            ], 404, [], JSON_UNESCAPED_UNICODE);
        }

        // 3) Productos y mapeo de estado de envío
        $products = json_decode($order->products ?? '[]', true);

        $shipmentStatusId = match (strtoupper((string) $order->status)) {
            'CREATED'    => 1,
            'PROCESSING' => 2,
            'SHIPPED'    => 3,
            'CONFIRMED'  => 4,
            'CANCELLED'  => 5,
            default      => 0,
        };

        $statusDescription = match ($shipmentStatusId) {
            1 => 'Order created successfully',
            2 => 'Order being processed',
            3 => 'Order shipped',
            4 => 'Order confirmed and ready for delivery',
            5 => 'Order cancelled by customer',
            default => 'Unknown status',
        };

        // 4) Fecha en zona horaria America/Santiago, en ISO-8601
        $updateDate = Carbon::parse($order->updated_at)
            ->setTimezone('America/Santiago')
            ->toIso8601String();

        // 5) Respuesta final conforme al manual (Message vacío en éxito)
        return response()->json([
            'Status'     => 200,
            'Message'    => '',
            'CodeError'  => '',
            'Exception'  => null,
            'Parameters' => [
                'Campaign'         => 'GoPoints',
                'ProviderOrderId'  => (string) $providerOrderId,
            ],
            'Order'      => [
                'ProviderOrderId' => (string) $order->order_id,
                'Shipments'       => [
                    [
                        'StatusId'    => $shipmentStatusId,
                        'Description' => $statusDescription,
                        'UpdateDate'  => $updateDate,
                        'Products'    => $products,
                    ],
                ],
            ],
        ], 200, [], JSON_UNESCAPED_UNICODE);
    }
}
