<?php
// Alternative: Create database via PHP
if(isset($_POST['submit'])) {
    $db_host = $_POST['db_host'];
    $db_user = $_POST['db_user'];
    $db_pass = $_POST['db_pass'];
    $db_name = 'onesbd_LMS';
    
    try {
        // Connect without database first
        $pdo = new PDO("mysql:host=$db_host", $db_user, $db_pass);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        // Create database
        $pdo->exec("CREATE DATABASE IF NOT EXISTS `$db_name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
        $pdo->exec("USE `$db_name`");
        
        // Create tables
        $pdo->exec("
            CREATE TABLE IF NOT EXISTS users (
                id INT PRIMARY KEY AUTO_INCREMENT,
                full_name VARCHAR(100) NOT NULL,
                email VARCHAR(100) UNIQUE NOT NULL,
                password VARCHAR(255) NOT NULL,
                phone VARCHAR(20),
                avatar VARCHAR(255) DEFAULT 'default-avatar.png',
                role ENUM('student','instructor','admin') DEFAULT 'student',
                bio TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        ");
        
        // Add more tables as needed...
        
        // Create includes/config.php
        $config = "<?php
session_start();
define('DB_HOST', '$db_host');
define('DB_NAME', '$db_name');
define('DB_USER', '$db_user');
define('DB_PASS', '$db_pass');
define('BASE_URL', 'https://onesbd.com/LMS/');
date_default_timezone_set('Asia/Dhaka');

try {
    \$pdo = new PDO(\"mysql:host=\" . DB_HOST . \";dbname=\" . DB_NAME . \";charset=utf8mb4\", DB_USER, DB_PASS);
    \$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException \$e) {
    die(\"Connection failed\");
}

function isLoggedIn() { return isset(\$_SESSION['user_id']); }
function getUserData(\$pdo, \$id) { \$s = \$pdo->prepare(\"SELECT * FROM users WHERE id = ?\"); \$s->execute([\$id]); return \$s->fetch(); }
function getYouTubeId(\$url) { preg_match('/(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([a-zA-Z0-9_-]+)/', \$url, \$m); return \$m[1] ?? null; }
function isEnrolled(\$pdo, \$cid, \$uid) { if(!isset(\$_SESSION['user_id'])) return false; \$s = \$pdo->prepare(\"SELECT COUNT(*) FROM enrollments WHERE user_id=? AND course_id=? AND payment_status='completed'\"); \$s->execute([\$uid, \$cid]); return \$s->fetchColumn() > 0; }
?>";
        
        if(!is_dir('includes')) mkdir('includes', 0755);
        file_put_contents('includes/config.php', $config);
        
        // Create other directories
        foreach(['uploads', 'uploads/avatars', 'assets', 'assets/css'] as $dir) {
            if(!is_dir($dir)) mkdir($dir, 0755, true);
        }
        
        echo "<div style='color:green;padding:20px;'>";
        echo "<h2>✅ Setup Complete!</h2>";
        echo "<p>Database: $db_name created</p>";
        echo "<p>Config file created</p>";
        echo "<p><a href='index.php'>Go to Homepage</a></p>";
        echo "<p style='color:red'>Delete setup.php now!</p>";
        echo "</div>";
        
    } catch(PDOException $e) {
        echo "<div style='color:red;padding:20px;'>";
        echo "<h2>❌ Error</h2>";
        echo "<p>" . $e->getMessage() . "</p>";
        echo "</div>";
    }
} else {
?>
    <form method="POST" style="max-width:400px;margin:50px auto;padding:20px;">
        <h2>Database Setup</h2>
        <p>Enter your cPanel MySQL credentials:</p>
        <input type="text" name="db_host" value="localhost" required class="form-control mb-2">
        <input type="text" name="db_user" placeholder="Database Username" required class="form-control mb-2">
        <input type="password" name="db_pass" placeholder="Database Password" required class="form-control mb-2">
        <button type="submit" name="submit" class="btn btn-primary w-100">Setup</button>
    </form>
<?php } ?>