#! /usr/bin/php 
<?php
     require_once( __DIR__.'/config.inc.php' );
     require_once( __DIR__.'/model.inc.php' );


     Model::init();

    $res = Model::requestBD( "SELECT COUNT(*) FROM `unotif` WHERE service='smsmode' AND state='need_retry' ORDER BY tsCreated ASC LIMIT 100" );
    if( !$res ){
        echo "Failed to query unotif table for smsmode need_retry entries\n";

        return false;
    }else{
        $nb = $res->fetchColumn();

        if( $nb > 0 ){
            echo "Found $nb smsmode need_retry entries\n";

            // Call msg API REST https://msg.mtp-eng.fr/cron

            $ch = curl_init();
            curl_setopt( $ch, CURLOPT_URL, 'https://msg.mtp-eng.fr/cron' );
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
            $response = curl_exec( $ch );
            $httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );    
            curl_close( $ch );

            if( $httpCode != 200 ){
                $errmsg = curl_error($ch);
                echo "Failed to call msg API REST /cron (HTTP code: $httpCode) Error: '$errmsg'\n";
            }else{
                echo "Successfully called msg API REST /cron\n";
                echo "Response: $response\n";
            }
        }else{
            
        }   
    }

?>