<?php
use BLR\Backline\Modules\Users\Pages as UsersPages;

if (!defined('ABSPATH')) {
    exit;
}

$__blr_ba_data = isset($ctx['user_view']) && is_array($ctx['user_view']) ? $ctx['user_view'] : UsersPages::view_data($ctx);
foreach ($__blr_ba_data as $__blr_ba_key => $__blr_ba_value) {
    ${$__blr_ba_key} = $__blr_ba_value;
}
unset($__blr_ba_data, $__blr_ba_key, $__blr_ba_value);

if (!empty($not_found)) {
    echo '<div class="container-fluid p-0"><div class="alert alert-danger">' . esc_html__('User not found.', 'blr-backline') . '</div></div>';
    return;
}
?>
<div class="container-fluid p-0">
    <div class="d-flex flex-column flex-lg-row align-items-lg-center justify-content-between gap-3 mb-3">
        <div class="d-flex align-items-center gap-3">
            <img class="rounded-circle" src="<?php echo esc_url((string) $user_item['avatar_url']); ?>" alt="" width="72" height="72">
            <div>
                <h1 class="h3 mb-1"><?php echo esc_html((string) $user_item['display_name']); ?></h1>
                <div class="text-muted">
                    <?php echo esc_html('@' . (string) $user_item['user_login']); ?>
                    <?php if (!empty($user_item['roles'])) : ?>
                        <span aria-hidden="true"> · </span><?php echo esc_html(implode(', ', (array) $user_item['roles'])); ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div>
            <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'users/'); ?>"><?php echo esc_html__('Back', 'blr-backline'); ?></a>
        </div>
    </div>

    <div class="row g-3">
        <div class="col-12 col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0"><?php echo esc_html__('User details', 'blr-backline'); ?></h5>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-12 col-md-6">
                            <div class="text-muted small"><?php echo esc_html__('Email', 'blr-backline'); ?></div>
                            <?php if (!empty($user_item['user_email'])) : ?>
                                <a href="mailto:<?php echo esc_attr((string) $user_item['user_email']); ?>"><?php echo esc_html((string) $user_item['user_email']); ?></a>
                            <?php else : ?>
                                <div class="text-muted">—</div>
                            <?php endif; ?>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="text-muted small"><?php echo esc_html__('Website', 'blr-backline'); ?></div>
                            <?php if (!empty($user_item['website'])) : ?>
                                <a href="<?php echo esc_url((string) $user_item['website']); ?>" target="_blank" rel="noopener"><?php echo esc_html((string) $user_item['website']); ?></a>
                            <?php else : ?>
                                <div class="text-muted">—</div>
                            <?php endif; ?>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="text-muted small"><?php echo esc_html__('First name', 'blr-backline'); ?></div>
                            <div><?php echo esc_html((string) ($user_item['first_name'] ?: '—')); ?></div>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="text-muted small"><?php echo esc_html__('Last name', 'blr-backline'); ?></div>
                            <div><?php echo esc_html((string) ($user_item['last_name'] ?: '—')); ?></div>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="text-muted small"><?php echo esc_html__('Joined', 'blr-backline'); ?></div>
                            <div><?php echo esc_html((string) ($user_item['registered_label'] ?: '—')); ?></div>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="text-muted small"><?php echo esc_html__('Last login', 'blr-backline'); ?></div>
                            <div>
                                <?php if (!empty($user_item['last_login_label'])) : ?>
                                    <?php echo esc_html((string) $user_item['last_login_label']); ?>
                                <?php else : ?>
                                    <?php echo esc_html__('Never', 'blr-backline'); ?>
                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="text-muted small"><?php echo esc_html__('Backline access', 'blr-backline'); ?></div>
                            <span class="badge <?php echo esc_attr(!empty($user_item['is_admin']) ? 'bg-primary' : 'bg-secondary'); ?>">
                                <?php echo esc_html((string) $user_item['access_label']); ?>
                            </span>
                        </div>
                    </div>

                    <hr>

                    <div class="text-muted small mb-2"><?php echo esc_html__('Bio', 'blr-backline'); ?></div>
                    <?php if (trim((string) $user_item['description']) !== '') : ?>
                        <?php echo wp_kses_post(wpautop((string) $user_item['description'])); ?>
                    <?php else : ?>
                        <div class="text-muted"><?php echo esc_html__('No bio has been added yet.', 'blr-backline'); ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-4">
            <div class="card mb-3">
                <div class="card-header">
                    <h5 class="card-title mb-0"><?php echo esc_html__('Module access', 'blr-backline'); ?></h5>
                </div>
                <div class="card-body">
                    <?php if (empty($user_item['module_labels'])) : ?>
                        <div class="text-muted"><?php echo esc_html__('No modules assigned.', 'blr-backline'); ?></div>
                    <?php else : ?>
                        <?php foreach ((array) $user_item['module_labels'] as $module_label) : ?>
                            <span class="badge bg-light text-dark border me-1 mb-1"><?php echo esc_html((string) $module_label); ?></span>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>

            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0"><?php echo esc_html__('Permissions', 'blr-backline'); ?></h5>
                </div>
                <div class="card-body">
                    <?php if (empty($user_item['extra_labels'])) : ?>
                        <div class="text-muted"><?php echo esc_html__('No extra permissions assigned.', 'blr-backline'); ?></div>
                    <?php else : ?>
                        <?php foreach ((array) $user_item['extra_labels'] as $extra_label) : ?>
                            <span class="badge bg-light text-dark border me-1 mb-1"><?php echo esc_html((string) $extra_label); ?></span>
                        <?php endforeach; ?>
                    <?php endif; ?>

                    <p class="text-muted small mt-3 mb-0">
                        <?php echo esc_html__('This Backline page is read-only. User management stays in WordPress admin.', 'blr-backline'); ?>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
