<?php

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

class StoryController extends Controller
{
    ## ACTIVE CATEGORY LIST
    public function getactivesubcategorylist(){
        $getsubcategorylist   = Subcategory::orderBy('subcategory_id', 'desc')->where('active_status','1')->get();
        if ($getsubcategorylist->isNotEmpty()){
            return response()->json(['success' => true,'message' => 'SubCategory Active Data','data'=> $getsubcategorylist ],200);
        }else{
            return response()->json(['status' => false,'data' => 'failed data'],200);
        }
    }

    ## STORE 
    public function storestory(Request $request){
        $validate   =   $request->validate([
            'subcategory_id'  => 'required',
            'story_image'     => 'required',
        ]);
        $imageorginalname                  = $request->file('story_image')->getClientOriginalName();
        $exploOrginalename                 = @explode('.',$imageorginalname);
        $exploOrginalename                 = str_replace(' ','-',$exploOrginalename);
        $uploadUrl                         = $this->cloudinaryImage($request->file('story_image'), 'Story',$exploOrginalename[0]);

        $storedata                         = new Story();
        $storedata->subcategory_id         = $request->subcategory_id;
        $storedata->story_image            = $uploadUrl;
        $StoreData                         = $storedata->save();
        if ($StoreData) {
            return response()->json(['status' => true,'message'=>'Story Image Upload And data Added Succssfully','data' => $uploadUrl],200);
        }else{
            return response()->json(['message'=>'Update Failed'],400);
        }
    }

    ## GET
    public function getstory(){
        $getdata      = Story::with('subcategorydetails')->orderBy('story_id','desc')->get();
        if ($getdata == true){
            return response()->json(['status' => true, 'data' => $getdata], 200);
        } else {
            return response()->json(['status' => false,'data' => 'failed data'], 400);
        }
    }

    
    ##FIND
    public function findstory($id){
        $find  = Story::with('subcategorydetails')->where('story_id', $id)->orderBy('story_id', 'desc')->get();
        if ($find == true) {
            return response()->json(['status' => true,'message'=>'Find Data Successfully','data' =>  $find],200);
        }else{
            return response()->json(['status' => false,'message'=>'failed find records'],400);
        }
    }


    ## UPDATE
    public function updatestory(Request $request){        
        $validate   =   $request->validate([
            'subcategory_id'  => 'required',
            'story_image'     => 'required',
        ]);
        $imageorginalname                  = $request->file('story_image')->getClientOriginalName();
        $exploOrginalename                 = @explode('.',$imageorginalname);
        $exploOrginalename                 = str_replace(' ','-',$exploOrginalename);
        $uploadUrl                         = $this->cloudinaryImage($request->file('story_image'), 'Story',$exploOrginalename[0]);
        
        $updatedata                        = Story::find($request->story_id);
        $updatedata->subcategory_id        = $request->subcategory_id;
        $updatedata->story_image           = $uploadUrl;
        $update                            = $updatedata->save();
        if (!$update){
            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);
        }
    }

    ## 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;
        }
    }
}
