<?php
// Test database connection and permissions
try {
    // Connect to MySQL
    $conn = new mysqli('localhost', 'root', '');
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected to MySQL server successfully<br>";
    
    // Check if database exists
    $result = $conn->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'real_estate_db'");
    if ($result->num_rows > 0) {
        echo "Database 'real_estate_db' exists<br>";
        
        // Select the database
        $conn->select_db('real_estate_db');
        
        // Check if table exists
        $result = $conn->query("SHOW TABLES LIKE 'contact_submissions'");
        if ($result->num_rows > 0) {
            echo "Table 'contact_submissions' exists<br>";
            
            // First, let's check the table structure
            $result = $conn->query("DESCRIBE contact_submissions");
            echo "<h3>Table Structure:</h3><pre>";
            while($row = $result->fetch_assoc()) {
                print_r($row);
            }
            echo "</pre>";
            
            // Test insert with error handling
            $testData = [
                'name' => 'Test User',
                'email' => 'test@example.com',
                'phone' => '1234567890',
                'message' => 'Test message',
                'ip_address' => '127.0.0.1',
                'user_agent' => 'Test Script',
                'status' => 'new',
                'created_at' => date('Y-m-d H:i:s')
            ];
            
            $sql = "INSERT INTO contact_submissions (name, email, phone, message, ip_address, user_agent, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            echo "<br>SQL to be executed: " . htmlspecialchars($sql) . "<br>";
            
            $stmt = $conn->prepare($sql);
            
            if ($stmt === false) {
                echo "<div style='color:red'>Error preparing statement: " . $conn->error . "</div>";
                exit;
            }
            
            $bindResult = $stmt->bind_param("ssssssss", 
                $testData['name'], 
                $testData['email'], 
                $testData['phone'], 
                $testData['message'],
                $testData['ip_address'],
                $testData['user_agent'],
                $testData['status'],
                $testData['created_at']
            );
            
            if ($bindResult === false) {
                echo "<div style='color:red'>Error binding parameters: " . $stmt->error . "</div>";
                exit;
            }
            
            if ($stmt->execute()) {
                echo "Test record inserted successfully<br>";
                echo "Database connection and permissions are working correctly!";
            } else {
                echo "Error inserting test record: " . $stmt->error . "<br>";
            }
            
            $stmt->close();
        } else {
            echo "Table 'contact_submissions' does not exist<br>";
        }
    } else {
        echo "Database 'real_estate_db' does not exist<br>";
    }
    
    $conn->close();
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
?>
