<?php
// echo password_hash("admin123", PASSWORD_DEFAULT);
$host = "localhost";
$port = "3307"; 
$dbname = "vaxjrkko_hessar_college";
$username = "vaxjrkko_hessar_admin";
$password = "?xM[m3^nwF+H_^@J"; // 
try {
    $pdo = new PDO(
        "mysql:host=$host;port=$port;dbname=$dbname;charset=utf8mb4",
        $username,
        $password
    );

    

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // ✅ TEST CONNECTION
    // echo "Connected successfully";

    // Ensure required tables exist (idempotent)
    $schemaFile = __DIR__ . '/database.sql';
    if (file_exists($schemaFile)) {
        try {
            $sql = file_get_contents($schemaFile);
            // Remove SQL comments
            $sql = preg_replace('/^\s*--.*$/m', '', $sql);
            // Split into individual statements by semicolon followed by newline
            $statements = preg_split('/;\s*[\r\n]+/', $sql);
            foreach ($statements as $statement) {
                $statement = trim($statement);
                if ($statement) {
                    $pdo->exec($statement);
                }
            }
        } catch (Throwable $e) {
            // Silent fail to avoid breaking the app if schema sync fails
        }
    }

} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}
?>
