<?php
// Minimal compatibility db helper - uses App\Database PDO connection
require_once __DIR__ . '/../vendor/autoload.php';

$envFile = __DIR__ . '/../.env';
if (file_exists($envFile)) {
  $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
  $dotenv->safeLoad();
}

use App\Database;

$pdo = Database::getConnection();

function db_is_pdo(): bool    { return isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof PDO; }

function db_fetch_one(string $sql, array $params = []): ?array {
  if (db_is_pdo()) {
    $pdo = $GLOBALS['pdo'];
    $st = $pdo->prepare($sql);
    $st->execute($params);
    $row = $st->fetch(PDO::FETCH_ASSOC);
    return $row ?: null;
  }
  return null;
}

function db_exec(string $sql, array $params = []): bool {
  if (db_is_pdo()) {
    $pdo = $GLOBALS['pdo'];
    $st = $pdo->prepare($sql);
    return $st->execute($params);
  }
  return false;
}
