<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\ContentimageUpdateRequest;
use App\Http\Requests\ContentimageDeleteRequest;
use App\Contentimage;
use App\Contenu;
//use App\Key;
use Exception;

class ContentimageController 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(Request $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 contentimage
                $new_contentmedia = new Contentmedia($key->bd);
                $new_contentmedia->contenu()->associate($contenu);
                $new_contentmedia->url = $request->url;
                $new_contentmedia->statut = "PUBLISHED";
                $new_contentmedia->save();

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

        try {

            //$key = Key::all()->where('api_key', '=', $request->header('Authorization'))->first();
            $contentimage = Contentimage::on('tenant')->findOrFail($id);

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

                $contentimage->url = $request->url;
            }

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

                $contentimage->description = $request->description_image;
            }

            $contentimage->update();

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

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

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

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

                        //Modification du contentimage
                        $contentimage = Contentimage::on($key->bd)->findOrFail($id);
                        $contentimage->contenu()->associate($contenu);
                        $contentimage->statut = $request->statut;
                        $contentimage->description = $request->description_image;
                        $contentimage->url = $request->url;
                        $contentimage->update();

                        return response()->json([
                            'status' => 'success',
                            'message' => 'CONTENTIMAGE_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'
                ]);
            }*/

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

        try {

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

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

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

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

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

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