<?php
/**
 * Admin User Detail View
 * Comprehensive profile view of a single user with admin controls.
 */
$roleColors = ['student' => 'primary', 'mentor' => 'success', 'tutor' => 'purple', 'admin' => 'danger'];
$roleColor = $roleColors[$user['role']] ?? 'secondary';
?>
<div class="container-fluid py-4 px-4">
    <!-- Breadcrumb -->
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="<?= APP_URL ?>/admin" class="text-decoration-none text-primary">Admin</a></li>
            <li class="breadcrumb-item"><a href="<?= APP_URL ?>/admin/users" class="text-decoration-none text-primary">Users</a></li>
            <li class="breadcrumb-item active text-muted"><?= htmlspecialchars($user['full_name']) ?></li>
        </ol>
    </nav>

    <div class="row g-4">
        <!-- Left: Profile Summary -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body p-4 text-center">
                    <div class="position-relative d-inline-block mb-3">
                        <img src="<?= !empty($user['avatar']) ? htmlspecialchars($user['avatar']) : APP_URL . '/assets/img/default-avatar.png' ?>" 
                             alt="<?= htmlspecialchars($user['full_name']) ?>" 
                             class="rounded-circle border border-4 border-white shadow" width="100" height="100" style="object-fit:cover;">
                        <span class="position-absolute bottom-0 end-0 badge bg-<?= ($user['status'] === 'active') ? 'success' : 'secondary' ?> rounded-circle p-1" style="width:18px;height:18px;"></span>
                    </div>
                    <h4 class="fw-bold font-outfit text-dark mb-1"><?= htmlspecialchars($user['full_name']) ?></h4>
                    <p class="text-muted mb-2"><?= htmlspecialchars($user['email']) ?></p>
                    <span class="badge bg-<?= $roleColor ?>-subtle text-<?= $roleColor ?> rounded-pill font-outfit fw-bold px-3 py-2 mb-3"><?= ucfirst($user['role']) ?></span>

                    <div class="d-flex justify-content-center gap-2 mt-2 flex-wrap">
                        <a href="<?= APP_URL ?>/admin/users/<?= $user['id'] ?>/edit" class="btn btn-primary rounded-pill px-4 font-outfit fw-bold btn-sm">
                            <i class="fa-solid fa-pen me-1"></i>Edit
                        </a>
                        <form action="<?= APP_URL ?>/admin/users/<?= $user['id'] ?>/toggle" method="POST">
                            <?= csrf_field() ?>
                            <button class="btn btn-<?= ($user['status'] === 'active') ? 'warning' : 'success' ?> rounded-pill px-4 font-outfit fw-bold btn-sm">
                                <i class="fa-solid fa-<?= ($user['status'] === 'active') ? 'ban' : 'circle-check' ?> me-1"></i><?= ($user['status'] === 'active') ? 'Deactivate' : 'Activate' ?>
                            </button>
                        </form>
                        <form action="<?= APP_URL ?>/admin/users/<?= $user['id'] ?>/delete" method="POST" onsubmit="return confirm('Permanently delete this user? This action cannot be undone.')">
                            <?= csrf_field() ?>
                            <button class="btn btn-danger rounded-pill px-4 font-outfit fw-bold btn-sm">
                                <i class="fa-solid fa-trash me-1"></i>Delete
                            </button>
                        </form>
                    </div>
                </div>
            </div>

            <!-- Account Stats -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body p-4">
                    <h6 class="fw-bold font-outfit text-dark mb-3"><i class="fa-solid fa-chart-simple text-primary me-2"></i>Account Stats</h6>
                    <ul class="list-unstyled mb-0 text-sm">
                        <li class="d-flex justify-content-between py-2 border-bottom"><span class="text-muted">Joined</span><span class="fw-bold text-dark"><?= formatDate($user['created_at']) ?></span></li>
                        <li class="d-flex justify-content-between py-2 border-bottom"><span class="text-muted">Status</span><span class="fw-bold text-dark"><?= ucfirst($user['status']) ?></span></li>
                        <li class="d-flex justify-content-between py-2 border-bottom"><span class="text-muted">Last Login</span><span class="fw-bold text-dark"><?= !empty($user['last_login']) ? formatDate($user['last_login']) : 'Never' ?></span></li>
                        <?php if ($user['role'] === 'student'): ?>
                        <li class="d-flex justify-content-between py-2 border-bottom"><span class="text-muted">Sessions</span><span class="fw-bold text-dark"><?= $activityStats['session_count'] ?? 0 ?></span></li>
                        <li class="d-flex justify-content-between py-2 border-bottom"><span class="text-muted">Messages</span><span class="fw-bold text-dark"><?= $activityStats['message_count'] ?? 0 ?></span></li>
                        <li class="d-flex justify-content-between py-2"><span class="text-muted">Checklist</span><span class="fw-bold text-dark"><?= $activityStats['checklist_completed'] ?? 0 ?> done</span></li>
                        <?php elseif ($user['role'] === 'mentor'): ?>
                        <li class="d-flex justify-content-between py-2 border-bottom"><span class="text-muted">Students Mentored</span><span class="fw-bold text-dark"><?= $activityStats['students_count'] ?? 0 ?></span></li>
                        <li class="d-flex justify-content-between py-2 border-bottom"><span class="text-muted">Sessions Held</span><span class="fw-bold text-dark"><?= $activityStats['session_count'] ?? 0 ?></span></li>
                        <li class="d-flex justify-content-between py-2"><span class="text-muted">Avg Rating</span><span class="fw-bold text-warning"><?= number_format($activityStats['avg_rating'] ?? 0, 1) ?> ⭐</span></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </div>

            <!-- Approve / Reject if Pending -->
            <?php if (!empty($user['is_pending_approval'])): ?>
            <div class="card border-0 shadow-sm border-start border-4 border-warning mb-4">
                <div class="card-body p-4">
                    <h6 class="fw-bold font-outfit text-warning mb-3"><i class="fa-solid fa-clock me-2"></i>Pending Approval</h6>
                    <p class="text-muted text-sm mb-3">This mentor account is awaiting verification and approval before they can be matched with students.</p>
                    <div class="d-flex gap-2">
                        <form action="<?= APP_URL ?>/admin/users/<?= $user['id'] ?>/approve" method="POST" class="flex-grow-1">
                            <?= csrf_field() ?>
                            <button class="btn btn-success rounded-pill w-100 font-outfit fw-bold"><i class="fa-solid fa-check me-1"></i>Approve</button>
                        </form>
                        <form action="<?= APP_URL ?>/admin/users/<?= $user['id'] ?>/reject" method="POST" class="flex-grow-1">
                            <?= csrf_field() ?>
                            <button class="btn btn-outline-danger rounded-pill w-100 font-outfit fw-bold"><i class="fa-solid fa-times me-1"></i>Reject</button>
                        </form>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        </div>

        <!-- Right: Detailed Info -->
        <div class="col-lg-8">
            <!-- Profile Info -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body p-4">
                    <h5 class="fw-bold font-outfit text-dark mb-4"><i class="fa-solid fa-id-card text-info me-2"></i>Profile Information</h5>
                    <div class="row g-3">
                        <?php
                        $fields = [
                            ['label' => 'Full Name',   'value' => $user['full_name']],
                            ['label' => 'Email',       'value' => $user['email']],
                            ['label' => 'Status',      'value' => ucfirst($user['status'] ?? 'unknown')],
                            ['label' => 'Phone',       'value' => $user['phone'] ?? '—'],
                            ['label' => 'Gender',      'value' => $user['gender'] ? ucfirst($user['gender']) : '—'],
                            ['label' => 'School',      'value' => $user['school'] ?? '—'],
                        ];
                        if ($user['role'] === 'student') {
                            $fields[] = ['label' => 'SHS Programme', 'value' => $user['shs_programme'] ? ucfirst(str_replace('_',' ',$user['shs_programme'])) : '—'];
                            $fields[] = ['label' => 'Target University', 'value' => $user['target_university'] ?? '—'];
                            $fields[] = ['label' => 'Target Programme', 'value' => $user['target_programme'] ?? '—'];
                        } elseif ($user['role'] === 'mentor') {
                            $fields[] = ['label' => 'Expertise',  'value' => $user['expertise'] ?? '—'];
                            $fields[] = ['label' => 'University', 'value' => $user['university'] ?? '—'];
                            $fields[] = ['label' => 'Profession', 'value' => $user['profession'] ?? '—'];
                        }
                        foreach ($fields as $field):
                        ?>
                            <div class="col-md-6">
                                <label class="text-muted text-xs font-outfit fw-bold d-block mb-1"><?= $field['label'] ?></label>
                                <span class="fw-bold font-outfit text-dark"><?= htmlspecialchars($field['value']) ?></span>
                            </div>
                        <?php endforeach; ?>
                        <?php if (!empty($user['bio'])): ?>
                        <div class="col-12">
                            <label class="text-muted text-xs font-outfit fw-bold d-block mb-1">Bio</label>
                            <p class="text-dark mb-0"><?= nl2br(htmlspecialchars($user['bio'])) ?></p>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- Recent Activity -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body p-4">
                    <h5 class="fw-bold font-outfit text-dark mb-4"><i class="fa-solid fa-clock-rotate-left text-warning me-2"></i>Recent Activity</h5>
                    <?php if (empty($recentActivity)): ?>
                        <p class="text-muted text-sm text-center py-3">No recent activity recorded.</p>
                    <?php else: ?>
                        <ul class="list-unstyled mb-0">
                            <?php foreach ($recentActivity as $act): ?>
                                <li class="d-flex gap-3 mb-3">
                                    <div class="badge-icon-box bg-primary-subtle text-primary rounded-circle flex-shrink-0" style="width:34px;height:34px;display:flex;align-items:center;justify-content:center;">
                                        <i class="fa-solid fa-bolt text-xs"></i>
                                    </div>
                                    <div>
                                        <span class="text-dark text-sm fw-bold font-outfit"><?= htmlspecialchars($act['description']) ?></span>
                                        <small class="text-muted d-block"><?= timeAgo($act['created_at']) ?></small>
                                    </div>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Admin Notes -->
            <div class="card border-0 shadow-sm">
                <div class="card-body p-4">
                    <h5 class="fw-bold font-outfit text-dark mb-3"><i class="fa-solid fa-note-sticky text-warning me-2"></i>Admin Notes</h5>
                    <form action="<?= APP_URL ?>/admin/users/<?= $user['id'] ?>/notes" method="POST">
                        <?= csrf_field() ?>
                        <textarea name="notes" class="form-control mb-3" rows="3" placeholder="Add an internal note about this user (not visible to user)..."></textarea>
                        <button class="btn btn-warning rounded-pill px-4 font-outfit fw-bold"><i class="fa-solid fa-save me-2"></i>Save Note</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
