<?php

namespace App\Http\Controllers;

use App\Models\Truck;
use App\Models\Company;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use App\Services\ActionNotificationService;

class TruckController extends Controller
{
    // Middleware applied in routes


    /**
     * Display a listing of trucks.
     */
    public function index(): View
    {
        $trucks = Truck::where('company_id', auth()->user()->company_id)->get();
        return view('trucks.index', compact('trucks'));
    }

    /**
     * Show create form.
     */
    public function create(): View
    {
        return view('trucks.create');
    }

    /**
     * Store new truck.
     */
    public function store(Request $request, ActionNotificationService $notifications): RedirectResponse
    {
        $validated = $request->validate([
            'number' => 'required|string|max:50',
            'name' => 'required|string|max:100',
            'equipment_number' => 'required|string|max:50',
            'fuel_cost_per_mile' => 'required|numeric|min:0',
        ]);

        $truck = Truck::create(array_merge($validated, [
            'company_id' => auth()->user()->company_id,
        ]));

        $notifications->notifyAction(
            'Truck Created',
            auth()->user(),
            [
                'channel' => 'web',
                'company_id' => $truck->company_id,
                'truck_number' => $truck->number,
                'truck_name' => $truck->name,
            ]
        );

        return redirect()->route('owner.trucks.index')->with('success', 'Truck added successfully!');
    }

    /**
     * Show edit form.
     */
    public function edit(Truck $truck): View
    {
        if ($truck->company_id !== auth()->user()->company_id) {
            abort(403);
        }

        return view('trucks.edit', compact('truck'));
    }

    /**
     * Update truck.
     */
    public function update(Request $request, Truck $truck, ActionNotificationService $notifications): RedirectResponse
    {
        if ($truck->company_id !== auth()->user()->company_id) {
            abort(403);
        }

        $validated = $request->validate([
            'number' => 'required|string|max:50',
            'name' => 'required|string|max:100',
            'equipment_number' => 'required|string|max:50',
            'fuel_cost_per_mile' => 'required|numeric|min:0',
        ]);

        $truck->update($validated);

        $notifications->notifyAction(
            'Truck Updated',
            auth()->user(),
            [
                'channel' => 'web',
                'company_id' => $truck->company_id,
                'truck_number' => $truck->number,
                'truck_name' => $truck->name,
            ]
        );

        return redirect()->route('owner.trucks.index')->with('success', 'Truck updated successfully!');
    }

    /**
     * Delete truck.
     */
    public function destroy(Truck $truck, ActionNotificationService $notifications): RedirectResponse
    {
        if ($truck->company_id !== auth()->user()->company_id) {
            abort(403);
        }

        $truckNumber = $truck->number;
        $truckName = $truck->name;
        $companyId = $truck->company_id;

        $truck->delete();

        $notifications->notifyAction(
            'Truck Deleted',
            auth()->user(),
            [
                'channel' => 'web',
                'company_id' => $companyId,
                'truck_number' => $truckNumber,
                'truck_name' => $truckName,
            ]
        );

        return redirect()->route('owner.trucks.index')->with('success', 'Truck deleted successfully!');
    }
}

