<div class="container py-4">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="<?= APP_URL ?>/mentors" class="text-decoration-none">Mentor Network</a></li>
            <li class="breadcrumb-item active" aria-current="page"><?= htmlspecialchars($mentor['first_name'] . ' ' . $mentor['last_name']) ?></li>
        </ol>
    </nav>

    <div class="row g-4">
        <!-- Mentor Info Details -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm p-4 text-center card-hover-effect mb-4 bg-glass">
                <div class="position-relative d-inline-block mx-auto mb-3">
                    <img src="<?= avatarUrl($mentor['avatar'], $mentor['first_name'] . ' ' . $mentor['last_name']) ?>" alt="Avatar" class="rounded-circle border border-4 border-white shadow" style="width: 80px; height: 80px; object-fit: cover;">
                    <?php if ($mentor['is_available']): ?>
                        <span class="position-absolute bottom-0 end-2 bg-success border border-white rounded-circle p-2" style="width: 20px; height: 20px;" title="Available"></span>
                    <?php endif; ?>
                </div>

                <h4 class="fw-bold font-outfit text-dark mb-1"><?= htmlspecialchars($mentor['first_name'] . ' ' . $mentor['last_name']) ?></h4>
                <p class="text-xs text-uppercase text-primary fw-semibold mb-3 tracking-wider">Verified Mentor</p>
                <p class="text-xs text-muted mb-2"><i class="fa-solid fa-school text-primary me-2"></i> <?= htmlspecialchars($mentor['university_name'] ?? 'University') ?></p>
                <p class="text-xs text-muted mb-3"><i class="fa-solid fa-book-open text-primary me-2"></i> <?= htmlspecialchars($mentor['program'] ?? 'Academic Program') ?></p>
                
                <?php if ($mentor['linkedin_url']): ?>
                    <a href="<?= htmlspecialchars($mentor['linkedin_url']) ?>" target="_blank" class="btn btn-sm btn-outline-primary rounded-pill px-4 py-2"><i class="fa-brands fa-linkedin me-2"></i>LinkedIn Connect</a>
                <?php endif; ?>

                <hr class="my-4 text-muted opacity-25">

                <div class="text-start text-xs text-muted">
                    <div class="d-flex justify-content-between mb-2">
                        <span>Academic Level:</span>
                        <strong class="text-dark"><?= $mentor['year_level'] ? 'Level ' . htmlspecialchars($mentor['year_level']) : 'N/A' ?></strong>
                    </div>
                    <div class="d-flex justify-content-between mb-2">
                        <span>Sessions Hosted:</span>
                        <strong class="text-dark"><?= $mentor['sessions_completed'] ?> Completed</strong>
                    </div>
                    <div class="d-flex justify-content-between">
                        <span>Mentees Count:</span>
                        <strong class="text-dark"><?= $mentor['current_mentees'] ?> / <?= $mentor['max_mentees'] ?></strong>
                    </div>
                </div>
            </div>

            <!-- Booking Section -->
            <?php if (Auth::check() && Auth::isStudent() && $mentor['is_available']): ?>
                <div class="card border-0 shadow-sm p-4 text-center">
                    <h5 class="fw-bold font-outfit mb-3"><i class="fa-regular fa-calendar-check text-primary me-2"></i>Schedule a Class</h5>
                    <p class="text-xs text-muted mb-3">Schedule a 1-on-1 virtual mentoring or academic prep class with this mentor.</p>
                    <a href="<?= APP_URL ?>/sessions/book/<?= $mentor['user_id'] ?>" class="btn btn-primary rounded-pill px-4 w-100 font-outfit fw-bold py-2 shadow-sm">Book Live Session</a>
                </div>
            <?php endif; ?>
        </div>

        <div class="col-lg-8">
            <div class="card border-0 shadow-sm p-4 mb-4">
                <h5 class="fw-bold font-outfit text-dark mb-3 border-bottom pb-2">Biography</h5>
                <p class="text-muted leading-relaxed mb-4"><?= nl2br(htmlspecialchars($mentor['bio'])) ?></p>

                <h5 class="fw-bold font-outfit text-dark mb-3 border-bottom pb-2">Expertise Areas</h5>
                <div class="d-flex flex-wrap gap-2 mb-4">
                    <?php
                    $tags = array_filter(array_map('trim', explode(',', $mentor['expertise_areas'])));
                    if (!empty($tags)):
                        foreach ($tags as $tag):
                    ?>
                            <span class="badge bg-primary-subtle text-primary rounded-pill px-3 py-2 text-xs"><?= htmlspecialchars($tag) ?></span>
                        <?php endforeach; else: ?>
                            <span class="text-xs text-muted">No specific tags specified.</span>
                    <?php endif; ?>
                </div>

                <h5 class="fw-bold font-outfit text-dark mb-3 border-bottom pb-2">Career Direction & Goals</h5>
                <p class="text-muted text-sm mb-0"><?= htmlspecialchars($mentor['career_goals'] ?: 'No details logged.') ?></p>
            </div>

            <!-- Request Mentorship Form -->
            <?php if (Auth::check() && Auth::isStudent()): ?>
                <div class="card border-0 shadow-sm p-4 border-start border-4 border-primary">
                    <h5 class="fw-bold font-outfit text-dark mb-3">Request Mentorship Relationship</h5>
                    
                    <?php if ($has_request): ?>
                        <div class="alert alert-info border-0 mb-0 d-flex align-items-center">
                            <i class="fa-solid fa-circle-notch fa-spin me-3 fs-5 text-primary"></i>
                            <div class="text-sm">
                                <strong class="d-block">Request Pending</strong>
                                <span>You have already requested this mentor. Awaiting their acceptance.</span>
                            </div>
                        </div>
                    <?php else: ?>
                        <p class="text-muted text-xs mb-4">Establishing a mentorship allows you to exchange direct chat messages, obtain university admissions advice, and schedule recurring classes.</p>
                        
                        <form action="<?= APP_URL ?>/mentors/<?= $mentor['user_id'] ?>/request" method="POST">
                            <?= csrf_field() ?>
                            
                            <div class="mb-3">
                                <label for="message" class="form-label text-sm fw-medium">Introduction Message</label>
                                <textarea class="form-control rounded-3 text-sm" id="message" name="message" rows="3" required placeholder="Introduce yourself, mention your high school, and explain why you want to connect..."></textarea>
                            </div>
                            
                            <div class="mb-4">
                                <label for="goals" class="form-label text-sm fw-medium">Your Academic Aspirations</label>
                                <textarea class="form-control rounded-3 text-sm" id="goals" name="goals" rows="2" required placeholder="What are your goals or what help do you expect from this mentor?"></textarea>
                            </div>
                            
                            <button type="submit" class="btn btn-primary rounded-pill px-5 shadow-sm font-outfit fw-bold py-2"><i class="fa-solid fa-user-plus me-2"></i>Send Connection Request</button>
                        </form>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
