<?php
error_reporting(-1);
session_start();
function randomColorHex()
{
    return sprintf("#%06X", mt_rand(0, 0xFFFFFF));
}

function access_token()
{
    if (isset($_SESSION['auren_cb_tokken'])) {
        return $_SESSION['auren_cb_tokken'];
    } else {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.prod.us.five9.net/oauth2/v1/token',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => 'grant_type=client_credentials&client_id=gG5RwWjof2AnJMdxULY4YG3Ji5AgnO2Hc5HU6FMhwPJk08tG&client_secret=SJurhTQBrgBZDRQsZoE4AWNYi0QAjABMEAu0S1fznGr876BihMmMBhZnb9e4luGi',
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/x-www-form-urlencoded'
            ),
        ));

        $access_token = curl_exec($curl);

        curl_close($curl);
        $access_token = json_decode($access_token, true);
        // print_r($access_token);
        $_SESSION['auren_cb_tokken'] = $access_token['access_token'];
        return $access_token['access_token'];
    }
}

function buyers_available_server($skill)
{
    $access_token = access_token();
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.prod.us.five9.net/snapshot-stats/v1/domains/119481/statistics/acd-status?filters=' . urlencode($skill),
        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: Bearer ' . $access_token
        ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    $response = json_decode($response, true);
    if (isset($response['details']['code']) || isset($response['details'][0]['code'])) {
        $access_token = access_token();
        return buyers_available_server($skill);
    }
    return $response['data'][0];
}

$skills = [
    'AL' => [266865, 266607],
    'AR' => [266866, 266609],
    'AZ' => [266867, 266615],
    'CA' => [266868, 266611],
    'CO' => [266869, 266613],
    'CT' => [266870, 266617],
    'DE' => [266872, 266621],
    'FL' => [266873, 266623],
    'GA' => [266874, 266625],
    'IA' => [266876, 266629],
    'ID' => [266877, 266631],
    'IL' => [266878, 266633],
    'IN' => [266879, 266737],
    'KS' => [266880, 266635],
    'KY' => [266881, 266637],
    'LA' => [266882, 266639],
    'ME' => [266885, 266645],
    'MI' => [266886, 266647],
    'MN' => [266887, 266649],
    'MO' => [266888, 266651],
    'MS' => [266889, 266653],
    'MT' => [266890, 266655],
    'NC' => [266891, 266657],
    'NE' => [266893, 266661],
    'NH' => [266894, 266663],
    'NM' => [266896, 266667],
    'NV' => [266897, 266669],
    'NY' => [266898, 266671],
    'OH' => [266899, 266673],
    'OK' => [266900, 266675],
    'OR' => [266901, 266677],
    'PA' => [266902, 266679],
    'RI' => [266903, 266739],
    'SC' => [266904, 266716],
    'TN' => [266906, 266683],
    'TX' => [266907, 266685],
    'UT' => [266908, 266687],
    'VA' => [266909, 266689],
    'WA' => [266911, 266714],
    'WI' => [266912, 266693],
    'WV' => [266913, 266695],

    // states with a single entry
    'AK' => [266605],
    'DC' => [266619],
    'HI' => [266627],
    'MA' => [266641],
    'MD' => [266643],
    'ND' => [266659],
    'NJ' => [266665],
    'SD' => [266681],
    'VT' => [266691],
    'WY' => [266718],
];

$mm_state        = (isset($_GET['asbon_state']) && $_GET['asbon_state'] != '') ? $_GET['asbon_state'] : '';
$ids = $skills[$mm_state];
$skill = '{"skillId":' . implode(',', $ids) . '}';
$response = buyers_available_server($skill);

$msg = '<div class="result">';
$msg .= "<strong>Available Agents: </strong> " . $response['availableAgents'] . "<br>";
$msg .= "<strong>Agents Ready For Voice: </strong> " . $response['agentsReadyForVoice'] . " <br>";
$msg .= '</div>';

$data = [
    "buyer_asb_cb" => $msg,
    "response" => $response,
];

header('Content-Type: application/json');
echo json_encode($data);
