<?php
/**
 * Temporary diagnostics for GoDaddy 500s.
 * Open: https://demo.barber-shop.us/health.php?key=demo-check
 * DELETE this file after the site works.
 */
declare(strict_types=1);

header('Content-Type: text/plain; charset=utf-8');

if (($_GET['key'] ?? '') !== 'demo-check') {
    http_response_code(403);
    echo "Forbidden. Use ?key=demo-check\n";
    exit;
}

echo "PHP " . PHP_VERSION . "\n";
echo "pdo_mysql: " . (extension_loaded('pdo_mysql') ? 'yes' : 'NO') . "\n";
echo "curl: " . (extension_loaded('curl') ? 'yes' : 'NO') . "\n";
echo "gd: " . (extension_loaded('gd') ? 'yes' : 'NO') . "\n";
echo "APP document root related: " . __DIR__ . "\n\n";

$localFile = __DIR__ . '/config/db.local.php';
echo "db.local.php: " . (is_readable($localFile) ? 'found' : 'MISSING') . "\n";

try {
    require_once __DIR__ . '/config/database.php';
    echo "DB_NAME=" . DB_NAME . "\n";
    echo "DB_USER=" . DB_USER . "\n";
    echo "APP_BASE='" . APP_BASE . "'\n";
    $pdo = new PDO(
        sprintf('mysql:host=%s;port=%s;dbname=%s;charset=utf8mb4', DB_HOST, DB_PORT, DB_NAME),
        DB_USER,
        DB_PASS,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    echo "DB connect: OK\n";
    $n = (int) $pdo->query('SELECT COUNT(*) FROM tenants')->fetchColumn();
    echo "tenants rows: {$n}\n";
    echo "OK — database side looks fine. If homepage still 500, check error log / PHP version (need 8.0+).\n";
} catch (Throwable $e) {
    echo "DB/config ERROR:\n" . $e->getMessage() . "\n";
    echo "\nTypical fixes:\n";
    echo "- Set DB_USER / DB_PASS / DB_NAME to your cPanel MySQL values (not root)\n";
    echo "- Import demobarbershop_seed.sql (or your backup) into that database\n";
    echo "- Set APP_BASE to '' (empty) when files are in the subdomain document root\n";
}
