<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Http\Requests\ContenantStoreRequest;
use App\Http\Requests\ContenantUpdateRequest;
use App\Http\Requests\ContenantDeleteRequest;
use Illuminate\Http\Request;
use App\Contenant;
use App\Contenu;
use App\Contenttexte;
use App\Contentimage;
use App\Contentvideo;
use Exception;

class ContenantController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        try {
            //$request = req$request::all()->where('api_req$request', '=', $request->header('Authorization'))->first();
            $contenants = Contenant::on('tenant')->get();

            return response()->json([
                'status' => 'success',
                'contenants' => $contenants
            ])->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'
            ]);
        }
    }

    /**
     * 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(ContenantStoreRequest $request)
    {
        try {

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

            //Vérifier si le type de contenant est valide
            //$statut_contenant_array = ['T','I','V','IT','TV','IV','ITV'];

            //foreach($statut_contenant_array as $type){

            //if($request->type == $type){

            if (!(Contenant::on('tenant')->where('titre', '=', $request->titre)->first())) {

                //Creation d'une nouveau contenant
                $contenant = new Contenant('tenant');

                $contenant->titre = $request->titre;
                //$contenant->many = $request->many;
                //$contenant->type = $request->type;
                //$contenant->statut = 'PUBLISHED';
                $contenant->save();

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

                return response()->json([
                    'status' => 'error',
                    'message' => 'This title name already exists.'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);
            }
            //}
            //}

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

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

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


            //$request = req$request::all()->where('api_req$request', '=', $request->header('Authorization'))->first();

            $contenant = Contenant::on('tenant')->findOrFail($id);

            return response()->json([
                'status' => 'success',
                'contenant' => $contenant
            ])->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'
            ]);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

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

        try {

            //$request = req$request::all()->where('api_req$request', '=', $request->header('Authorization'))->first();

            //On vérifie si le titre du contenant existe déjà au niveau des autres titres de contenants
            if (!(Contenant::on('tenant')->where('id', '!=', $id)->where('titre', '=', $request->titre))->first()) {

                //Vérifier si le type de contenant est valide
                //$statut_contenant_array = ['T','I','V','IT','TV','IV','ITV'];

                //foreach($statut_contenant_array as $type){

                //if($request->type == $type){

                //On vérifie si le statut de la requete est valide
                //$statut_array = ['PUBLISHED', 'BLOCKED', 'PENDING', 'SIGNALED'];

                //foreach ($statut_array as $statut) {

                    //if ($request->statut == $statut) {

                        //Modification du contenant
                        $contenant = Contenant::on('tenant')->findOrFail($id);

                        $contenant->titre = $request->titre;
                        //$contenant->many = $request->many;
                        //$contenant->type = $request->type;
                        //$contenant->statut = $request->statut;
                        $contenant->save();

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

               /* return response()->json([
                    'status' => 'error',
                    'message' => 'This statut does not exist.'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);*/

                //}
                //}

               /* return response()->json([
                    'status' => 'error',
                    'message' => 'This type does not exist.'
                ])->withHeaders([
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/json'
                ]);*/
            } else {

                return response()->json([
                    'status' => 'error',
                    'message' => 'This Contenant name already exists.'
                ])->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(ContenantDeleteRequest $request, $id)
    {

        try {

            //$request = req$request::all()->where('api_req$request', '=', $request->header('Authorization'))->first();

            //Obtenir le contenant
            $contenant = Contenant::on('tenant')->findOrFail($id);

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

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

                if (Contenu::on('tenant')->where('contenant_id', '=', $contenant->id)->get()) {

                    $contenus = Contenu::on('tenant')->where('contenant_id', '=', $contenant->id)->get();

                    foreach ($contenus as $contenu) {

                        $contenu->statut = "DELETED";
                        $contenu->update();

                        if (Contenttexte::on('tenant')->where('contenu_id', '=', $contenu->id)->get()) {

                            $contenttextes = Contenttexte::on('tenant')->where('contenu_id', '=', $contenu->id)->get();

                            foreach ($contenttextes as $contenttexte) {

                                $contenttexte->statut = 'DELETED';
                                $contenttexte->update();
                            }
                        }

                        if (Contentimage::on('tenant')->where('contenu_id', '=', $contenu->id)->get()) {

                            $contentimages = Contentimage::on('tenant')->where('contenu_id', '=', $contenu->id)->get();

                            foreach ($contentimages as $contentimage) {

                                $contentimage->statut = 'DELETED';
                                $contentimage->update();
                            }
                        }

                        if (Contentvideo::on('tenant')->where('contenu_id', '=', $contenu->id)->get()) {

                            $contentvideos = Contentvideo::on('tenant')->where('contenu_id', '=', $contenu->id)->get();

                            foreach ($contentvideos as $contentvideo) {

                                $contentvideo->statut = 'DELETED';
                                $contentvideo->update();
                            }
                        }
                    }
                }

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

                $contenant->delete();
                return response()->json([
                    'status' => 'success',
                    'message' => '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'
            ]);
        }
    }
}
