<?php

namespace App\Http\Controllers;

use App\Models\Education;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

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

    public function store(Request $request)
    {
        $education = $this->validateRequest($request);
        $education['start_date'] = Carbon::parse($education['start_date']);

        $education['end_date'] = Carbon::parse($education['end_date']);
        Education::create(array_merge(
            $education,
            [
                'engineer_id' => $request->user()->engineer->id,
            ]
        ));
        return back(303)->with(['success' => 'success']);

    }

    protected function validateRequest(Request $request)
    {
        return $request->validate([
            'school' => 'required',
            'degree' => 'required',
            'start_date' => 'required|date',
            'end_date' => 'required|date',
        ]);
    }

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

    /**
     * Display the specified resource.
     *
     * @param Education $education
     * @return Response
     */
    public function show(Education $education)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Education $education
     * @return Response
     */
    public function edit(Education $education)
    {
        //
    }

    public function update(Request $request)
    {
        $education = Education::find($request->id);

        $edu = $this->validateRequest($request);
        $edu['start_date'] = Carbon::parse($edu['start_date']);

        $edu['end_date'] = Carbon::parse($edu['end_date']);
        $education->update($edu);

        return back(303)->with(['success' => 'success']);

    }

    public function destroy(Request $request)
    {
        $education = Education::find($request->id);
        $education->delete();
        return back(303)->with(['success' => 'success']);
    }
}
