<?php
header('Content-Type: text/plain');
$out = [];

// 1. List DBs via known creds
$configs = [
    ['host'=>'localhost','db'=>'monara_swarnawahini_db','user'=>'monara','pass'=>'UcKTQetxHi1['],
    ['host'=>'localhost','db'=>'monara_ratings_db','user'=>'monara_ratings_user','pass'=>'.{L*tHx@O9MR'],
];

foreach ($configs as $c) {
    try {
        $pdo = new PDO("mysql:host={$c['host']};dbname={$c['db']};charset=utf8", $c['user'], $c['pass']);
        $out[] = "=== DB: {$c['db']} ===";
        $tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
        $pay_tables = array_filter($tables, fn($t) => preg_match('/pay|book|order|trans|invoice|bill|stripe|gateway/i', $t));
        $out[] = "payment-related tables: " . implode(', ', $pay_tables ?: ['none']);
        foreach ($pay_tables as $t) {
            $cnt = $pdo->query("SELECT COUNT(*) FROM `$t`")->fetchColumn();
            $out[] = "  $t: $cnt rows";
        }
    } catch (Exception $e) {
        $out[] = "DB {$c['db']}: ERR " . $e->getMessage();
    }
}

// 2. Try read hotel wp-config DB name
$wpc = @file_get_contents('/home/monara/public_html/Hotel_Sapphire/wp-config.php');
if ($wpc) {
    preg_match("/DB_NAME',\s*'([^']+)'/", $wpc, $m1);
    preg_match("/DB_USER',\s*'([^']+)'/", $wpc, $m2);
    preg_match("/DB_PASSWORD',\s*'([^']+)'/", $wpc, $m3);
    $out[] = "\n=== Hotel_Sapphire wp-config ===";
    $out[] = "DB: " . ($m1[1]??'?') . " USER: " . ($m2[1]??'?') . " PASS: " . ($m3[1]??'?');
    if (!empty($m1[1])) {
        try {
            $pdo = new PDO("mysql:host=localhost;dbname={$m1[1]};charset=utf8", $m2[1], $m3[1]);
            $opts = $pdo->query("SELECT option_name, LEFT(option_value,200) FROM wp_options WHERE option_name LIKE '%payment%' OR option_name LIKE '%stripe%' OR option_name LIKE '%paypal%' OR option_name LIKE '%eagle%' OR option_name LIKE '%gateway%' OR option_name LIKE '%checkout%' LIMIT 30")->fetchAll(PDO::FETCH_ASSOC);
            $out[] = "WP payment options:";
            foreach ($opts as $o) $out[] = "  {$o['option_name']}: {$o['option_value']}";
            $bk = $pdo->query("SELECT paypal_payment_status, checkout_payment_type, COUNT(*) c FROM wp_eb_booking GROUP BY paypal_payment_status, checkout_payment_type")->fetchAll(PDO::FETCH_ASSOC);
            $out[] = "eagle-booking stats:";
            foreach ($bk as $b) $out[] = "  status={$b['paypal_payment_status']} type={$b['checkout_payment_type']} count={$b['c']}";
        } catch (Exception $e) {
            $out[] = "Hotel DB ERR: " . $e->getMessage();
        }
    }
}

// 3. Sites overview
$out[] = "\n=== Sites with booking/payment keywords ===";
$dirs = glob('/home/monara/public_html/*', GLOB_ONLYDIR);
foreach ($dirs as $d) {
    $name = basename($d);
    if (preg_match('/hotel|cinema|book|shop|pay|moodle|rating|commerce/i', $name)) {
        $out[] = "  $name";
    }
}

echo implode("\n", $out);
