<?php

namespace App\Http\Controllers\Supervisor;

use Carbon\Carbon;
use App\Models\Trip;
use App\Constants\Status;
use Illuminate\Http\Request;
use App\Rules\FileTypeValidate;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class SupervisorController extends Controller
{
    public function dashboard()
    {
        return to_route('supervisor.trips');
    }

    public function trips()
    {
        if(!request()->filled('date')) {
            request()->merge(['date' => date('F d, Y').' - '.date('F d, Y')]);
        }
        $supervisor = authUser('supervisor');
        $trips = $supervisor->trips()
            ->dateFilter('start_time')
            ->with('supervisor', 'vehicle', 'vehicle.fleetType')
            ->orderBy('start_time', 'asc')
            ->paginate(getPaginate());

        $pageTitle = 'Assigned Trips';
        return view('supervisor.trips', compact('pageTitle', 'trips', 'supervisor'));
    }

    public function viewTrips($id)
    {
        $supervisor = authUser('supervisor');
        $trip = Trip::with(['route', 'fleetType', 'bookedTickets', 'bookedTickets.user', 'bookedTickets.guestUser', 'bookedTickets.pickup', 'bookedTickets.drop'])->findOrFail($id);

        $pageTitle = $trip->title;
        $stoppages = $trip->route->stoppages;
        return view('supervisor.trip_view', compact('pageTitle', 'trip', 'stoppages'));
    }

    public function tripStatus($id) {
        $trip         = Trip::findOrFail($id);
        if(!$trip->vehicle_id) {
           $notify[] = ['error', 'You cannot change the status without assigning a vehicle.'];
            return back()->withNotify($notify);
        }


        $trip->status = request('status');
        if (request()->filled('end_time') == Status::TRIP_COMPLETED) {
            $trip->end_time = showDateTime(request('end_time'), 'Y-m-d H:i:s');
        }
        if (request()->filled('running_time') == Status::TRIP_COMPLETED) {
            $trip->running_time = showDateTime(request('running_time'), 'Y-m-d H:i:s');
        }
        $trip->save();
        $notify[] = ['success', 'Status changed successfully'];
        return back()->withNotify($notify);
    }


    public function profile()
    {
        $pageTitle = 'Profile';
        $supervisor = authUser('supervisor');
        return view('supervisor.profile', compact('pageTitle', 'supervisor'));
    }

    public function profileUpdate(Request $request)
    {
        $request->validate([
            'firstname' => 'required',
            'lastname'  => 'required',
            'address'   => 'nullable|string',
            'state'     => 'nullable|string',
            'zip'       => 'nullable|string',
            'city'      => 'nullable|string',
            'image'     => ['nullable', 'image', new FileTypeValidate(['jpg', 'jpeg', 'png'])]
        ]);

        $supervisor = authUser('supervisor');
        if ($request->hasFile('image')) {
            try {
                $old = $supervisor->image;
                $supervisor->image = fileUploader($request->image, getFilePath('supervisor'), getFileSize('supervisor'), $old);
            } catch (\Exception $exp) {
                $notify[] = ['error', 'Couldn\'t upload your image'];
                return back()->withNotify($notify);
            }
        }

        $supervisor->firstname = $request->firstname;
        $supervisor->lastname  = $request->lastname;
        $supervisor->address   = $request->address;
        $supervisor->state     = $request->state;
        $supervisor->zip       = $request->zip;
        $supervisor->city      = $request->city;
        $supervisor->save();

        $notify[] = ['success', 'Profile updated successfully'];
        return back()->withNotify($notify);
    }

    public function password()
    {
        $pageTitle = 'Password Setting';
        $supervisor = authUser('supervisor');
        return view('supervisor.password', compact('pageTitle', 'supervisor'));
    }

    public function passwordUpdate(Request $request)
    {
        $request->validate([
            'old_password' => 'required',
            'password' => 'required|min:6|confirmed',
        ]);

        $supervisor = authUser('supervisor');
        if (!Hash::check($request->old_password, $supervisor->password)) {
            $notify[] = ['error', 'Password doesn\'t match!!'];
            return back()->withNotify($notify);
        }
        $supervisor->password = Hash::make($request->password);
        $supervisor->save();
        $notify[] = ['success', 'Password changed successfully.'];
        return back()->withNotify($notify);
    }

    public function banned()
    {
        $supervisor = authUser('supervisor');
        if ($supervisor->status) {
            return to_route('supervisor.dashboard');
        }
        Auth::guard('supervisor')->logout();
        return to_route('supervisor.login');
    }
}
