<?php

namespace App\Http\Controllers;

use App\Models\Skill;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Session;

class SkillController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $skills = Skill::all();
            //->where('verified',true);
        return response()->json(['skills' => $skills]) ;
    }


    public function store(Request $request)
    {
    //    dd($request->all());
        $skill= Skill::where('name_en', $request->name_en)->first();

        if($skill!=null)
           {
             return response()->json(['skill' => $skill]);
            }
        else $skill = Skill::create($this->validateSkillRequest($request));

        return response()->json(['skill' => $skill]);
    }

    /**
     * @param Request $request
     * @return array
     */
    public function validateSkillRequest(Request $request)
    {
        return $request->validate([
            'name_en' => 'required',
            'name_ar' => 'required',
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param Skill $skill
     * @return Response
     */
    public function show(Skill $skill)
    {
        return Skill::find($skill);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param Skill $skill
     * @return RedirectResponse
     */
    public function update(Request $request, Skill $skill)
    {

        $skill->update($this->validateSkillRequest($request));

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

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Skill $skill
     * @return RedirectResponse
     */
    public function destroy(Skill $skill)
    {
        $skill->delete();
        return back(303)->with(['success' => 'success']);
    }
}
