<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use CloudinaryLabs\CloudinaryLaravel\Facades\Cloudinary;
use Illuminate\Http\Request;
use App\Models\Homebanner;
use App\Models\Featured;
use App\Models\Spotlight;
use App\Models\Highlights;
use App\Models\HomeMobile;

class ImageuploadController extends Controller
{
    public function imageUpload(Request $request) {

        ## ECART IMAGE UPLOAD
        if (!empty($request->id) && $request->file('home_banner_image')){
            $uploadUrl                               = $this->cloudinaryImage($request->file('home_banner_image'), 'HomeBanner','home_banner_image'.$request->id);
            $updateGalleryData                        = Homebanner::find($request->id);
            $updateGalleryData->home_banner_image     = $uploadUrl;
            $UpdateData                               = $updateGalleryData->save();
            if ($UpdateData) {
                return response()->json(['status' => true,'message'=>'Update Image Successfully','data' => $uploadUrl],200);
            }else{
                return response()->json(['message'=>'Update Failed'],400);
            }
        }else  if (!empty($request->id) && $request->file('featured_image')){
            $uploadUrl                               = $this->cloudinaryImage($request->file('featured_image'), 'Featured','featured_image'.$request->id);
            $updateGalleryData                        = Featured::find($request->id);
            $updateGalleryData->featured_image        = $uploadUrl;
            $UpdateData                               = $updateGalleryData->save();
            if ($UpdateData) {
                return response()->json(['status' => true,'message'=>'Update Image Successfully','data' => $uploadUrl],200);
            }else{
                return response()->json(['message'=>'Update Failed'],400);
            }
        }else  if (!empty($request->id) && $request->file('spotlight_image')){
            $uploadUrl                               = $this->cloudinaryImage($request->file('spotlight_image'), 'Spotlight','spotlight_image'.$request->id);
            $updateGalleryData                        = Spotlight::find($request->id);
            $updateGalleryData->spotlight_image       = $uploadUrl;
            $UpdateData                               = $updateGalleryData->save();
            if ($UpdateData) {
                return response()->json(['status' => true,'message'=>'Update Image Successfully','data' => $uploadUrl],200);
            }else{
                return response()->json(['message'=>'Update Failed'],400);
            }
        }else  if (!empty($request->id) && $request->file('highlights_image')){
            $uploadUrl                               = $this->cloudinaryImage($request->file('highlights_image'), 'Highlights','highlights_image'.$request->id);
            $updateGalleryData                        = Highlights::find($request->id);
            $updateGalleryData->highlights_image      = $uploadUrl;
            $UpdateData                               = $updateGalleryData->save();
            if ($UpdateData) {
                return response()->json(['status' => true,'message'=>'Update Image Successfully','data' => $uploadUrl],200);
            }else{
                return response()->json(['message'=>'Update Failed'],400);
            }
        }else if (!empty($request->id) && $request->file('home_mobile_banner_image')){
            $uploadUrl                               = $this->cloudinaryImage($request->file('home_mobile_banner_image'), 'HomeMobileBanner','home_mobile_banner_image'.$request->id);
            $updateGalleryData                               = HomeMobile::find($request->id);
            $updateGalleryData->home_mobile_banner_image     = $uploadUrl;
            $UpdateData                                      = $updateGalleryData->save();
            if ($UpdateData) {
                return response()->json(['status' => true,'message'=>'Update Image Successfully','data' => $uploadUrl],200);
            }else{
                return response()->json(['message'=>'Update 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;
        }
    }
}
