<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo "START\n";
require 'config.php';
require 'libraries/medoo/medoo.php';
try {
  $mdb = new medoo([
    'database_type' => 'mysql',
    'database_name' => $database['name'],
    'server' => $database['host'],
    'username' => $database['user'],
    'password' => $database['password'],
    'charset' => 'utf8'
  ]);
  echo "CONNECTED\n";
  $cols = $mdb->query("SHOW COLUMNS FROM clients")->fetchAll(PDO::FETCH_ASSOC);
  echo "COLUMNS=" . count($cols) . "\n";
  foreach ($cols as $c) {
    echo ($c['Field'] ?? '') . "\n";
  }
  $data = $mdb->query("SELECT id, name, active, COALESCE(google_ads_customer_id,'') AS google_ads_customer_id FROM clients ORDER BY id ASC")->fetchAll(PDO::FETCH_ASSOC);
  echo "ROWS=" . count($data) . "\n";
  foreach ($data as $r) {
    echo json_encode($r, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . "\n";
  }
  $eligible = $mdb->query("SELECT id FROM clients WHERE COALESCE(deleted,0)=0 AND active=1 AND google_ads_customer_id IS NOT NULL AND google_ads_customer_id<>'' ORDER BY id ASC")->fetchAll(PDO::FETCH_COLUMN);
  echo "ELIGIBLE=" . json_encode($eligible) . "\n";
} catch (Throwable $e) {
  echo "ERR: " . $e->getMessage() . "\n";
}
?>
