<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Storage;
use App\Modules\RemoteStorage\GoogleBucketGateway;
use App\Traits\ValidatorTraits;
use App\Traits\GoogleBucket;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Str;
use App\Traits\RetrieveCustomerDataTraits;



class TestController extends Controller
{
    use ValidatorTraits, GoogleBucket, RetrieveCustomerDataTraits;

    public function __construct()
    {
        $this->googleBucket = new GoogleBucketGateway;
    }

    public function uploadFile(Request $payload, GoogleBucketGateway $googleBucket){

        $rules = array(
            'resume' => 'required|mimes:pdf|max:10240',
        );
        $validate = $this->validateRequest($payload, $rules);
        if ($validate) {
            return $validate;
        }

        if (request()->has('resume')) {
            // return "naay resume do!";

            $uniqueString = date('Y_m_d_H_i_s');
            $extension = $payload->file('resume')->extension();
            $userID = 1;
            $resumeFilePathName = 'files/resume/jobseeker/'. $userID .'/'. $uniqueString . '.'.$extension;


            // temporarily upload on the server 
            $temporaryFilePath = $payload->file('resume')->storeAs(
                '',  $resumeFilePathName
            );

            /***************************************
                Upload file on the google bucket
            **************/
            $fileSource = Storage::get($temporaryFilePath); //get the file on the tempary storage
            $savedProfile = $googleBucket->store($fileSource, $resumeFilePathName);

            // delete the pic uploaded to server 
            if($savedProfile){
                Storage::delete($resumeFilePathName);
            }

            return [
                'uniqueString' => $uniqueString,
                'extension' => $extension,
                'resumeFilePathName' => $resumeFilePathName,
                'temporaryFilePath' => $temporaryFilePath,
            ];
        }

        return "Resume is required";
    }

    public function removeFile(){
        $filePath = 'files/resume/jobseeker/39/2022_05_18_02_21_33.pdf'; //not exisinting
        // $filePath = 'files/resume/jobseeker/36/2022_05_17_06_44_44.pdf'; //exisinting

        return $this->deleteUploadedFile($filePath, $this->googleBucket);
    }   

    public function saveProfile(Request $payload){

        $profilePicture = $payload->profile_picture;

        $hasProfile = $profilePicture == 'null' || $profilePicture == ''?  false : true;
        $profilePicture = $hasProfile? $payload->profile_picture : null;
        // profile picture
        if ($hasProfile) {
            $image_64 = $profilePicture;
            $extension = explode('/', explode(':', substr($image_64, 0, strpos($image_64, ';')))[1])[1]; // .jpg .png
            $replace = substr($image_64, 0, strpos($image_64, ',') + 1);
            $image = explode(',', $image_64)[1];

            $companyID = 1;
            $filePath = $this->createUniqueImageNameWorker($companyID, $extension);

            $this->uploadBase64Image($image, $filePath);

            return [
                'filePath' => $filePath,
                'image' => $image,
            ];
        }

        // if($payload->image_object){
        //     $filePayloadName = 'image_object'; //input name of the file on the payload
        //     // if jobseeker is created - upload file on google bucket
        //     $resumeExtension = $payload->file($filePayloadName)->extension();
        //     $resumeFilePathName = 'dev/test/image-upload.'. $resumeExtension;
        //     //upload resume file in the google bucket
        //     $this->uploadNewFile($payload, $resumeFilePathName, $filePayloadName, $this->googleBucket);

        //     return [
        //         'resumeExtension' => $resumeExtension,
        //         'resumeFilePathName' => $resumeFilePathName,
        //         'filePayloadName' => $filePayloadName,
        //     ];
        // }
    }

    public function retrieveCustomer(){
        $check = $this->retrieveStripeCustomerData('cus_M4yVdjUNriPo6o');
        // $check = $this->retrieveStripeCustomerData('cus_M4yVdjUNriPo6oasdasdd');

        if($check){
            return "naay sulod oh";
        }

        return 'way sulod';
    }
}
