<?php

$captain_id = getenv('CAPTAIN_ID');

// Replaces dashes in keys with underscores
foreach($args as $index => $arg) {
	$split = strpos($arg, "=");
	if ( $split ) {
		$key = str_replace('-', '_', substr( $arg , 0, $split ) );
		$value = substr( $arg , $split, strlen( $arg ) );

		// Removes unnecessary bash quotes
		$value = trim( $value,'"' ); 				// Remove last quote 
		$value = str_replace( '="', '=', $value );  // Remove quote right after equals

		$args[$index] = $key.$value;
	} else {
		$args[$index] = str_replace('-', '_', $arg);
	}

}

// Converts --arguments into $arguments
parse_str( implode( '&', $args ), $arguments );
$site        = $arguments['site'];
$environment = $arguments['environment'];
$storage     = $arguments['storage'];
$visits      = $arguments['visits'];

// Fetch site details
$site_details   = json_decode( shell_exec( "captaincore site get $site --captain-id=$captain_id" ) );
$environment_id = ( new CaptainCore\Site( $site_details->site_id ) )->fetch_environment_id( $environment );

$environment_update = [
    "environment_id" => $environment_id,
    "storage"        => $storage,
    "visits"         => $visits,
    "updated_at"     => date("Y-m-d H:i:s"),
];

$json        = $_SERVER['HOME'] . "/.captaincore/config.json";
$config_data = json_decode ( file_get_contents( $json ) );
$system      = $config_data[0]->system;

foreach($config_data as $config) {
	if ( isset( $config->captain_id ) and $config->captain_id == $captain_id ) {
		$configuration = $config;
		break;
	}
}

// Update current environment with new data.
( new CaptainCore\Environments )->update( $environment_update, [ "environment_id" => $environment_id ] );

if ( ! empty ( $system->captaincore_standby ) && $system->captaincore_standby == "true" ) {
	echo "Standby mode, local update only: " . json_encode( $environment_update ) . "\n";
	return;
}

// Prepare request to API
$request = [
    'method'  => 'POST',
    'headers' => [ 'Content-Type' => 'application/json' ],
    'body'    => json_encode( [ 
        "command" => "usage-update",
        "site_id" => $site_details->site_id,
        "token"   => $configuration->keys->token,
        "data"    => $environment_update,
    ] ),
];

if ( ! empty ( $system->captaincore_dev ) && $system->captaincore_dev == "true" ) {
    $request['sslverify'] = false;
}

// Post to CaptainCore API
$response = wp_remote_post( $configuration->vars->captaincore_api, $request );
echo $response['body'];