<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Http\Requests\ContenttexteStoreRequest;
use App\Http\Requests\ContenttexteUpdateRequest;
use App\Http\Requests\ContenttexteDeleteRequest;
use App\Contenu;
use App\Contenttexte;
//use App\Key;
use Exception;

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

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

        /*try {

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


            //On vérifie si l'id du contenu existe
            if (Contenu::on($key->bd)->where('id', '=', $request->contenu_id)->first()) {

                $contenu = Contenu::on($key->bd)->where('id', '=', $request->contenu_id)->first();

                //Création du contenttexte
                $new_contenttexte = new Contenttexte($key->bd);
                $new_contenttexte->contenu()->associate($contenu);
                $new_contenttexte->valeur = $request->valeur;
                $new_contenttexte->statut = "PUBLISHED";
                $new_contenttexte->save();

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

                return response()->json([
                    'status' => 'error',
                    'message' => 'This contenu_id 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($id)
    {
        //
    }

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

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

            $contenttexte = Contenttexte::on('tenant')->findOrFail($id);
            $contenttexte->valeur = $request->valeur;
            $contenttexte->update();

            return response()->json([
                "val" => $contenttexte->valeur,
                "id" => $contenttexte->id,
                'status' => 'success',
                'message' => 'CONTENTTEXTE_MODIFIED_SUCCESSFULLY'
            ])->withHeaders([
                'Accept' => 'application/json',
                'Content-Type' => 'application/json'
            ]);

            //On vérifie si l'id du contenu existe
           // if ((Contenu::on($key->bd)->where('id', '=', $request->contenu_id)->first())) {

                //On vérifie si le statut du contenu existe
                /*$statut_array = ['PUBLISHED', 'BLOCKED', 'PENDING', 'SIGNALED'];
                foreach ($statut_array as $statut) {

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

                        $contenu = Contenu::on($key->bd)->where('id', '=', $request->contenu_id)->first();*/

                        //Modification du contenttexte
                        /*$contenttexte = Contenttexte::on($key->bd)->findOrFail($id);
                        $contenttexte->contenu()->associate($contenu);
                        $contenttexte->statut = $request->statut;
                        $contenttexte->valeur = $request->valeur;
                        $contenttexte->update();

                        return response()->json([
                            'status' => 'success',
                            'message' => 'CONTENTTEXTE_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'
                ]);
            } else {

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

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

        try {

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

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

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

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

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

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