<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require 'config.php';
require 'libraries/medoo/medoo.php';

$host = $database['remote_host'];
try {
  $mdb = new medoo([
    'database_type' => 'mysql',
    'database_name' => $database['name'],
    'server' => $host,
    'username' => $database['user'],
    'password' => $database['password'],
    'charset' => 'utf8'
  ]);

  echo "CONNECTED_REMOTE=" . $host . PHP_EOL;

  $rows = $mdb->query("SELECT id, name, COALESCE(active,0) AS active, COALESCE(deleted,0) AS deleted, CONCAT('[', COALESCE(google_ads_customer_id,''), ']') AS google_ads_customer_id FROM clients ORDER BY id ASC")->fetchAll(PDO::FETCH_ASSOC);
  echo "CLIENTS_TOTAL=" . count($rows) . PHP_EOL;
  foreach ($rows as $r) {
    echo json_encode($r, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL;
  }

  $eligible = $mdb->query("SELECT id, name FROM clients WHERE COALESCE(deleted,0)=0 AND COALESCE(active,0)=1 AND TRIM(COALESCE(google_ads_customer_id,''))<>'' ORDER BY id ASC")->fetchAll(PDO::FETCH_ASSOC);
  echo "ELIGIBLE_TOTAL=" . count($eligible) . PHP_EOL;
  foreach ($eligible as $e) {
    echo "ELIGIBLE=" . json_encode($e, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL;
  }

} catch (Throwable $e) {
  echo "ERR=" . $e->getMessage() . PHP_EOL;
}
?>
