<?php

namespace App\Http\Controllers;

use App\Models\Load;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class LoadsController extends Controller
{
    public function index(Request $request): View
    {
        $user = Auth::user();
        $baseQuery = Load::where('company_id', $user->company_id)->with(['truck', 'driver']);

        $loads = (clone $baseQuery)->latest()->paginate(10);
        $summary = [
            'load_count' => (clone $baseQuery)->count(),
            'total_miles' => (clone $baseQuery)->sum('miles'),
            'total_revenue' => (clone $baseQuery)->sum('gross_rate'),
            'total_profit' => (clone $baseQuery)->sum('net_profit'),
        ];

        return view('loads.index', compact('loads', 'summary'));
    }
}

