<?php

namespace App\Http\Controllers\API\v1;

use App\Article;
use App\Http\Controllers\Controller;
use App\Http\Requests\SectionUpdateRequest;
use App\Key;
use App\Log;
use App\Section;
use App\User;
use Exception;
use Illuminate\Http\Request;

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

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

    /**
     * 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(SectionUpdateRequest $request, $id)
    {
        try {
            //$key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();


            $SchoolDepInfo= $request->get('SchoolDep');
            $departement = $SchoolDepInfo['departement_id'];
            $bd = $SchoolDepInfo['etablissement_id']."_".$departement;
            $User= $request->get('User');

            //Update of the section
            $section = Section::on('tenant')->findOrFail($id);

            //Verify if the user requesting is the owner of the article
            if (Article::on('tenant')->findOrFail($section->article_id)->get()->first()->user_matricule == $User->matricule) {
                $section->titre = $request->titre;
                $section->update();

                //Sauvegarder l'opération
                $log = new Log();
                $log->bd = $bd;
                $log->operation = 'SECTION UPDATE';
                $log->description = json_encode($section);
                $log->user_matricule = $User->matricule;
                $log->save();

                return response()->json([
                    'status' => 'success',
                    'section' => $section
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            } else {
                //Sauvegarder l'opération
                $log = new Log();
                $log->bd = $bd;
                $log->operation = 'SECTION UPDATE';
                $log->description = 'PERMISSIONS NOT GRANTED';
                $log->user_matricule = $User->matricule;
                $log->save();

                return response()->json([
                    'status' => 'error',
                    'message' => "Permissions not granted"
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            }
        } catch (Exception $e) {
            //Sauvegarder l'opération
            $log = new Log();
            $log->bd = $bd;
            $log->operation = 'SECTION UPDATE';
            $log->description = $e->getMessage();
            $log->user_matricule = $User->matricule;
            $log->save();

            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(Request $request, $id)
    {
        try {
            //$key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();

            $SchoolDepInfo= $request->get('SchoolDep');
            $departement = $SchoolDepInfo['departement_id'];
            $bd = $SchoolDepInfo['etablissement_id']."_".$departement;
            $User= $request->get('User');

            //Get the section
            $section = Section::on('tenant')->findOrFail($id);

            //Get related article
            $article = Article::on('tenant')->where('id', '=', $section->article_id)->get()->first();

            //Verify if the user requesting is the owner of the article
            if ($article->user_matricule == $User->matricule) {
                //Sauvegarder l'opération
                $log = new Log();
                $log->bd = $bd;
                $log->operation = 'SECTION DELETE';
                $log->description = json_encode($section);

                $section->delete();

                $log->user_matricule = $User->matricule;
                $log->save();

                return response()->json([
                    'status' => 'success',
                    'message' => 'SECTION_DELETE_DONE'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            } else {
                //Sauvegarder l'opération
                $log = new Log();
                $log->bd = $bd;
                $log->operation = 'SECTION DELETE';
                $log->description = 'PERMISSIONS NOT GRANTED';
                $log->user_matricule = $User->matricule;
                $log->save();

                return response()->json([
                    'status' => 'error',
                    'message' => "Permissions not granted"
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            }
        } catch (Exception $e) {
            //Sauvegarder l'opération
            $log = new Log();
            $log->bd = $bd;
            $log->operation = 'SECTION DELETE';
            $log->description = $e->getMessage();
            $log->user_matricule = $User->matricule;
            $log->save();

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