<?php

// Database connection details
$servername = "localhost";
$username = "kingschatgames_sofia";
$password = "F7XLfT6CRM";
$dbname = "kingschatgames_sofia";

// Create connection with error handling
try {
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        throw new Exception("Database connection failed: " . $conn->connect_error);
    }

    // Set character encoding
    if (!$conn->set_charset("utf8mb4")) {
        throw new Exception("Error setting character encoding: " . $conn->error);
    }

    // KingsChat API configuration
    define('KINGSCHAT_CLIENT_ID', '4b333856-61fa-4169-9c0a-a189a44dae04');
    // define('KINGSCHAT_CLIENT_SECRET', 'your_client_secret_here'); // Add this if needed
    define('KINGSCHAT_REDIRECT_URI', 'https://kingschatgames.com/kingschat_login_result.php');
    define('KINGSCHAT_API_URL', 'https://api.kingsch.at/v1/');

    // Error reporting (adjust for production)
    error_reporting(E_ALL);
    ini_set('display_errors', 0); // Set to 1 for debugging, 0 for production
    ini_set('log_errors', 1);
    ini_set('error_log', __DIR__ . '/php_errors.log');

} catch (Exception $e) {
    // Log the error securely
    error_log($e->getMessage());

    // Display user-friendly message
    die("We're experiencing technical difficulties. Please try again later.");
}

// Optional: Add helper functions here
function sanitize_input($data)
{
    global $conn;
    return $conn->real_escape_string(htmlspecialchars(trim($data)));
}
?>