<?php

namespace App\Http\Controllers\API\v1;

use App\Competence;
use App\Http\Controllers\Controller;
use App\Http\Requests\CompetenceStoreRequest;
use App\Http\Requests\CompetenceUpdateRequest;
use App\Http\Requests\CompetenceDeleteRequest;
use Illuminate\Http\Request;
//use App\Key;
use Exception;

class CompetenceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try {

            $competences = Competence::on('tenant')->get();

            return response()->json([
                'status' => 'success',
                'competences' => $competences
            ])->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);

        } catch (Exception $e) {

            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage()
            ], 404)->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CompetenceStoreRequest $request)
    {
        try {

            //$key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();

            $SchoolDepInfo= $request->get('SchoolDep');
            $departement = $SchoolDepInfo['departement_id'];
            $bd = $SchoolDepInfo['etablissement_id']."_".$departement;
            //Création d'une nouvelle compétence
            $competence = new Competence('tenant');
            $competence->filiere_id = $request->filiere_id;
            $competence->texte = $request->texte;
            $competence->departement_id = $departement;
            $competence->save();

            return response()->json([
                'status' => 'success',
                'competence' => $competence
            ])->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);

        } catch (Exception $e) {

            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage()
            ], 404)->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);
        }
    }

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

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

        try {

            //$key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();

            $competence = Competence::on('tenant')->findOrFail($id);

            $SchoolDepInfo= $request->get('SchoolDep');
            $departement = $SchoolDepInfo['departement_id'];
            $bd = $SchoolDepInfo['etablissement_id']."_".$departement;
            //Modification de la compétence
            $competence->filiere_id = $request->filiere_id;
            $competence->texte = $request->texte;
            $competence->departement_id = $departement;
            $competence->update();

            return response()->json([
                'status' => 'success',
                'competence' => $competence
            ])->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);

        } catch (Exception $e) {

            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage()
            ], 404)->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(CompetenceDeleteRequest $request,$id)
    {
        try {

            //$key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();

            //Obtenir la compétence
            $competence = Competence::on('tenant')->findOrFail($id);

            if ($request->mode == "soft") {

                $competence->statut = 'DELETED';
                $competence->update();

                return response()->json([
                    'status' => 'success',
                    'competence' => 'SOFT_DELETE_DONE'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            } else if ($request->mode == "strict") {

                $competence->delete();
                return response()->json([
                    'status' => 'success',
                    'competence' => 'STRICT_DELETE_DONE'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            } else {

                return response()->json([
                    'status' => 'error',
                    'message' => 'This mode does not exist.'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            }
        } catch (Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => $e->getMessage()
            ], 404)->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);
        }
    }
}
