<?php
session_start();
require_once 'config/database.php';

echo "<h1>Quick Fix for Current Teacher</h1>";

if (!isset($_SESSION['user_id'])) {
    echo "<p style='color: red;'>❌ আপনি লগইন করেননি!</p>";
    exit;
}

try {
    // Get current teacher
    $stmt = $pdo->prepare("SELECT id, first_name, last_name FROM teachers WHERE user_id = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $teacher = $stmt->fetch();

    if (!$teacher) {
        echo "<p style='color: red;'>❌ আপনার শিক্ষক রেকর্ড পাওয়া যায়নি!</p>";
        exit;
    }

    $teacher_id = $teacher['id'];
    echo "<p>শিক্ষক: {$teacher['first_name']} {$teacher['last_name']} (ID: $teacher_id)</p>";

    // Delete existing assignments for this teacher
    $stmt = $pdo->prepare("DELETE FROM class_teachers WHERE teacher_id = ?");
    $stmt->execute([$teacher_id]);
    echo "<p>পুরানো অ্যাসাইনমেন্ট মুছে ফেলা হয়েছে</p>";

    // Get all active classes
    $stmt = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active'");
    $classes = $stmt->fetchAll();

    echo "<p>উপলব্ধ ক্লাস: " . count($classes) . "</p>";

    // Assign teacher to all classes
    $assigned = 0;
    foreach ($classes as $class) {
        $stmt = $pdo->prepare("
            INSERT INTO class_teachers (class_id, teacher_id, assignment_type, status, assigned_at)
            VALUES (?, ?, 'primary', 'active', NOW())
        ");
        $stmt->execute([$class['id'], $teacher_id]);
        $assigned++;
    }

    echo "<p style='color: green;'>✅ $assigned টি ক্লাসে অ্যাসাইন করা হয়েছে</p>";

    // Check if there are students in these classes
    $stmt = $pdo->prepare("
        SELECT COUNT(*) as student_count
        FROM students s
        JOIN class_teachers ct ON s.class_id = ct.class_id
        WHERE ct.teacher_id = ? AND ct.status = 'active' AND s.status = 'active'
    ");
    $stmt->execute([$teacher_id]);
    $student_count = $stmt->fetch()['student_count'];

    echo "<p>অ্যাক্সেসযোগ্য ছাত্র-ছাত্রী: $student_count</p>";

    if ($student_count == 0) {
        echo "<p style='color: orange;'>⚠️ কোনো ছাত্র-ছাত্রী নেই। নমুনা ছাত্র-ছাত্রী তৈরি করা হচ্ছে...</p>";

        // Create sample students in first class
        $stmt = $pdo->query("SELECT id FROM classes WHERE status = 'active' LIMIT 1");
        $class = $stmt->fetch();

        if ($class) {
            $sample_students = [
                ['first_name' => 'রাহুল', 'last_name' => 'দাস', 'student_id' => '2024001', 'roll_number' => '1'],
                ['first_name' => 'প্রিয়া', 'last_name' => 'চৌধুরী', 'student_id' => '2024002', 'roll_number' => '2'],
                ['first_name' => 'আরিফ', 'last_name' => 'হোসেন', 'student_id' => '2024003', 'roll_number' => '3'],
            ];

            foreach ($sample_students as $student) {
                $stmt = $pdo->prepare("
                    INSERT INTO students (
                        class_id, first_name, last_name, student_id, roll_number,
                        session, phone, email, status, created_at
                    ) VALUES (
                        ?, ?, ?, ?, ?, '2024-2025', '01700000001', 'student@school.com',
                        'active', NOW()
                    )
                ");
                $stmt->execute([
                    $class['id'], $student['first_name'], $student['last_name'],
                    $student['student_id'], $student['roll_number']
                ]);
            }

            echo "<p style='color: green;'>✅ নমুনা ছাত্র-ছাত্রী তৈরি করা হয়েছে</p>";
            $student_count = count($sample_students);
        }
    }

    echo "<h2>সমাধান সম্পূর্ণ!</h2>";
    echo "<div style='background: #d4edda; color: #155724; padding: 15px; border-radius: 5px; margin: 10px 0;'>";
    echo "<p>✅ শিক্ষক সব ক্লাসে অ্যাসাইন হয়েছে</p>";
    echo "<p>✅ $student_count জন ছাত্র-ছাত্রী অ্যাক্সেসযোগ্য</p>";
    echo "<p>🎉 এখন my_students.php সঠিকভাবে কাজ করবে!</p>";
    echo "</div>";

    echo "<p><a href='teachers/my_students.php' style='background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;'>My Students পেজে যান</a></p>";

} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}
?>
