<?php

namespace App\Http\Controllers;

use App\Professor;
use App\Section;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SectionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * 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)
    {
        $request = $request->validate([
            'course_id'     => ['required', 'integer', 'exists:courses,id'],
            'semester_code' => ['required', 'size:3', 'exists:semesters,code'],
            'section_count' => ['required', 'integer', 'max:50'],
        ]);
        for ($i=1; $i <= $request->section_count; $i++) {
            Section::create([
                'course_id'     => $request->course_id,
                'semester_code' => $request->semester_code,
                'code'          => str_pad($i, 3, '0', STR_PAD_LEFT)
            ]);
        }
        return redirect('/course');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function show(Section $section)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Section  $section
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Section $section)
    {
        // dd($request);
        $data = array_filter($request->validate([
            'code'              => ['nullable', 'size:3', 'string'],
            'professor_id'      => ['nullable', 'array'],
            'professor_id.*'    => ['exists:professors,id'],
            'syllabus'          => ['nullable', 'file', 'mimes:pdf'],
            'credits'           => ['nullable', 'numeric'],
            'student_count'     => ['nullable', 'integer'],
        ]));
        // TODO: Insert syllabus

        $section->update($data);
        foreach($data['professor_id'] as $professor_id) {
            $section->professors()->attach($professor_id);
        }
        // return 1;
        // return $request;
        // return next($request);
        return redirect()->back();
    }

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