<?php

namespace App\Http\Controllers;

use App\Professor;
use App\Semester;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class ProfessorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // dd(Professor::where('dept_id', '=', 1)->with(['sections:semester_code,credits,student_count', 'semesters:semester_code,admin_load,investigative_load'])->orderBy('professors.last_name')->get());
        return view('professors.index', [
            'professors' => Professor::where('dept_id', '=', 1)->with(['sections:semester_code,credits,student_count', 'semesters:semester_code,admin_load,investigative_load'])->orderBy('professors.last_name')->get(),
            'semesters' => Semester::orderBy('semesters.code', 'asc')->get(),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $professor = Professor::create( $request->validate([
            'num_prof'      => ['required', 'unique:professors', 'max:16', 'string'],
            'first_name'    => ['required', 'max:255', 'string', 'alpha'],
            'last_name'     => ['required', 'max:255', 'string', 'alpha'],
            'email'         => ['nullable', 'email'],
            'type'          => ['nullable', 'in:plantilla,contrato,ta'],
            'dept_id'       => ['required', 'exists:departments,id', 'integer'],
        ]));
        // dd($professor);

        return redirect('/professor/' . $professor->id);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Professor  $professor
     * @return \Illuminate\Http\Response
     */
    public function show(Professor $professor)
    {
        $professor->loadMissing(['sections.course', 'semesters.sections.course']);
        $loads = $professor->semesters()->get()->keyBy('code');
        $sections = $professor->sections()->with('course:id,code')->get()->groupBy('semester_code');
        $semesters = Semester::whereIn('code', $loads->keys())->orWhereIn('code', $sections->keys())->get();

        return view('professors.show', compact([
            'professor',
            'semesters',
            'sections',
            'loads'
        ]));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Professor  $professor
     * @return \Illuminate\Http\Response
     */
    public function edit(Professor $professor)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Professor  $professor
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Professor $professor)
    {
        // TODO: Add logic for updating professor
        if ($request->isMethod('put')) {
            $data = array_filter($request->validate([
                'semester_code'         => ['required_with:admin_load,investigative_load', 'string','size:3'],
                'admin_load'            => ['nullable', 'numeric'],
                'investigative_load'    => ['nullable', 'numeric']
            ]));
            // dd($data);
            if (! $professor->semesters->contains($data['semester_code'])) {
                $professor->semesters()->attach($data['semester_code'], $data);
            } else {
                $professor->semesters()->updateExistingPivot($data['semester_code'], $data);
            }
        }
        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Professor  $professor
     * @return \Illuminate\Http\Response
     */
    public function destroy(Professor $professor)
    {
        //
    }
}
