<?php

namespace App\Http\Controllers;

use App\Models\Shipment;
use App\Models\TrackingUpdate;
use Illuminate\Http\Request;

class TrackingUpdateController extends Controller
{
    public function create(Shipment $shipment)
    {
        return view('admin.tracking-updates.create', compact('shipment'));
    }

    public function store(Request $request, Shipment $shipment)
    {
        $validated = $request->validate([
            'location' => 'required|string|max:255',
            'status' => 'required|string|in:pending,in_transit,delivered',
            'latitude' => 'required|numeric',
            'longitude' => 'required|numeric',
            'description' => 'required|string',
        ]);

        $shipment->trackingUpdates()->create($validated);

        // Update shipment status and current location
        $shipment->update([
            'status' => $validated['status'],
            'current_location' => $validated['location']
        ]);

        return redirect()->route('admin.shipments.show', $shipment)
            ->with('success', 'Tracking update added successfully.');
    }
}
