<?php

namespace App\Http\Controllers;

use App\Course;
use App\Semester;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Storage;

class CourseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('courses.index', [
            'courses' => Course::where('dept_id', '=', 1)->with('sections:id,course_id,semester_code')->get()->sort('cmpCourseCode'),
            'semesters' => Semester::orderBy('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)
    {
        // dd($request->validate(['code'      => ['required','unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string']]));
        $course = Course::create( $request->validate([
            'code'      => ['required','unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
            'title'     => ['required','max:255', 'string'],
            'dept_id'   => ['required','exists:departments,id', 'integer'],
            'syllabus'  => ['nullable','file', 'mimes:pdf']
        ]));

        if ($request->file('syllabus')) {
            $path = $request->file('syllabus')->store('course_syllabus', 'public');
            $course->update(['syllabus' => asset($path)]);
        }

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

    /**
     * Display the specified resource.
     *
     * @param  \App\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function show(Course $course)
    {
        // dd($course->toJson());
        // dump(1);
        // Auth::logout();
        // $semesters = $course->semesters->unique()->loadMissing('sections');
        // $course->loadMissing('semesters.sections');
        // dd($semesters);
        $course->loadMissing(['semesters.sections.professors']);
        // dd($course->semesters->unique());
        // $semesters = $course->sections->groupBy('semester_code')->map( function ($sections) {
        //     return collect(['alpha' => $sections[0]->semester->alpha, 'sections' => $sections->sortBy('code')]);
        // })->sort();
        // dd($semesters);
        // dd($course->loadMissing(['sections.semester']));
        // dd($course->sections->groupBy('semester_code'));
        return view('courses.show', [
            'course' => $course,
            // 'semesters' => $semesters
        ]);
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Course $course)
    {

        $course->update( $request->validate([
            'code'      => ['unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
            'title'     => ['max:255', 'string'],
            'dept_id'   => ['exists:departments,id', 'integer'],
            'syllabus'  => ['nullable', 'file', 'mimetypes:application/pdf'],
        ]));

        if ($request->file('syllabus')) {
            $path = $request->file('syllabus')->store('course_syllabus');
            $course->update(['syllabus' => asset($path)]);
        }
        dump(asset($path));
        // dd($course->syllabus);
        return redirect($course->syllabus);
        return redirect('/course');
    }

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