<?php

preg_match("/^agrobr-app02\.*/", gethostname(), $matches);

require_once('Agro_Curl.php');

$now = new \Datetime;

try {
    if (!isset($argv[1])) {
        throw new \Exception('Parâmetro obrigatório não informado.');
    }

    if (in_array(strtolower(explode('.', basename($argv[1]))[1]), ['bmp', 'jpg', 'png', 'gif', 'jpeg', 'jfif', 'tiff', 'avif'])) {
        list($idProposta, $tipo, $idVistoria, $quadraExtension) = explode('_', basename($argv[1]));
    }

    if (empty($idProposta) || empty($idVistoria)) {
        throw new \Exception('Dados necessários não foi informados!');
    }

    echo "\n{$now->format('Y-m-d H:i:s')}: Atualizando arquivos de foto para ID_PROPOSTA {$idProposta}\n";

    if (count($matches) > 0) {
        set_include_path(get_include_path().':/var/www/html/library/');
        $agro_curl = new Agro_Curl('agronet.agrobrasilseguros.com.br', 'https');
    } else {
        set_include_path(get_include_path().':/usr/share/php/:../library/');
        $agro_curl = new Agro_Curl('teste-agronet.agrobrasilseguros.com.br', 'http');
    }

    $agro_curl->setUrl('/seguro/vistoriaprevia/atualizastatusarquivosfoto');
    $agro_curl->doRequest('POST', array('idProposta' => $idProposta, 'idVistoria' => $idVistoria));

    $response = $agro_curl->getResult();
    
    echo "\n{$now->format('Y-m-d H:i:s')}: {$response->message}\n";
} catch (\Exception $e) {
    echo "\n{$now->format('Y-m-d H:i:s')}: {$e->getMessage()}\n";
}
