<?php
require_once 'config/database.php';

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['create_data'])) {
    try {
        $pdo->beginTransaction();

        // Create a test teacher if not exists
        $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
        $stmt->execute(['testteacher']);
        $existing_user = $stmt->fetch();

        if (!$existing_user) {
            // Create test teacher user
            $hashed_password = password_hash('teacher123', PASSWORD_DEFAULT);
            $stmt = $pdo->prepare("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, 'teacher')");
            $stmt->execute(['testteacher', $hashed_password, 'teacher@test.com']);
            $user_id = $pdo->lastInsertId();

            // Create teacher record
            $stmt = $pdo->prepare("INSERT INTO teachers (user_id, employee_id, first_name, last_name, email) VALUES (?, ?, ?, ?, ?)");
            $stmt->execute([$user_id, 'TCH0001', 'Test', 'Teacher', 'teacher@test.com']);

            $message .= "✅ টেস্ট শিক্ষক অ্যাকাউন্ট তৈরী হয়েছে<br>";
            $message .= "ইউজারনেম: testteacher<br>";
            $message .= "পাসওয়ার্ড: teacher123<br><br>";
        }

        // Create some test students
        $classes = $pdo->query("SELECT id, name, section FROM classes LIMIT 3")->fetchAll();

        if (!empty($classes)) {
            $student_count = 0;
            foreach ($classes as $class) {
                for ($i = 1; $i <= 5; $i++) { // 5 students per class
                    $student_id = $class['name'][0] . $class['section'] . str_pad($i, 3, '0', STR_PAD_LEFT);
                    $roll_number = $i;

                    // Check if student already exists
                    $stmt = $pdo->prepare("SELECT id FROM students WHERE student_id = ?");
                    $stmt->execute([$student_id]);
                    if (!$stmt->fetch()) {
                        // Get current session
                        $stmt = $pdo->prepare("SELECT id FROM academic_sessions WHERE is_current = 1 LIMIT 1");
                        $stmt->execute();
                        $session = $stmt->fetch();

                        $session_id = $session ? $session['id'] : 1;

                        $stmt = $pdo->prepare("INSERT INTO students (student_id, first_name, last_name, email, phone, class_id, session_id, roll_number, admission_date, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW(), 'active')");
                        $stmt->execute([
                            $student_id,
                            'ছাত্র' . $i,
                            'শিক্ষার্থী',
                            'student' . $i . '@test.com',
                            '017' . str_pad(mt_rand(10000000, 99999999), 8, '0', STR_PAD_LEFT),
                            $class['id'],
                            $session_id,
                            $roll_number
                        ]);
                        $student_count++;
                    }
                }
            }
            $message .= "✅ $student_count জন টেস্ট ছাত্র-ছাত্রী তৈরী হয়েছে<br>";
        } else {
            $message .= "⚠️ কোন ক্লাস পাওয়া যায়নি<br>";
        }

        $pdo->commit();

    } catch (Exception $e) {
        $pdo->rollBack();
        $error = 'ডেটা তৈরী করতে সমস্যা: ' . $e->getMessage();
    }
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>টেস্ট ডেটা তৈরী</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h3><i class="fas fa-database"></i> টেস্ট ডেটা তৈরী</h3>
                    </div>
                    <div class="card-body">
                        <p>এই পেজে টেস্ট শিক্ষক এবং কিছু ছাত্র-ছাত্রী তৈরী করা যাবে।</p>

                        <?php if ($message): ?>
                            <div class="alert alert-success">
                                <?php echo $message; ?>
                            </div>
                        <?php endif; ?>

                        <?php if ($error): ?>
                            <div class="alert alert-danger">
                                <?php echo $error; ?>
                            </div>
                        <?php endif; ?>

                        <form method="POST">
                            <input type="hidden" name="create_data" value="1">
                            <button type="submit" class="btn btn-success btn-lg">
                                <i class="fas fa-magic"></i> টেস্ট ডেটা তৈরী করুন
                            </button>
                        </form>

                        <hr>
                        <div class="text-center">
                            <a href="check_db.php" class="btn btn-info">ডেটাবেস চেক করুন</a>
                            <a href="login.php" class="btn btn-primary ms-2">লগইন করুন</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
