<?php

namespace App\Http\Controllers\Employee;

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

class EmployeeController extends Controller
{
    public function dashboard()
    {
        if(auth()->user()->role != 'employee'){
            abort(403);
        }

        $vehicles = Vehicle::where('status', 'available')
            ->get();

        $activeBookings = VehicleRequest::where(
            'user_id',
            auth()->id()
        )
        ->where('status', 'approved')
        ->count();

        $notifCount = VehicleRequest::where(
            'user_id',
            auth()->id()
        )
        ->whereIn('status', [
            'approved',
            'rejected',
            'returned'
        ])
        ->count();

        $recentActivities = VehicleRequest::with('vehicle')
            ->where('user_id', auth()->id())
            ->whereIn('status', [
                'approved',
                'rejected',
                'returned'
            ])
            ->latest()
            ->take(5)
            ->get();

        $myRequests = VehicleRequest::with('vehicle')
            ->where('user_id', auth()->id())
            ->latest()
            ->get();

        return view('employee.dashboard', compact(
            'vehicles',
            'activeBookings',
            'notifCount',
            'recentActivities',
            'myRequests'
        ));
    }

    public function requestForm($id)
    {
        if(auth()->user()->role != 'employee'){
            abort(403);
        }

        $vehicle = Vehicle::findOrFail($id);

        return view('employee.request-form',
            compact('vehicle'));
    }

    public function storeRequest(Request $request, $id)
    {
        if(auth()->user()->role != 'employee'){
            abort(403);
        }

        $conflict = VehicleRequest::where(
            'vehicle_id',
            $id
        )
        ->whereIn('status', [
            'pending',
            'approved',
            'in_use',
            'pending_return'
        ])
        ->where(function ($query) use ($request) {

            $query->whereBetween('start_date', [
                $request->start_date,
                $request->end_date
            ])

            ->orWhereBetween('end_date', [
                $request->start_date,
                $request->end_date
            ]);
        })
        ->exists();

        if ($conflict) {

            return back()->with(
                'error',
                'Vehicle already booked for selected dates.'
            );
        }

        $existingBooking = VehicleRequest::where(
    'vehicle_id',
    $request->vehicle_id
)
->whereIn('status', [
    'approved',
    'in_use',
    'pending_return'
])
->where(function ($query) use ($request) {

    $query->whereBetween(
        'start_date',
        [
            $request->start_date,
            $request->end_date
        ]
    )

    ->orWhereBetween(
        'end_date',
        [
            $request->start_date,
            $request->end_date
        ]
    );
})
->exists();

if ($existingBooking) {

    return back()->with(
        'error',
        'Vehicle already booked for selected schedule.'
    );
}


        VehicleRequest::create([
            'user_id' => auth()->id(),
            'vehicle_id' => $id,
            'start_date' => $request->start_date,
            'end_date' => $request->end_date,

            'origin' => $request->origin,
            'destination' => $request->destination,
            'route_notes' => $request->route_notes,

            'purpose' => $request->purpose,
            'status' => 'pending'
    ]);

        return redirect('/employee/dashboard')
            ->with(
                'success',
                'Vehicle request submitted.'
            );
    }

    public function myRequests()
    {
        if(auth()->user()->role != 'employee'){
            abort(403);
        }

        $requests = VehicleRequest::with('vehicle')
            ->where('user_id', auth()->id())
            ->get();

        return view('employee.requests',
            compact('requests'));
    }
}