<?php

namespace App\Http\Controllers;
use App\Models\Spotlight;
use Illuminate\Http\Request;
use CloudinaryLabs\CloudinaryLaravel\Facades\Cloudinary;

class SpotlightController extends Controller
{
    ##STORE
    public function storespotlight(Request $request){
        $validate   =   $request->validate([
            'spotlight_link'       => 'required',
            'spotlight_image'      => 'required',
        ]);
        $imageorginalname             = $request->file('spotlight_image')->getClientOriginalName();
        $exploOrginalename            = @explode('.',$imageorginalname);
        $exploOrginalename            = @str_replace(' ','-',$exploOrginalename);
        $uploadUrl                    = $this->cloudinaryImage($request->file('spotlight_image'), 'Spotlight',$exploOrginalename[0]);

        $storedata                    = new Spotlight();
        $storedata->spotlight_link    = $request->spotlight_link;
        $storedata->spotlight_image   = $uploadUrl;
        $store                        = $storedata->save();
        if ($store == true){
            return response()->json(['status' => true,'data' => 'Spotlight added Successfully'],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed Store'],200);
        }
    }

    ##GET 
    public function getspotlight(){
        $getdata        = Spotlight::all();
        if ($getdata == true){
            return response()->json(['success' => true, 'data' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }
        ##GET 
        public function getwebSpotlightCount(){
            $data = [];
            $data['active'] = Spotlight::where('active_status', 1)->count();
            $data['inactive'] = Spotlight::where('active_status', 0)->count();
            $data['all'] = Spotlight::all()->count();
    
            if ($data == true){
                return response()->json(['status' => true, 'data' => $data], 200);
            } else {
                return response()->json(['status' => false,'data' => 'failed data'], 400);
            }
        }
    

    ##SWITCH
    public function switchspotlight($id){
        $switchData                  = Spotlight::find($id);
        $status                      = $switchData->active_status === 1 ? ['num' => 0, 'message' => 'Deactiveted'] : ['num' => 1, 'message' => 'Activeted'];
        $switchData->active_status   = $status['num'];
        $Switch                      = $switchData->save();
        if ($Switch) {
            return response()->json(['status' => true,'message'=> $status['message']],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed switch '],400);
        }
    }

    ##DESTORY
    public function destroyspotlight($id){
        $destroy  = Spotlight::where('spotlight_id', $id)->delete();
        if ($destroy == true) {
            return response()->json(['status' => true,'message'=>'destroy Success'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed destroy'],400);
        }
    }

    ##UPDATE
    public function updatespotlight(Request $request){
        $validate   =   $request->validate([
            'spotlight_link'       => 'required',
            'spotlight_image'      => 'required',
        ]);
        $imageorginalname             = $request->file('spotlight_image')->getClientOriginalName();
        $exploOrginalename            = @explode('.',$imageorginalname);
        $exploOrginalename            = @str_replace(' ','-',$exploOrginalename);
        $uploadUrl                    = $this->cloudinaryImage($request->file('spotlight_image'), 'Spotlight',$exploOrginalename[0]);

        $updatedata                     = Spotlight::find($request->id);
        $updatedata->spotlight_image    = $uploadUrl;
        $updatedata->spotlight_link     = $request->spotlight_link;
        $update                         = $updatedata->save();
        if (!$updatedata){
            return response()->json(['status' => false,'message'=>'update data not found'],404);
        }else if ($update == true) {
            return response()->json(['status' => true,'message'=>'update Success'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed update'],400);
        }
    }


    ##FIND
    public function findspotlight($id){
        $find     = Spotlight::where('spotlight_id', $id)->orderBy('spotlight_id', 'desc')->get();
        if ($find == true && !empty($find)) {
            return response()->json(['status' => true,'message'=>'Find Data Successfully','data' =>  $find],200);
        }else if(empty($find)){
            return response()->json(['status' => true,'message'=>'Data is Empty'],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed find records'],400);
        }
    }

    ##SEARCH
    public function searchspotlight($name){
        $searchRecord   = Spotlight::Where('spotlight_link', 'LIKE', '%'.$name.'%')->get();
        if (!empty($searchRecord)) {
            return response()->json(['status' => true,'message'=>'search Record Successfully','data' =>  $searchRecord],200);
        }else{
            return response()->json(['status' => false,'message'=>'searchRecord Record Failed'],400);
        }
    }

    ## CLOUDINARY PARTS
    public function cloudinaryImage($file, $folder, $name) {
        try {
            $image_path = $file->getRealPath();
            $uploadUrl = Cloudinary::upload($image_path, ['folder' => 'Ecart/'.$folder, 'public_id' => $name])->getSecurePath();
            return $uploadUrl;
        } catch (\Exception $e) {
            // Log the error or handle it in another way
            Log::error('Cloudinary upload failed: '.$e->getMessage());
            return null;
        }
    }

}
