<?php
/**
 * modules/users/view_users.php
 */

// 1. STRICT SECURITY KICK-OUT
$p = $GLOBALS['my_privs'] ?? ['can_view' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) {
    $p['can_view'] = 1;
}

if ($p['can_view'] == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=users&page=list_users') . "&status=access_denied");
    exit;
}

$user_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($user_id == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=users&page=list_users'));
    exit;
}

// 2. FETCH USER PROFILE & ROLES
$sql = "SELECT u.*, GROUP_CONCAT(r.ROLE_NAME SEPARATOR ', ') as ASSIGNED_ROLES
        FROM TBL_USERS u
        LEFT JOIN TBL_USER_ROLES ur ON u.USER_ID = ur.USER_ID
        LEFT JOIN TBL_ROLES r ON ur.ROLE_ID = r.ROLE_ID
        WHERE u.USER_ID = $user_id
        GROUP BY u.USER_ID";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    header("Location: index.php?s=" . encrypt_url('mod=users&page=list_users'));
    exit;
}
?>

<div class="p-3">
    <section class="content-header px-0">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="font-weight-bold text-primary"><i class="fas fa-id-card mr-2"></i>User Profile</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="index.php?s=<?php echo encrypt_url('mod=users&page=list_users'); ?>" class="btn btn-default shadow-sm border">
                        <i class="fas fa-arrow-left mr-1"></i> Back to List
                    </a>
                </div>
            </div>
        </div>
    </section>

    <section class="content px-0">
        <div class="container-fluid">
            <div class="card card-outline card-primary shadow-sm" style="border-top: 4px solid #007bff;">
                <div class="card-header bg-white">
                    <h3 class="card-title font-weight-bold text-dark">Account Information</h3>
                    <div class="card-tools">
                        <span class="badge <?php echo ($data['STATUS'] == 'ACTIVE') ? 'badge-success' : 'badge-danger'; ?> px-3 py-1" style="font-size: 14px;">
                            <?php echo $data['STATUS']; ?>
                        </span>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Full Name</label>
                            <h5 class="font-weight-bold text-dark"><?php echo $data['FULL_NAME']; ?></h5>
                        </div>
                        <div class="col-md-6 mb-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">System Username</label>
                            <h5 class="text-primary font-weight-bold">@<?php echo $data['USERNAME']; ?></h5>
                        </div>
                        
                        <div class="col-md-12 mb-4">
                            <label class="text-muted text-uppercase mb-2" style="font-size: 12px;">Assigned Authorizations (Roles)</label>
                            <div>
                                <?php 
                                    if ($data['ASSIGNED_ROLES']) {
                                        $roles = explode(', ', $data['ASSIGNED_ROLES']);
                                        foreach($roles as $role) {
                                            echo '<span class="badge badge-info shadow-sm mr-2 px-3 py-2" style="font-size:14px;"><i class="fas fa-shield-alt mr-1"></i>'.$role.'</span>';
                                        }
                                    } else {
                                        echo '<span class="text-muted">No roles currently assigned.</span>';
                                    }
                                ?>
                            </div>
                        </div>

                        <div class="col-md-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Account Created</label>
                            <p class="font-weight-bold"><?php echo date('d-M-Y h:i A', strtotime($data['CREATED_DATE'])); ?></p>
                        </div>
                        <div class="col-md-4">
                            <label class="text-muted text-uppercase mb-1" style="font-size: 12px;">Last Login</label>
                            <p class="font-weight-bold"><?php echo $data['LAST_LOGIN'] ? date('d-M-Y h:i A', strtotime($data['LAST_LOGIN'])) : 'Never'; ?></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>