<?php

namespace App\Http\Controllers;

use App\Models\OrdenLavanderia;
use App\Services\LaundryTrackingPresentationService;
use Illuminate\Http\JsonResponse;

/**
 * Seguimiento público sin datos personales identificables (solo folio, estados, presencia, envío agregado, últimos escaneos).
 */
class PublicTrackingController extends Controller
{
    public function __construct(
        private LaundryTrackingPresentationService $trackingPresentation
    ) {
    }

    public function show(string $folio): JsonResponse
    {
        $orden = \App\Support\LaundryScanResolver::findOrdenByFolio($folio);

        if ($orden === null) {
            return response()->json([
                'data' => null,
                'meta' => null,
                'errors' => ['folio' => ['Orden no encontrada.']],
                'message' => 'Orden no encontrada.',
            ], JsonResponse::HTTP_NOT_FOUND);
        }

        $data = $this->trackingPresentation->buildPublicDataArray($orden);

        return response()->json([
            'data' => $data,
            'meta' => null,
            'errors' => null,
        ], JsonResponse::HTTP_OK);
    }
}
