<?php
// Direct database connection
$host = 'localhost';
$dbname = 'school_management';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
    ]);
    
    echo "<h2>Direct Database Connection</h2>";
    
    // Check if we have any teachers
    $stmt = $pdo->query("SELECT id FROM teachers LIMIT 1");
    $teacher = $stmt->fetch();
    
    if (!$teacher) {
        echo "<p>No teachers found. Creating a sample teacher...</p>";
        
        // Create a sample teacher user
        $stmt = $pdo->prepare("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, ?)");
        $password_hash = password_hash('teacher123', PASSWORD_DEFAULT);
        $stmt->execute(['teacher1', $password_hash, 'teacher1@school.com', 'teacher']);
        $userId = $pdo->lastInsertId();
        
        // Create teacher record
        $stmt = $pdo->prepare("INSERT INTO teachers (user_id, employee_id, first_name, last_name, email) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$userId, 'T001', 'জন', 'ডেভেলপার', 'teacher1@school.com']);
        $teacherId = $pdo->lastInsertId();
    } else {
        $teacherId = $teacher['id'];
        echo "<p>Found teacher with ID: " . $teacherId . "</p>";
    }
    
    // Insert sample assignment
    $stmt = $pdo->prepare("
        INSERT INTO assignments (title, description, class_id, subject_id, teacher_id, due_date, max_marks, status) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    ");
    
    $result = $stmt->execute([
        'গণিত - বীজগণিত', // title
        'অধ্যায় ৩ - বহুপদী সমীকরণ', // description
        11, // class_id (একাদশ)
        3, // subject_id (গণিত)
        $teacherId, // teacher_id
        '2025-10-25 23:59:59', // due_date
        20, // max_marks
        'published' // status
    ]);
    
    if ($result) {
        $assignmentId = $pdo->lastInsertId();
        echo "<p style='color: green;'>Sample assignment created successfully with ID: " . $assignmentId . "</p>";
    } else {
        echo "<p style='color: red;'>Failed to create sample assignment</p>";
    }
    
    // Check assignments
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM assignments");
    $count = $stmt->fetch();
    echo "<p>Total assignments: " . $count['count'] . "</p>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}
?>