<?php
// ============================================
// إعدادات الاتصال بقاعدة البيانات - AUTO TESTER
// ============================================
class Database {
    private $host = "localhost";
private $db_name = "digitals_autotester_db";  // أو digitals_اسم_القاعدة
private $username = "digitals";               // أو digitals_اسم_المستخدم
private $password = "bassam.bassam";          // كلمة المرور الصحيحة
    private $conn;
    private static $instance = null;
    
    private function __construct() {}
    
    public static function getInstance() {
        if (self::$instance === null) {
            self::$instance = new Database();
        }
        return self::$instance;
    }
    
    public function getConnection() {
        if ($this->conn === null) {
            try {
                $this->conn = new PDO(
                    "mysql:host=" . $this->host . ";dbname=" . $this->db_name . ";charset=utf8mb4",
                    $this->username,
                    $this->password,
                    [
                        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                        PDO::ATTR_EMULATE_PREPARES => false
                    ]
                );
                $this->conn->exec("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
            } catch(PDOException $e) {
                error_log('Database connection failed: ' . $e->getMessage());
                die(json_encode([
                    'success' => false, 
                    'message' => 'Database connection failed: ' . $e->getMessage()
                ]));
            }
        }
        return $this->conn;
    }
    
    public function testConnection() {
        try {
            $this->getConnection()->query("SELECT 1");
            return true;
        } catch(Exception $e) {
            return false;
        }
    }
}

// دالة مساعدة
function getDB() {
    return Database::getInstance()->getConnection();
}
?>