<?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)
    {
        $data = $request->validate([
            'course_id'     => ['required', 'integer', 'exists:courses,id'],
            'semester_code' => ['required', 'size:3', 'exists:semesters,code'],
            'section_count' => ['required', 'integer', 'max:50'],
        ]);

        // TODO: Check if section code is taken
        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()->back();
    }

    /**
     * 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'],
            'credits'           => ['nullable', 'numeric'],
            'student_count'     => ['nullable', 'integer'],
        ]));
        $section->update($data);

        // TODO: Insert syllabus
        if ($request->file('syllabus')) {
            $path = $request->file('syllabus')->store('section_syllabus');
            $section->update(['syllabus' => '/storage/'.$path]);
        }

        if (isset($data['professor_id'])) {
            $section->professors()->detach();
            foreach($data['professor_id'] as $professor_id) {
                $section->professors()->attach($professor_id);
            }
        }
        return redirect()->back();
    }

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