<?php
error_reporting(E_ALL); ini_set('display_errors',1);
$wp = file_get_contents('/home/monara/public_html/Hotel_Sapphire/wp-config.php');
preg_match("/table_prefix\s*=\s*'([^']+)'/", $wp, $pfx);
preg_match("/define\(\s*'DB_NAME'\s*,\s*'([^']+)'/", $wp, $n);
preg_match("/define\(\s*'DB_USER'\s*,\s*'([^']+)'/", $wp, $u);
preg_match("/define\(\s*'DB_PASSWORD'\s*,\s*'([^']*)'/", $wp, $p);
$pdo = new PDO("mysql:host=localhost;dbname={$n[1]};charset=utf8", $u[1], $p[1]);
$pre = $pfx[1] ?? 'wp_';
echo "prefix=$pre\n";
$tables = $pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);
$book = array_filter($tables, fn($t)=>preg_match('/book|order|pay|trans/i',$t));
echo "tables(".count($tables)."): payment/booking: ".implode(', ',$book)."\n";
$val = $pdo->query("SELECT option_value FROM {$pre}options WHERE option_name='eagle_booking_settings'")->fetchColumn();
if ($val) {
    $s = @unserialize($val);
    if (is_array($s)) {
        foreach (['payment_gateways','checkout_payment','currency','paypal_email','stripe_public_key','stripe_secret_key','bank_transfer','payment_method'] as $k) {
            if (isset($s[$k])) echo "$k: ".(is_array($s[$k])?json_encode($s[$k]):$s[$k])."\n";
        }
        echo "--- all keys: ".implode(', ', array_keys($s))."\n";
        // dump payment-related keys
        foreach ($s as $k=>$v) {
            if (preg_match('/pay|stripe|paypal|bank|gateway|checkout|currency|price/i',$k)) {
                echo "  $k => ".(is_array($v)?json_encode($v):substr((string)$v,0,200))."\n";
            }
        }
    } else echo "settings raw: ".substr($val,0,500)."\n";
}
// bookings table
foreach ($tables as $t) {
    if (stripos($t,'booking')!==false) {
        $cnt=$pdo->query("SELECT COUNT(*) FROM `$t`")->fetchColumn();
        echo "TABLE $t: $cnt rows\n";
        $cols=$pdo->query("SHOW COLUMNS FROM `$t`")->fetchAll(PDO::FETCH_COLUMN);
        echo "  cols: ".implode(', ',array_filter($cols,fn($c)=>preg_match('/pay|price|amount|total|currency|status/i',$c)))."\n";
        if ($cnt>0) {
            $r=$pdo->query("SELECT * FROM `$t` ORDER BY id DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC);
            foreach ($r as $k=>$v) if (preg_match('/pay|price|amount|total|currency|status|type|email/i',$k)) echo "  $k=$v\n";
        }
    }
}

// ratings db
echo "\n=== ratings ===\n";
$pdo2 = new PDO('mysql:host=localhost;dbname=monara_ratings_db;charset=utf8','monara_ratings_user','.{L*tHx@O9MR');
$rt = $pdo2->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);
echo "tables: ".implode(', ',$rt)."\n";
foreach ($rt as $t) if (preg_match('/pay|subscri|premium|billing/i',$t)) echo "PAY TABLE: $t\n";

// moodle check
echo "\n=== moodle config ===\n";
$mc = glob('/home/monara/public_html/moodle/config.php');
if ($mc) {
    $m = file_get_contents($mc[0]);
    if (preg_match('/payment/i',$m)) echo "moodle has payment refs\n";
    preg_match("/dbname\s*=\s*'([^']+)'/",$m,$md);
    echo "moodle db: ".($md[1]??'?')."\n";
}
