<?php
/**
 * BLOOM IRIS — CONTACT DIAGNOSTIC
 * --------------------------------------------------------------------------
 * TEMPORARY troubleshooting file. Put this in the project root
 * (same folder as index.php) and open it in your browser:
 *     http://localhost/bloom-iris/diagnose.php
 *
 * It checks the things that make a contact form silently fail on XAMPP.
 * DELETE this file once the contact form works — do not leave it on a
 * live server.
 * --------------------------------------------------------------------------
 */

header('Content-Type: text/plain; charset=utf-8');
echo "BLOOM IRIS — CONTACT FORM DIAGNOSTIC\n";
echo str_repeat('=', 55) . "\n\n";

$base = __DIR__;
$pass = 0; $fail = 0;
function ok($msg)   { global $pass; $pass++; echo "  [ OK ]  $msg\n"; }
function bad($msg)  { global $fail; $fail++; echo "  [FAIL]  $msg\n"; }
function info($msg) { echo "  [info]  $msg\n"; }

/* 1. Session save directory ------------------------------------------- */
echo "1. SESSION STORAGE\n";
$sessDir = $base . '/application/cache/sessions';
if (is_dir($sessDir)) {
    ok("Session folder exists: application/cache/sessions");
    if (is_writable($sessDir)) {
        ok("Session folder is writable");
    } else {
        bad("Session folder is NOT writable — sessions & flash messages will fail");
        info("Fix: right-click the folder > Properties > uncheck Read-only");
    }
} else {
    bad("Session folder is MISSING: application/cache/sessions");
    info("Fix: create the folder application/cache/sessions");
}
echo "\n";

/* 2. PHP session actually works --------------------------------------- */
echo "2. PHP SESSION TEST\n";
@session_start();
$_SESSION['_biris_test'] = 'works';
if (isset($_SESSION['_biris_test']) && $_SESSION['_biris_test'] === 'works') {
    ok("PHP sessions are working");
} else {
    bad("PHP sessions are NOT working");
}
echo "\n";

/* 3. Database connection ---------------------------------------------- */
echo "3. DATABASE CONNECTION\n";
$dbFile = $base . '/application/config/database.php';
if (is_file($dbFile)) {
    $src = file_get_contents($dbFile);
    preg_match("/'hostname'\\s*=>\\s*'([^']*)'/", $src, $mH);
    preg_match("/'username'\\s*=>\\s*'([^']*)'/", $src, $mU);
    preg_match("/'password'\\s*=>\\s*'([^']*)'/", $src, $mP);
    preg_match("/'database'\\s*=>\\s*'([^']*)'/", $src, $mD);
    $h = $mH[1] ?? ''; $u = $mU[1] ?? ''; $p = $mP[1] ?? ''; $d = $mD[1] ?? '';
    info("hostname = '$h'  username = '$u'  database = '$d'");
    // PHP 8.1+ throws exceptions for mysqli errors — handle both modes.
    mysqli_report(MYSQLI_REPORT_OFF);
    $mysqli = @new mysqli($h, $u, $p, $d);
    if ($mysqli->connect_errno) {
        bad("Cannot connect: " . $mysqli->connect_error);
        info("Fix: correct the credentials in application/config/database.php");
    } else {
        ok("Connected to database '$d'");

        /* 4. inquiries table + insert test ---------------------------- */
        echo "\n4. INQUIRIES TABLE\n";
        $res = $mysqli->query("SHOW TABLES LIKE 'inquiries'");
        if ($res && $res->num_rows > 0) {
            ok("Table 'inquiries' exists");
            $cnt = $mysqli->query("SELECT COUNT(*) AS c FROM inquiries")->fetch_assoc();
            info("Current row count: " . $cnt['c']);

            /* try a real insert */
            $stmt = $mysqli->prepare(
                "INSERT INTO inquiries (type,name,email,phone,subject,message,status,ip_address,created_at)
                 VALUES ('contact','DIAGNOSTIC TEST','diag@test.local','000','Diagnostic','Inserted by diagnose.php','new','127.0.0.1',NOW())"
            );
            if ($stmt && $stmt->execute()) {
                ok("TEST INSERT succeeded — row id " . $mysqli->insert_id);
                info("The database accepts inquiry inserts. Check phpMyAdmin —");
                info("you should now see a 'DIAGNOSTIC TEST' row in the inquiries table.");
            } else {
                bad("TEST INSERT failed: " . $mysqli->error);
                info("This is why your messages are not saved. The error above is the reason.");
            }
        } else {
            bad("Table 'inquiries' does NOT exist in database '$d'");
            info("Fix: import database.sql into this database via phpMyAdmin");
        }
        $mysqli->close();
    }
} else {
    bad("application/config/database.php not found");
}
echo "\n";

/* 5. Summary ---------------------------------------------------------- */
echo str_repeat('=', 55) . "\n";
echo "RESULT: $pass passed, $fail failed.\n";
if ($fail === 0) {
    echo "Everything checks out. If the contact form still shows no\n";
    echo "message, the issue is the form POST itself — tell the developer\n";
    echo "that all diagnostic checks passed.\n";
} else {
    echo "Fix the [FAIL] items above, then submit the contact form again.\n";
}
echo "\nDELETE this file when finished.\n";