<?php
/**
 * Diagnostic script for parent registration page
 */

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Parent Registration Page Diagnostic</h2>";

// Check if database connection works
echo "<h3>1. Testing Database Connection...</h3>";
try {
    require_once __DIR__ . '/config/database.php';
    echo "<p style='color: green;'>✅ Database connection successful</p>";
    
    // Check if required tables exist
    echo "<h3>2. Checking Required Tables...</h3>";
    
    // Check users table
    $users_check = $pdo->query("SHOW TABLES LIKE 'users'");
    if ($users_check->rowCount() > 0) {
        echo "<p style='color: green;'>✅ users table exists</p>";
    } else {
        echo "<p style='color: red;'>❌ users table does NOT exist</p>";
    }
    
    // Check parents table
    $parents_check = $pdo->query("SHOW TABLES LIKE 'parents'");
    if ($parents_check->rowCount() > 0) {
        echo "<p style='color: green;'>✅ parents table exists</p>";
    } else {
        echo "<p style='color: red;'>❌ parents table does NOT exist - this is required for parent registration</p>";
    }
    
    // Check login_attempts table
    $login_attempts_check = $pdo->query("SHOW TABLES LIKE 'login_attempts'");
    if ($login_attempts_check->rowCount() > 0) {
        echo "<p style='color: green;'>✅ login_attempts table exists</p>";
    } else {
        echo "<p style='color: orange;'>⚠️  login_attempts table does NOT exist - creating it...</p>";
        try {
            $pdo->exec("
                CREATE TABLE login_attempts (
                    id BIGINT AUTO_INCREMENT PRIMARY KEY,
                    username VARCHAR(50),
                    ip_address VARCHAR(45) NOT NULL,
                    user_agent VARCHAR(255),
                    attempt_status ENUM('success', 'failed') NOT NULL,
                    failure_reason VARCHAR(255),
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    INDEX idx_ip_time (ip_address, created_at),
                    INDEX idx_username (username)
                )
            ");
            echo "<p style='color: green;'>✅ login_attempts table created successfully</p>";
        } catch (Exception $e) {
            echo "<p style='color: red;'>❌ Could not create login_attempts table: " . $e->getMessage() . "</p>";
        }
    }
    
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Database connection failed: " . $e->getMessage() . "</p>";
    die();
}

echo "<h3>3. Checking SecurityHelper...</h3>";
if (file_exists(__DIR__ . '/includes/SecurityHelper.php')) {
    echo "<p style='color: green;'>✅ SecurityHelper.php exists</p>";
} else {
    echo "<p style='color: red;'>❌ SecurityHelper.php does NOT exist</p>";
}

echo "<h3>4. Checking parent_register.php file...</h3>";
if (file_exists(__DIR__ . '/parent_register.php')) {
    $size = filesize(__DIR__ . '/parent_register.php');
    $content = file_get_contents(__DIR__ . '/parent_register.php');
    echo "<p style='color: green;'>✅ parent_register.php exists (Size: {$size} bytes)</p>";
    
    // Check for critical functions
    if (strpos($content, 'SecurityHelper::validatePost()') !== false) {
        echo "<p style='color: green;'>✅ SecurityHelper::validatePost() found</p>";
    } else {
        echo "<p style='color: red;'>❌ SecurityHelper::validatePost() NOT found</p>";
    }
    
    if (strpos($content, 'sanitize(') !== false) {
        echo "<p style='color: green;'>✅ sanitize function found</p>";
    } else {
        echo "<p style='color: red;'>❌ sanitize function NOT found</p>";
    }
} else {
    echo "<p style='color: red;'>❌ parent_register.php does NOT exist</p>";
}

echo "<h3>5. Testing basic functionality...</h3>";
try {
    // Test if basic database queries work
    $test_query = $pdo->query("SELECT 1 as test");
    $result = $test_query->fetch();
    if ($result && $result['test'] == 1) {
        echo "<p style='color: green;'>✅ Basic database query works</p>";
    } else {
        echo "<p style='color: red;'>❌ Basic database query failed</p>";
    }
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Basic database query failed: " . $e->getMessage() . "</p>";
}

echo "<h3>6. Summary and Recommendations:</h3>";
echo "<ul>";
echo "<li>If the parents table doesn't exist, run the database migration script</li>";
echo "<li>Make sure MAMP's Apache and MySQL services are running</li>";
echo "<li>Try accessing http://localhost/bidda/debug_parent_register.php first to see detailed diagnostics</li>";
echo "<li>Check your PHP error logs for more specific error details</li>";
echo "</ul>";

?>