<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\CallLog;
use Carbon\Carbon;

class CallLogController extends Controller
{
    public function GetCallLog(){

       # dd(env('Knowlarity_link').'/Basic/v1/account/calllog?start_time=2023-01-01&end_time=2024-06-26&limit=50');
       $date = Carbon::now();
       $start_date =  Carbon::now()->addYears(-1);
       $strt_date= $start_date->format('Y-m-d');
       $endDate = $date->format('Y-m-d');
       isset($_REQUEST['limit']) ? $limit=$_REQUEST['limit'] : $limit=50;
       // $limit=50;
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => env('Knowlarity_link')."/Basic/v1/account/calllog?start_time=$strt_date&end_time=$endDate&limit=$limit",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        CURLOPT_HTTPHEADER => array(
        'Authorization: e6891827-bf14-4482-9b30-0bb7545fab8a',
        'x-api-key: GrVkgos9Py8Bd7SHrsyPY78Y6zZx5OWbLKMLPJRc'
        ),
        ));
       // $response = curl_exec($curl);
        $someArray = json_decode(curl_exec($curl), true);
        curl_close($curl);
        //return $someArray['objects'][0];
        $newData = CallLog::updateOrCreate($someArray['objects'][0]);
    }
}