<?php
// instructor-profile.php - Single Instructor Profile
require_once 'includes/config.php';

$instructor_id = $_GET['id'] ?? 0;

$stmt = $pdo->prepare("
    SELECT u.*, 
           COUNT(DISTINCT c.id) as course_count,
           COUNT(DISTINCT e.user_id) as student_count,
           COALESCE(AVG(r.rating), 0) as avg_rating,
           COUNT(DISTINCT r.id) as review_count
    FROM users u
    LEFT JOIN courses c ON u.id = c.instructor_id AND c.status = 'published'
    LEFT JOIN enrollments e ON c.id = e.course_id AND e.payment_status = 'completed'
    LEFT JOIN reviews r ON c.id = r.course_id
    WHERE u.id = ? AND u.role = 'instructor'
    GROUP BY u.id
");
$stmt->execute([$instructor_id]);
$instructor = $stmt->fetch();

if(!$instructor) {
    header('Location: instructors.php');
    exit;
}

// Get instructor courses
$stmt = $pdo->prepare("
    SELECT c.*, 
           COUNT(e.id) as enrollment_count,
           COALESCE(AVG(r.rating),0) as avg_rating,
           COUNT(r.id) as review_count
    FROM courses c
    LEFT JOIN enrollments e ON c.id = e.course_id AND e.payment_status = 'completed'
    LEFT JOIN reviews r ON c.id = r.course_id
    WHERE c.instructor_id = ? AND c.status = 'published'
    GROUP BY c.id
    ORDER BY c.created_at DESC
");
$stmt->execute([$instructor_id]);
$courses = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title><?= htmlspecialchars($instructor['full_name']) ?> - বাংলা LMS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root { --primary: #6C5CE7; --gradient: linear-gradient(135deg, #667eea, #764ba2); }
        body { background:#f0f2f8; font-family:'Segoe UI',Arial,sans-serif; }
        .container { max-width:1000px; margin:0 auto; padding:40px 20px; }
        .profile-card { background:white; border-radius:24px; padding:40px; box-shadow:0 10px 40px rgba(0,0,0,0.1); text-align:center; margin-bottom:30px; }
        .avatar { width:150px; height:150px; border-radius:50%; object-fit:cover; border:5px solid var(--primary); }
        .course-card { background:white; border-radius:15px; padding:20px; margin-bottom:15px; transition:0.3s; }
        .course-card:hover { transform:translateY(-3px); box-shadow:0 5px 20px rgba(0,0,0,0.1); }
    </style>
</head>
<body>
<div class="container">
    <a href="instructors.php" class="btn btn-outline-primary rounded-pill mb-3">← সকল ইন্সট্রাক্টর</a>
    
    <div class="profile-card">
        <img src="uploads/avatars/<?= $instructor['avatar']?:'default-avatar.png' ?>" class="avatar"
             onerror="this.src='https://ui-avatars.com/api/?name=<?=urlencode($instructor['full_name'])?>&background=6C5CE7&color=fff&size=150'">
        <h2 class="mt-3"><?= htmlspecialchars($instructor['full_name']) ?></h2>
        <p class="text-muted"><?= htmlspecialchars($instructor['designation'] ?? 'ইন্সট্রাক্টর') ?></p>
        <p><?= nl2br(htmlspecialchars($instructor['bio'] ?? '')) ?></p>
        <div class="row mt-4">
            <div class="col-4"><h4><?= $instructor['course_count'] ?></h4><small>কোর্স</small></div>
            <div class="col-4"><h4><?= number_format($instructor['student_count']) ?></h4><small>শিক্ষার্থী</small></div>
            <div class="col-4"><h4>⭐ <?= round($instructor['avg_rating'],1) ?></h4><small>রেটিং</small></div>
        </div>
    </div>
    
    <h4 class="mb-3">📚 এই ইন্সট্রাক্টরের কোর্স</h4>
    <?php foreach($courses as $c): ?>
        <div class="course-card d-flex justify-content-between align-items-center">
            <div>
                <strong><?= htmlspecialchars($c['title_bn']) ?></strong><br>
                <small><?= $c['enrollment_count'] ?> শিক্ষার্থী | ⭐ <?= round($c['avg_rating'],1) ?></small>
            </div>
            <a href="course-details.php?id=<?= $c['id'] ?>" class="btn btn-primary btn-sm rounded-pill">দেখুন</a>
        </div>
    <?php endforeach; ?>
</div>
</body>
</html>