<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Purchase;

class PurchaseTrackingController extends Controller
{
    /**
     * GET /api/purchases/{purchase}/tracking
     */
    public function index(Purchase $purchase)
    {
        $trackings = $purchase->trackings()->with('actor:id,name')->get();

        return response()->json($trackings->map(fn($t) => [
            'id'              => $t->id,
            'status'          => $t->status,
            'description'     => $t->description,
            'actor_name'      => $t->actor?->name ?? 'System',
            'actor_role'      => $t->actor_role,
            'tracking_number' => $t->tracking_number,
            'created_at'      => $t->created_at->toISOString(),
        ]));
    }
}
