<?php
require_once __DIR__ . '/../includes/bootstrap_api.php';
require_once __DIR__ . '/../includes/functions.php';

header('Content-Type: image/png');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');

$rows = $db->select('qr_code', '*', 'id = :id', '', [':id' => 1]);
$qrValue = trim((string)($rows[0]['url'] ?? ''));
if ($qrValue === '') $qrValue = 'https://wa.me/5571983834846';

try {
    require_once __DIR__ . '/../qrlib.php';
    if (class_exists('QRcode')) {
        QRcode::png($qrValue, false, QR_ECLEVEL_L, 6, 2);
        exit;
    }
} catch (Throwable $e) {
    // fallback abaixo
}

header('Location: https://image-charts.com/chart?chs=500x500&cht=qr&chl=' . urlencode($qrValue), true, 302);
exit;
