<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\PageStoreRequest;
use App\Http\Requests\PageUpdateRequest;
use App\Http\Requests\PageDeleteRequest;
//use App\Key;
use App\Page;
use App\Contenu;
use App\Contenttexte;
use App\Contentimage;
use App\Contentvideo;
use Illuminate\Support\Facades\DB;
use Exception;

class PageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index_sites( Request $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;
            $User= $request->get('User');

            //$pages = Page::on('tenant')->where('departement_id',$departement)->where('statut','=','PUBLISHED')->get();
            $pages = DB::connection('tenant')->table('pages')->where('departement_id',$departement)->where('statut','=','PUBLISHED')->get();

            $page = DB::connection('tenant')->table('contenus')
                ->select('contenus.*', 'contenants.titre as titre_contenant', 'pages.nom as page_nom', 'contenttextes.valeur as valeur_texte', 'contenttextes.id as contenttexte_id', 'contentimages.url as image_url', 'contentimages.id as contentimages_id','contentvideos.id as contentvideos_id','contentvideos.url as video_url','contentimages.description as image_description','contentvideos.description as video_description')
                ->join('contenants', 'contenus.contenant_id', '=', 'contenants.id')
                ->leftJoin('pages', 'contenus.page_id', '=', 'pages.id')
                ->leftJoin('contenttextes', 'contenus.id', '=', 'contenttextes.contenu_id')
                ->leftJoin('contentimages', 'contenus.id', '=', 'contentimages.contenu_id')
                ->leftJoin('contentvideos', 'contenus.id', '=', 'contentvideos.contenu_id')
                ->get()
                ->where('page_nom', '=', "GENERALE")
                ->groupBy('titre_contenant');

            // Prendre les themes de l'ecole
            $etabli = $SchoolDepInfo['etablissement_id'];
            $server_response = getFromGest('api/v1/etablissements/'.$etabli.'/themes', [])->json();

            if($departement != 1000000) {
                $depInfo = getFromGest('api/v1/etablissement/departement/' . $departement, [
                    'bd' => substr($bd, 5)
                ])->json();

            } else {
                $depInfo = getFromGest('api/v1/etablissement/'.$etabli)->json();

            }

            return response()->json([
                'status' => 'success',
                'pages' => $pages,
                'generale' => $page,
                'themes' => $server_response['data'],
                'DepInfo' => $depInfo
            ])->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'
            ]);
        }
    }

    public function index( Request $request)
    {
        try {

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

            $SchoolDepInfo= $request->get('SchoolDep');
            $departement = $SchoolDepInfo['departement_id'];

            //$pages = Page::on('tenant')->where('departement_id',$departement)->where('statut','=','PUBLISHED')->get();
            $pages = DB::connection('tenant')->table('pages')->where('departement_id',$departement)->get();

            return response()->json([
                'status' => 'success',
                'pages' => $pages,
            ])->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(PageStoreRequest $request)
    {
        try {


            $SchoolDepInfo= $request->get('SchoolDep');
            $departement = $SchoolDepInfo['departement_id'];
            
            $User= $request->get('User');

            //On vérifie si le nom de page existe déjà
            if (!(Page::on('tenant')->where('nom', $request->nom)->first())) {

                //Creation d'une nouvelle page
                $page = new Page('tenant');

                $page->nom = $request->nom;
                $page->departement_id = $departement;
                //$page->statut = 'PUBLISHED';
                $page->save();

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

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

        //try {

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

            /*$request->validate([
                'bd' => 'required|min:3',
                'departement' => 'required|min:3',
            ]);*/

            /*$page = Page::on(hackKey(false,$request->bd))->findOrFail($id);

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

        }*/
        try {

            // $bd = hackKey(false,$request->bd);
            $User= $request->get('User');

            $pageInfo = DB::connection('tenant')->table('pages')->where('id', '=', $id)->first();
            $page = DB::connection('tenant')->table('contenus')
                ->select('contenus.*', 'contenants.titre as titre_contenant', 'pages.nom as page_nom', 'contenttextes.valeur as valeur_texte', 'contenttextes.id as contenttexte_id', 'contentimages.url as image_url', 'contentimages.id as contentimages_id','contentvideos.id as contentvideos_id','contentvideos.url as video_url','contentimages.description as image_description','contentvideos.description as video_description')
                ->join('contenants', 'contenus.contenant_id', '=', 'contenants.id')
                ->leftJoin('pages', 'contenus.page_id', '=', 'pages.id')
                ->leftJoin('contenttextes', 'contenus.id', '=', 'contenttextes.contenu_id')
                ->leftJoin('contentimages', 'contenus.id', '=', 'contentimages.contenu_id')
                ->leftJoin('contentvideos', 'contenus.id', '=', 'contentvideos.contenu_id')
                ->get()
                ->where('page_id', '=', $id)
                ->groupBy('titre_contenant')
                ;


            /*$data = DB::connection(hackKey(false,$request->bd))->table('contenus')
            ->select('contenants.titre as contenant_titre','contenus.nom as contenu','contenttextes.valeur as valeur_contenttexte','contentimages.url as url_contentimage','contentvideos.url as url_contentvideo')
            ->join('contenttextes','contenus.id','=','contenttextes.contenu_id')
            ->join('contentimages','contenus.id','=','contentimages.contenu_id')
            ->join('contentvideos','contenus.id','=','contentvideos.contenu_id')
            ->join('contenants','contenus.contenant_id','=','contenants.id')
            ->where('page_id','=',$id)
            ->get();*/

            /*$contenus = DB::connection(hackKey(false,$request->bd))->table('contenus')->where('page_id','=',$id)->get();
            $contenus_array = array();

            foreach ($contenus as $contenu){

                $dat = DB::connection(hackKey(false,$request->bd))->table('contenus')
                ->join('contenttextes','contenus.id','=','contenttextes.contenu_id')
                ->join('contentimages','contenus.id','=','contentimages.contenu_id')
                ->join('contentvideos','contenus.id','=','contentvideos.contenu_id')
                ->where('page_id','=',$id)
                ->get();

            }*/
            //->join('contentimages','contenus.id','=','contentimages.contenu_id')
            //->join('contentvideos','contenus.id','=','contentvideos.contenu_id')
            //->join('contenants','contenus.contenant_id','=','contenants.id')
            //$page = Page::on(hackKey(false,$request->bd))->findOrFail($id);

            //Récupérer les contenus de la page

            /*$contenus_with_texts = DB::connection('site_epac')->table('contenus')
                ->select('contenus.*', 'contenants.titre', 'pages.nom as page_nom', 'contenttextes.valeur', 'contenttextes.id as contenttexte_id', 'contentimages.url', 'contentimages.id as contentimages_id')
                ->join('contenants', 'contenus.contenant_id', '=', 'contenants.id')
                ->leftJoin('pages', 'contenus.page_id', '=', 'pages.id')
                ->leftJoin('contenttextes', 'contenus.id', '=', 'contenttextes.contenu_id')
                ->rightJoin('contentimages', 'contenus.id', '=', 'contentimages.contenu_id')
                ->get()
                ->where('page_id', '=', $id)
                ->groupBy('nom');*/



            // $contenttextes = DB::connection($key->bd)->table('contenttextes')
            //     ->select('contenttextes.*', 'contenus.nom as contenu_nom', 'pages.id as page_id', 'pages.nom as page_nom')
            //     ->join('contenus', 'contenttextes.contenu_id', '=', 'contenus.id')
            //     ->join('pages', 'contenus.page_id', '=', 'pages.id')
            //     ->get()
            //     ->where('page_nom', '=', $page->nom)
            //     ->groupBy('titre', 'nom');

            //     $temptable = DB::connection($key->bd)->raw("(SELECT contenu_id, valeur
            // FROM contenttextes GROUP BY contenu_id) as t1");

            //     $contenus_with_texts = DB::connection($key->bd)->table('contenus')
            //     ->select('contenus.*', 'contenants.titre', 'pages.nom as page_nom', 'contenttextes.valeur', 'contenttextes.id as contenttexte_id')
            //     ->join('contenants', 'contenus.contenant_id', '=', 'contenants.id')
            //     ->join('pages', 'contenus.page_id', '=', 'pages.id')
            //     ->leftJoin($temptable, 't1.contenu_id', '=', 'contenus.id')
            //     // ->leftJoin('contentmedias', 'contenus.id', '=', 'contentmedias.contenu_id')
            //     ->get()
            //     ->where('page_nom', '=', $page->nom)
            //     ->groupBy('titre', 'nom');


            // $contenus_with_texts = DB::connection($key->bd)->table('contenus')
            // ->select('contenus.*', 'contenants.titre', 'pages.nom as page_nom', 'contenttextes.valeur', 'contenttextes.id as contenttexte_id', 'contentmedias.url', 'contentmedias.id as contentmedias_id')
            // ->leftJoin(
            //     DB::raw('(SELECT contenttexte_id FROM contenttextes GROUP BY (contenttexte_id)
            //     ) as t2'), function ($join) {
            //         $join->on ( 'seasons.id', '=', 't2.season_id' );
            //     }
            // )
            // ->leftJoin(
            //     DB::raw('(SELECT season_id, rating FROM ratings_season WHERE user_id = 1) as t3'), function ($join) {
            //         $join->on ( 'seasons.id', '=', 't3.season_id' );
            //     }
            // )
            // ->orderBy('seasons.number', 'desc')
            // ->get();

            // $contenus_with_medias = DB::connection($key->bd)->table('contenus')
            //     ->select('contenus.*', 'contenants.titre', 'pages.nom', 'contentmedias.url')
            //     ->join('contenants', 'contenus.contenant_id', '=', 'contenants.id')
            //     ->join('pages', 'contenus.page_id', '=', 'pages.id')
            //     ->join('contentmedias', 'contentmedias.contenu_id', '=', 'contenus.id')
            //     ->get()
            //     ->where('nom', '=', $page->nom)
            //     ->groupBy('titre');

            return response()->json([
                'status' => 'success',
                'page' => $page,
                'pageInfo' =>$pageInfo
            ])->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(PageUpdateRequest $request, $id)
    {
        try {
            $User= $request->get('User');

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

            /*if(isset($request->nom)&& isset($request->statut)){

                //On vérifie si le nom de la page existe déjà au niveau des autres noms de page
                if (!(Page::on($key->bd)->where('id', '!=', $id)->where('nom', '=', $request->nom))->first()) {

                    //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 de la page
                            $page = Page::on($key->bd)->findOrFail($id);
                            $page->nom = $request->nom;
                            $page->statut = $request->statut;
                            $page->update();
                            $variable = 1;
                        }
                    }
                    if ($variable == 0){

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

                }else {

                    return response()->json([
                        'status' => 'error',
                        'message' => 'This page name already exists.'
                    ])->withHeaders([
                        'Accept' => 'application/json',
                        'Content-Type' => 'application/json'
                    ]);
                }
            }
            else if (!isset($request->nom) && isset($request->statut)){

                //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 de la page
                        $page = Page::on($key->bd)->findOrFail($id);
                        $page->statut = $request->statut;
                        $page->update();
                        $variable = 1;
                    }
                }

                if ($variable == 0){

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

            }
            else if(isset($request->nom) && !isset($request->statut)){

                //On vérifie si le nom de la page existe déjà au niveau des autres noms de page
                if (!(Page::on($key->bd)->where('id', '!=', $id)->where('nom', '=', $request->nom))->first()) {

                    //Modification de la page
                    $page = Page::on($key->bd)->findOrFail($id);
                    $page->nom = $request->nom;
                    $page->update();
                }
                else{

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

            // $bd = hackKey(false,$request->bd);
            //$departement = hackKey(false,$request->departement);

            //On vérifie si le nom de la page existe déjà au niveau des autres noms de page
            if (!(Page::on('tenant')->where('id', '!=', $id)->where('nom', '=', $request->nom))->first()) {

                //Modification de la page
                $page = Page::on('tenant')->findOrFail($id);

                if(isset($request->nom)){
                    $page->nom = $request->nom;
                }
                if(isset($request->statut)){
                    $page->statut = $request->statut;
                }
                $page->update();
            }
            else{

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

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



            //On vérifie si le nom de la page existe déjà au niveau des autres noms de page
            /*if (!(Page::on($key->bd)->where('id', '!=', $id)->where('nom', '=', $request->nom))->first()) {


                if ((isset($request->statut))){

                    //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 de la page
                        $page = Page::on($key->bd)->findOrFail($id);

                        if(isset($request->nom)){
                            $page->nom = $request->nom;
                        }

                        $page->statut = $request->statut;
                        $page->update();
                    }
                  }

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

                    //Modification de la page
                    $page = Page::on($key->bd)->findOrFail($id);
                    $page->nom = $request->nom;
                    $page->update();
                }

                if (Contenu::on($key->bd)->where('page_id', '=', $page->id)->get()) {

                    $contenus = Contenu::on($key->bd)->where('page_id', '=', $page->id)->get();

                    foreach ($contenus as $contenu) {

                        $contenu->statut = $request->statut;
                        $contenu->update();

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

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

                            foreach ($contenttextes as $contenttexte) {

                                $contenttexte->statut = $request->statut;
                                $contenttexte->update();
                            }
                        }

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

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

                            foreach ($contentimages as $contentimage) {

                                $contentimage->statut = $request->statut;
                                $contentimage->update();
                            }
                        }

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

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

                            foreach ($contentvideos as $contentvideo) {

                                $contentvideo->statut = $request->statut;
                                $contentvideo->update();
                            }
                        }
                    }
                }

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

            } else {

                return response()->json([
                    'status' => 'error',
                    'message' => 'This page 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'
            ]);
        }
    }

    public function restore(Request $request,$id){

        try {

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

            $User= $request->get('User');

            // $bd = hackKey(false,$request->bd);
            //$departement = hackKey(false,$request->departement);

            //Obtenir la page
            $page = Page::on('tenant')->findOrFail($id);

            if($page->statut == 'DELETED'){

                $page->statut = "PUBLISHED";
                $page->update();

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

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

                    foreach ($contenus as $contenu) {

                        $contenu->statut = "PUBLISHED";
                        $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 = 'PUBLISHED';
                                $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 = 'PUBLISHED';
                                $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 = 'PUBLISHED';
                                $contentvideo->update();
                            }
                        }
                    }
                }

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

            }else{

                return response()->json([
                    'status' => 'success',
                    'message' => 'STATUT_NOT_EQUAL_TO_DELETED'
                ])->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(PageDeleteRequest $request, $id)
    {

        try {

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

            // $bd = hackKey(false,$request->bd);
            //$departement = hackKey(false,$request->departement);
            $User= $request->get('User');

            //Obtenir la page
            $page = Page::on('tenant')->findOrFail($id);

            //Suppression de la page selon le mode
            if ($request->mode == "soft") {

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

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

                    $contenus = Contenu::on('tenant')->where('page_id', '=', $page->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") {

                $page->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'
            ]);
        }
    }
}
