<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Cloudinary\Cloudinary;
use Cloudinary\Api\Upload\UploadApi;
use App\Models\HomeEducate;
use App\Repositories\ResponseRepository;
use Illuminate\Support\Facades\Log;
use Cloudinary\Uploader;



class HomeEducateController extends Controller
{
    public function __construct(ResponseRepository $response) {
        $this->response = $response;
        $this->ruleOne = [
            'home_educate_title' => 'required',
            'home_educate_description' => 'required',
            'home_educate_one' => 'required',
            'home_educate_two' => 'required',
            'home_educate_three' => 'required',
            'home_educate_four' => 'required',
            'home_educate_title' => 'required',
            'home_educate_image_text' => 'required',

        ];
        $this->ruleTwo = [
            'home_educate_video' => 'required',
        ];
    }

    public function getHomeEducateDetailsTitles() {
        return $this->response->jsonResponse(false, "Home   Fetched Successfully", HomeEducate::all(), 550);
    }

    public function updateHomeEducateDetailsTitles(Request $request) {
        $validate = $this->response->validate($request->all(), $this->ruleOne);
        if($validate === true) {
            return $this->response->jsonResponse(false, $this->response->message('Home Details', 'update'), HomeEducate::first()->update($request->all()), 551);
        } else {
            return $this->response->jsonResponse(true, 'Home Details Update Failed', $validate, 552);
        }

    }

    public function homeEducateVideoUpload(Request $request) {

        $httpClient = new HomeEducate();
        $client = new HomeEducate(
            // 'https://ws.api.video',
             env('APP_APIVIDEO'),
            $httpClient
        );
        $file = request()->file('video');
        $fileName = request()->title;
        $payload = (new VideoCreationPayload())->setTitle($fileName);
        $payload = $payload->setTitle('Hello');
        if (isset(request()->description))
        {
            $payload->setDescription(request()->description);
        }
        if(isset(request()->public))
        {
            $payload->setPublic(False);
        }
        if(isset(request()->mp4support))
        {
            $payload->setMp4support(False);
        }
        $video = $client->videos()->create($payload);
        $filePath = request()->file('video')->getRealPath();

        $response = $client->videos()->upload(
            $video->getVideoId(),
            new \SplFileObject($filePath)
        );

        return redirect('/response');
    }


    public function show($id) {
        return $this->response->jsonResponse(false, $this->response->message('HomeEducate', 'show'), $this->findHomeEducate($id), 302);
    }


//     public function videouploaded(Request $request) {
//         $getBanner = $this->findHomeEducate($request['id']);
//        if($request->hasFile('home_educate_video')) {
//            $file = $request->file('home_educate_video');
//            $name = $this->response->generateSlug($getBanner['home_educate_title']);
//            $uploadUrl = $this->response->cloudinaryImage($request->file('home_educate_video'), 'video', $name);
//            return $this->response->jsonResponse(false, $this->response->message('HomeEducate ', 'Video'), $getBanner->update(['home_educate_video' => $uploadUrl]), 108);
//        } else {
//            return $this->response->jsonResponse(true, 'video is too high', [], 109);
//        }
//    }


public function videouploaded(Request $request) {
    $getBanner = $this->findHomeEducate($request['id']);
    if ($request->hasFile('home_educate_video')) {
        $file = $request->file('home_educate_video');
        $name = $this->response->generateSlug($getBanner['home_educate_title']);

        // Create a Cloudinary instance
        $cloudinary = new Cloudinary(config('cloudinary'));

        // Use the UploadApi class to upload the video
        $uploadApi = new UploadApi($cloudinary->getApi());

        // Upload the video
        $uploadResult = $uploadApi->uploadVideo($file->getRealPath(), [
            'resource_type' => 'video',
            'public_id' => $name,
            'folder' => 'video_folder' // Optional folder name
        ]);

        // Get the video URL from the upload result
        $uploadUrl = $uploadResult['secure_url'];

        return $this->response->jsonResponse(false, $this->response->message('HomeEducate', 'Video'), $getBanner->update(['home_educate_video' => $uploadUrl]), 108);
    } else {
        return $this->response->jsonResponse(true, 'video is too high', [], 109);
    }
}


   public function findHomeEducate($id) {
    return HomeEducate::find($id);
}
}
