<?php

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

class HomebannerController extends Controller
{
    ##STORE
    public function storehomebanner(Request $request){
        $validate   =   $request->validate([
            'home_banner_image'      => 'required',
            'home_banner_link'       => 'required',
        ]);
        $imageorginalname             = $request->file('home_banner_image')->getClientOriginalName();
        $exploOrginalename            = @explode('.',$imageorginalname);
        $exploOrginalename            = @str_replace(' ','-',$exploOrginalename);
        $uploadUrl                    = $this->cloudinaryImage($request->file('home_banner_image'), 'HomeBanner',$exploOrginalename[0]);

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

    ##GET 
    public function gethomebanner(){
        // $getdata['AcitveHomebanner']      = Homebanner::where('active_status',1)->orderBy('id','desc')->get();
        // $getdata['DeacitveHomebanner']    = Homebanner::where('active_status',0)->orderBy('id','desc')->get();
        $getdata    = Homebanner::all();

        if ($getdata == true){
            return response()->json(['status' => true, 'data' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }

    ##GET 
    public function getwebBannerCount(){
        $data = [];
        $data['active'] = Homebanner::where('active_status', 1)->count();
        $data['inactive'] = Homebanner::where('active_status', 0)->count();
        $data['all'] = Homebanner::all()->count();

        if ($data == true){
            return response()->json(['status' => true, 'data' => $data], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }

    // public function productCount() {
    //     $data = [];
    //     $data['active'] = Product::where('active_status', 1)->count();
    //     $data['inactive'] = Product::where('active_status', 0)->count();
    //     return $this->response->jsonResponse(false, 'Product Count Fetched', $data, 201);
    // }
 
 
 
 
 
 
 


    ##SWITCH
    public function switchhomebanner($id){
        $switchData                  = Homebanner::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 destroyhomebanner($id){
        $destroy  = Homebanner::where('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 updatehomebanner(Request $request){
        $validate   =   $request->validate([
            'home_banner_image'  => 'required',
            'home_banner_link'   => 'required',
        ]);
        $imageorginalname                  = $request->file('home_banner_image')->getClientOriginalName();
        $exploOrginalename                 = @explode('.',$imageorginalname);
        $exploOrginalename                 = @str_replace(' ','-',$exploOrginalename);
        $uploadUrl                         = $this->cloudinaryImage($request->file('home_banner_image'), 'HomeBanner',$exploOrginalename[0]);
        $updatedata                        = Homebanner::find($request->id);
        $updatedata->home_banner_image     = $uploadUrl;
        $updatedata->home_banner_link      = $request->home_banner_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 findhomebanner($id){
        $find  = Homebanner::where('id', $id)->orderBy('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 searchhomebanner($name){
        $searchRecord   = Homebanner::where('home_banner_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;
        }
    }
}
