<?php

namespace App\Http\Controllers\User;

use App\DayOff;
use App\Models\AdditionalShift;
use App\Models\Bid;
use App\User;
use App\Spot;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;

class PSheetController extends Controller
{
    /**
     * Return 5 important arrays this arrays will be formatted before sending to the view.
     * A list of all shifts for the selected schedule is gather to create the tables by shift in the view
     * First all the bids are gather with the regular schedule and all the days off for officer. An array gets created
     * with the regular schedule menus days off.
     * Second all the RDOs are gather and the list of the additional shift. Then an array gets created with the days off
     * menus officers with additions shift.
     *
     * @param \http\Env\Request
     * @return view with the list for that specific date
    **/
    public function date(Request $request)
    {
        $dataValidated = $request->validate([
            'calendar_date' => ['required', 'date']
        ]);

        $dayofweek = date('w', strtotime($dataValidated['calendar_date']));

        $weekMap = [
            0 => 'sunday',
            1 => 'monday',
            2 => 'tuesday',
            3 => 'wednesday',
            4 => 'thursday',
            5 => 'friday',
            6 => 'saturday',
        ];
        $weekday = $weekMap[$dayofweek];
        $todayStart = $weekday.'_s';
        $todayEnd = $weekday.'_e';
        $shifts = array(); //Array that has the list of Shift of the date selected
        $bids = array(); //This is the array that will print regular schedule. The offices with dayOff have been remove from here
        $RDOs = array();  //RDO is Pinecrest identifier for an officer day off the schedule. the offices with additionals shift have been remove from here
        $daysOff = array(); //Array that host the users off of the date selected
        $additionalShifts = array(); //Array that host all the officers with additional shifts

        $dateSchedule = Bid::join('spots', 'bids.spot_id', '=', 'spots.id')
            ->join('shifts', 'spots.shift_id', '=', 'shifts.id')
            ->join('schedules', 'shifts.schedule_id', '=', 'schedules.id')
            ->where([['schedules.start_date', '<=', $dataValidated['calendar_date']], ['schedules.end_date', '>=', $dataValidated['calendar_date']], ['bids.approved', '=', 1]])
            ->get();

        $daysOff = DayOff::where('date', '=', $dataValidated['calendar_date'])->get();
        $additionalShifts = AdditionalShift::where('date', '=', $dataValidated['calendar_date'])->get();

        //Create the array with the regular bids schedule for the selected date
        $regularBids = array();
        $regularRDOs = array();
        foreach ($dateSchedule as $item){
            if ($item->spot->$todayStart != null && $item->spot->$todayEnd != null){
                array_push($regularBids, $item);
            }
            else{
                array_push($regularRDOs, $item);
            }
        }

        //Create the bids array with out the officers with days off
        foreach ($regularBids as $item){
            $found = false;
            foreach ($daysOff as $dayOff){
                if($item->user->id == $dayOff->user_id)
                    $found = true;
                    break;
            }
            if($found == false){
                array_push($bids, $item);
            }
        }

        //Create the RDOs array with out the officers with additional shifts
        foreach ($regularRDOs as $item){
            $found = false;
            foreach ($additionalShifts as $additionalShift){
                if($item->user->id == $additionalShift->user_id)
                    $found = true;
                break;
            }
            if($found == false){
                array_push($RDOs, $item);
            }
        }

        //Create the shift array
        foreach ($dateSchedule as $item){
            if(!in_array($item->spot->shift->name, $shifts)){
                array_push($shifts, $item->spot->shift->name);
            }
        }

        $spotsItemsLenght = sizeof($bids);

        if($spotsItemsLenght == 0){
            $user = Auth::user();
            if($user->hasAnyRoles(['root', 'admin'])){
                return view('user.psheet')->with([
                    'editable' => true,
                    'bids' => $bids,
                    'weekday' => $weekday,
                    'shifts' => $shifts,
                    'daySelected' => $dataValidated['calendar_date'],
                    'RDOs' => $RDOs,
                    'noSpots' => 'No schedule available for this date',
                    'daysOff' => $daysOff,
                    'additionalShifts' => $additionalShifts
                ]);
            }

            return view('user.psheet')->with([
                'bids' => $bids,
                'weekday' => $weekday,
                'shifts' => $shifts,
                'daySelected' => $dataValidated['calendar_date'],
                'RDOs' => $RDOs,
                'noSpots' => 'No schedule available for this date',
                'daysOff' => $daysOff,
                'additionalShifts' => $additionalShifts
            ]);
        }

        $user = Auth::user();
        if($user->hasAnyRoles(['root', 'admin'])){
            return view('user.psheet')->with([
                'editable' => true,
                'bids' => $bids,
                'weekday' => $weekday,
                'shifts' => $shifts,
                'daySelected' => $dataValidated['calendar_date'],
                'RDOs' => $RDOs,
                'daysOff' => $daysOff,
                'additionalShifts' => $additionalShifts
            ]);
        }

        return view('user.psheet')->with([
            'bids' => $bids,
            'weekday' => $weekday,
            'shifts' => $shifts,
            'daySelected' => $dataValidated['calendar_date'],
            'RDOs' => $RDOs,
            'daysOff' => $daysOff,
            'additionalShifts' => $additionalShifts
        ]);
    }


}
