<?php
// Enable error display temporarily for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

echo "<h2>Testing pin_auth.php Dependencies</h2>";
echo "<hr>";

// Test 1: Check if session.php exists and can be included
echo "<h3>1. Testing session.php</h3>";
$session_file = __DIR__ . '/include/session.php';
if (file_exists($session_file)) {
    echo "✓ File exists: $session_file<br>";
    try {
        include_once($session_file);
        echo "✓ session.php loaded successfully<br>";
    } catch (Exception $e) {
        echo "✗ Error loading session.php: " . $e->getMessage() . "<br>";
    }
} else {
    echo "✗ File NOT found: $session_file<br>";
}

echo "<hr>";

// Test 2: Check if class.user.php exists
echo "<h3>2. Testing class.user.php</h3>";
$class_file = __DIR__ . '/include/class.user.php';
if (file_exists($class_file)) {
    echo "✓ File exists: $class_file<br>";
    try {
        require_once($class_file);
        echo "✓ class.user.php loaded successfully<br>";
        
        // Try to instantiate USER class
        $test_user = new USER();
        echo "✓ USER class instantiated successfully<br>";
    } catch (Exception $e) {
        echo "✗ Error with class.user.php: " . $e->getMessage() . "<br>";
    }
} else {
    echo "✗ File NOT found: $class_file<br>";
}

echo "<hr>";

// Test 3: Check for error_log files
echo "<h3>3. Looking for error_log files</h3>";
$locations = [
    __DIR__,
    __DIR__ . '/dashboard',
    __DIR__ . '/include',
    dirname(__DIR__),
];

foreach ($locations as $location) {
    $error_log = $location . '/error_log';
    if (file_exists($error_log)) {
        echo "<strong style='color:green;'>FOUND: $error_log</strong><br>";
        echo "<pre style='background:#f5f5f5; padding:10px; max-height:300px; overflow:auto;'>";
        echo htmlspecialchars(file_get_contents($error_log));
        echo "</pre><br>";
    } else {
        echo "<span style='color:gray;'>Not found: $error_log</span><br>";
    }
}

echo "<hr>";

// Test 4: Check if email_login_notification.php exists
echo "<h3>4. Testing email_login_notification.php</h3>";
$email_file = __DIR__ . '/dashboard/email_login_notification.php';
if (file_exists($email_file)) {
    echo "✓ File exists: $email_file<br>";
} else {
    echo "✗ File NOT found: $email_file<br>";
    echo "<strong>This might be causing the 500 error!</strong><br>";
}

echo "<hr>";

// Test 5: Simulate pin_auth.php execution
echo "<h3>5. Simulating pin_auth.php execution</h3>";
session_start();
$_SESSION['uname'] = 'stanlee'; // Test account

try {
    $reg_user = new USER();
    $stmt = $reg_user->runQuery("SELECT * FROM account WHERE uname=:uname");
    $stmt->execute(array(":uname" => $_SESSION['uname']));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($row) {
        echo "✓ Successfully fetched user data for: " . $row['fname'] . " " . $row['lname'] . "<br>";
        echo "✓ Account has pin_auth: " . (isset($row['pin_auth']) ? 'Yes' : 'No') . "<br>";
    } else {
        echo "✗ No user found with uname: stanlee<br>";
    }
} catch (Exception $e) {
    echo "✗ Database error: " . $e->getMessage() . "<br>";
}

?>
