<?php

namespace App\Http\Controllers\Security;

use App\Http\Controllers\Controller;
use App\Models\TripLog;
use App\Models\VehicleRequest;
use App\Models\Vehicle;
use App\Models\MaintenanceRecord;
use Illuminate\Http\Request;
use App\Models\FuelLog;

class SecurityController extends Controller
{
    public function startTrip($id)
{
    $request = VehicleRequest::findOrFail($id);
    $activeTrip = TripLog::where('vehicle_id', $request->vehicle_id)
    ->whereNull('return_time')
    ->exists();

    if ($activeTrip) {
    return back()->with(
        'error',
        'Vehicle is already being used.'
    );
}

    TripLog::create([
        'user_id' => $request->user_id,
        'vehicle_id' => $request->vehicle_id,
        'departure_time' => now()
    ]);

    Vehicle::where('id', $request->vehicle_id)
    ->update([
        'status' => 'in_use'
    ]);

    $request->update([
    'status' => 'in_use'
    ]);

    return back();
}

    public function endTrip($id)
{
    $request = VehicleRequest::with('vehicle', 'user')
        ->findOrFail($id);

    return view('security.end-trip', compact('request'));
}

    public function submitEndTrip(Request $request, $id)
{
    $request->validate([
        'fuel_used' => 'required|numeric|min:0'
    ]);
    $tripRequest = VehicleRequest::findOrFail($id);

    $log = TripLog::where('vehicle_id', $tripRequest->vehicle_id)
        ->whereNull('return_time')
        ->latest()
        ->first();

    if ($log) {

        $log->update([
            'return_time' => now()
        ]);

        FuelLog::create([
            'trip_id' => $log->id,
            'fuel_used' => $request->fuel_used
        ]);

        $tripRequest->update([
            'status' => 'pending_return'
        ]);

        \App\Models\Notification::create([
            'user_id' => $tripRequest->user_id,
            'message' => 'Trip ended. Waiting for return approval.',
        'is_read' => false
        ]);

        Vehicle::where('id', $tripRequest->vehicle_id)
            ->update([
                'status' => 'pending_return'
            ]);
    }

    return redirect('/security/pending-returns');
}

    public function pendingReturns()
    {
        $requests = VehicleRequest::with('vehicle', 'user')
            ->where('status', 'pending_return')
            ->latest()
            ->get();

        return view('security.pending-returns', compact('requests'));
    }

    public function approveReturn($id)
    {
        $request = VehicleRequest::findOrFail($id);

        $request->update([
            'status' => 'returned'
        ]);

        \App\Models\Notification::create([
            'user_id' => $request->user_id,
            'message' => 'Your vehicle return has been approved.',
            'is_read' => false
        ]);

        Vehicle::where('id', $request->vehicle_id)
            ->update([
                'status' => 'available'
            ]);

        return back();
    }

    public function sendMaintenance($id)
    {
        $request = VehicleRequest::findOrFail($id);

        $request->update([
            'status' => 'maintenance'
        ]);

        \App\Models\Notification::create([
            'user_id' => $request->user_id,
            'message' => 'Vehicle sent to maintenance.',
            'is_read' => false
        ]);

        Vehicle::where('id', $request->vehicle_id)
            ->update([
                'status' => 'maintenance'
            ]);

        MaintenanceRecord::create([
            'vehicle_id' => $request->vehicle_id,
            'service_type' => 'Post-trip inspection',
            'service_date' => now(),
            'notes' => 'Ditemukan kerusakan kendarran saat inspeksi kendaraan'
        ]);

        return back();
    }

    public function tripLog()
{
    $requests = VehicleRequest::with('vehicle', 'user')
        ->where('status', 'in_use')
        ->orWhere('status', 'approved')
        ->get();

    $activeTrips = TripLog::whereNull('return_time')
        ->pluck('vehicle_id')
        ->toArray();

    $logs = TripLog::with('vehicle', 'user', 'fuelLogs')
        ->latest()
        ->get();

    return view('security.triplog', compact(
        'requests',
        'logs',
        'activeTrips'
    ));
}
}