<?php
// config/db.php
$host = 'localhost';
$dbname = 'crs_delhie';
$username = 'root';
$password = '';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    // If the database doesn't exist, try to create it (for initial setup)
    try {
        $pdo = new PDO("mysql:host=$host", $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbname`");
        $pdo->exec("USE `$dbname`");
        
        // Now run the schema creation if tables don't exist
        $sql = file_get_contents(__DIR__ . '/database.sql');
        $pdo->exec($sql);
        
    } catch (PDOException $ex) {
        die("Connection failed: " . $ex->getMessage());
    }
}
?>
