<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "START\n";
echo "PDO: " . (class_exists('PDO') ? 'yes' : 'no') . "\n";
try {
    $pdo = new PDO('mysql:host=localhost;dbname=monara_swarnawahini_db;charset=utf8', 'monara', 'UcKTQetxHi1[');
    echo "swarnawahini_db: OK\n";
    $t = $pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);
    echo "tables: " . count($t) . "\n";
    $pay = array_filter($t, fn($x) => preg_match('/pay|order|book|trans/i', $x));
    echo "pay tables: " . implode(',', $pay) . "\n";
} catch (Throwable $e) { echo "ERR1: " . $e->getMessage() . "\n"; }

$wp = '/home/monara/public_html/Hotel_Sapphire/wp-config.php';
if (file_exists($wp)) {
    $c = file_get_contents($wp);
    preg_match("/define\(\s*'DB_NAME'\s*,\s*'([^']+)'/", $c, $n);
    preg_match("/define\(\s*'DB_USER'\s*,\s*'([^']+)'/", $c, $u);
    preg_match("/define\(\s*'DB_PASSWORD'\s*,\s*'([^']*)'/", $c, $p);
    echo "HOTEL DB={$n[1]} USER={$u[1]}\n";
    try {
        $pdo2 = new PDO("mysql:host=localhost;dbname={$n[1]};charset=utf8", $u[1], $p[1]);
        echo "hotel_db: OK\n";
        $opts = $pdo2->query("SELECT option_name FROM wp_options WHERE option_name LIKE '%eagle%' OR option_name LIKE '%payment%' OR option_name LIKE '%stripe%' OR option_name LIKE '%paypal%' LIMIT 20")->fetchAll(PDO::FETCH_COLUMN);
        echo "wp_opts: " . implode(', ', $opts) . "\n";
        $tables = $pdo2->query("SHOW TABLES LIKE '%eb_%'")->fetchAll(PDO::FETCH_COLUMN);
        echo "eb_tables: " . implode(', ', $tables) . "\n";
        if ($tables) {
            $tname = $tables[0];
            $cols = $pdo2->query("SHOW COLUMNS FROM `$tname` LIKE '%pay%'")->fetchAll(PDO::FETCH_COLUMN);
            echo "pay_cols: " . implode(', ', $cols) . "\n";
            $cnt = $pdo2->query("SELECT COUNT(*) FROM `$tname`")->fetchColumn();
            echo "bookings: $cnt\n";
            $sample = $pdo2->query("SELECT checkout_payment_type, paypal_payment_status, COUNT(*) c FROM `$tname` GROUP BY checkout_payment_type, paypal_payment_status LIMIT 10")->fetchAll(PDO::FETCH_ASSOC);
            foreach ($sample as $s) echo "  type={$s['checkout_payment_type']} status={$s['paypal_payment_status']} n={$s['c']}\n";
        }
    } catch (Throwable $e) { echo "ERR2: " . $e->getMessage() . "\n"; }
}
echo "END\n";
