<?php require APPROOT . '/app/Views/layouts/header.php'; ?>

<div class="max-w-5xl mx-auto mb-10">
    
    <div class="flex flex-col md:flex-row md:items-center justify-between border-b border-gray-200 dark:border-gray-700/50 pb-6 mb-8">
        <div class="mb-4 md:mb-0">
            <div class="flex items-center space-x-3 mb-2">
                <a href="<?php echo URLROOT; ?>/admin/users" class="text-gray-400 hover:text-primary transition-colors">
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
                </a>
                <h1 class="text-2xl md:text-3xl font-bold text-gray-900 dark:text-white">User Profile</h1>
            </div>
            <p class="text-sm text-gray-600 dark:text-gray-400 ml-9">Viewing detailed account information and security status.</p>
        </div>
        <div class="flex space-x-3">
            <a href="<?php echo URLROOT; ?>/admin/editUser/<?php echo $data['user']->id; ?>" class="w-full md:w-auto bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-800 dark:text-gray-200 font-bold py-2 px-4 rounded-lg transition-colors text-sm shadow-sm flex items-center justify-center">
                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"></path></svg>
                Edit User
            </a>
            <?php if($data['user']->id !== $_SESSION['user_id'] && !isset($_SESSION['admin_impersonating'])): ?>
                <form action="<?php echo URLROOT; ?>/admin/impersonate/<?php echo $data['user']->id; ?>" method="POST" onsubmit="confirmAction(event, this, 'Impersonate User', 'Are you sure you want to login as this user?');" class="inline">
                    <button type="submit" class="w-full md:w-auto bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2 px-4 rounded-lg transition-colors text-sm shadow-sm flex items-center justify-center">
                        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg>
                        Impersonate
                    </button>
                </form>
            <?php endif; ?>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        
        <div class="lg:col-span-2 space-y-8">
            
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex items-center">
                    <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path></svg>
                    <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider">Identity Details</h3>
                </div>
                <div class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Full Name</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo htmlspecialchars($data['user']->name); ?></span>
                        </div>
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Email Address</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base">
                                <a href="mailto:<?php echo htmlspecialchars($data['user']->email); ?>" class="text-primary hover:underline"><?php echo htmlspecialchars($data['user']->email); ?></a>
                            </span>
                        </div>
                        
                        <?php if(!empty($data['user']->company_name)): ?>
                        <div class="md:col-span-2">
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Company Name</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo htmlspecialchars($data['user']->company_name); ?></span>
                        </div>
                        <?php endif; ?>

                        <?php if(!empty($data['user']->partner_name)): ?>
                        <div class="md:col-span-2">
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Assigned Agency</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base text-indigo-500"><?php echo htmlspecialchars($data['user']->partner_name); ?></span>
                        </div>
                        <?php endif; ?>

                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1.5">Account Role</span>
                            <span class="px-3 py-1 inline-flex text-xs font-bold uppercase tracking-wider rounded border 
                                 <?php 
                                 if ($data['user']->role === 'admin') echo 'bg-purple-50 dark:bg-purple-900/10 text-purple-700 dark:text-purple-400 border-purple-200 dark:border-purple-800/30'; 
                                 elseif ($data['user']->role === 'staff') echo 'bg-blue-50 dark:bg-blue-900/10 text-blue-700 dark:text-blue-400 border-blue-200 dark:border-blue-800/30'; 
                                 elseif ($data['user']->role === 'partner') echo 'bg-indigo-50 dark:bg-indigo-900/10 text-indigo-700 dark:text-indigo-400 border-indigo-200 dark:border-indigo-800/30';
                                 else echo 'bg-emerald-50 dark:bg-emerald-900/10 text-emerald-700 dark:text-emerald-400 border-emerald-200 dark:border-emerald-800/30'; 
                                ?>">
                                <?php echo $data['user']->role === 'partner' ? 'Agency' : ucfirst($data['user']->role); ?>
                            </span>
                            
                            <?php if($data['user']->role === 'staff' && !empty($data['user']->staff_role_name)): ?>
                                <span class="ml-2 px-2 py-1 inline-flex text-[10px] font-bold tracking-wider rounded border bg-gray-100 text-gray-600 border-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-600">
                                    <?php echo htmlspecialchars($data['user']->staff_role_name); ?>
                                </span>
                            <?php endif; ?>
                        </div>
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1.5">Account Status</span>
                            <?php if($data['user']->is_active == 1): ?>
                                <span class="px-3 py-1 inline-flex text-xs font-bold uppercase tracking-wider rounded border bg-green-50 text-green-700 border-green-200 dark:bg-green-900/10 dark:text-green-400 dark:border-green-800/30">Active</span>
                            <?php else: ?>
                                <span class="px-3 py-1 inline-flex text-xs font-bold uppercase tracking-wider rounded border bg-red-50 text-red-700 border-red-200 dark:bg-red-900/10 dark:text-red-400 dark:border-red-800/30">Disabled</span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Contact & Address Details Block -->
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex items-center">
                    <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"></path></svg>
                    <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider">Contact & Address Details</h3>
                </div>
                <div class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div class="md:col-span-2">
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Contact Number</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo !empty($data['user']->contact_number) ? htmlspecialchars($data['user']->contact_number) : '<span class="text-gray-400 italic">Not provided</span>'; ?></span>
                        </div>
                        <div class="md:col-span-2">
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Street Address</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo !empty($data['user']->address_street) ? htmlspecialchars($data['user']->address_street) : '<span class="text-gray-400 italic">Not provided</span>'; ?></span>
                        </div>
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Suburb</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo !empty($data['user']->address_suburb) ? htmlspecialchars($data['user']->address_suburb) : '<span class="text-gray-400 italic">-</span>'; ?></span>
                        </div>
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">City</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo !empty($data['user']->address_city) ? htmlspecialchars($data['user']->address_city) : '<span class="text-gray-400 italic">-</span>'; ?></span>
                        </div>
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">State / Province</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo !empty($data['user']->address_state) ? htmlspecialchars($data['user']->address_state) : '<span class="text-gray-400 italic">-</span>'; ?></span>
                        </div>
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Postal / Zip Code</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo !empty($data['user']->address_zip) ? htmlspecialchars($data['user']->address_zip) : '<span class="text-gray-400 italic">-</span>'; ?></span>
                        </div>
                        <div class="md:col-span-2">
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Country</span>
                            <span class="text-gray-900 dark:text-white font-medium text-base"><?php echo !empty($data['user']->address_country) ? htmlspecialchars($data['user']->address_country) : '<span class="text-gray-400 italic">-</span>'; ?></span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex items-center">
                    <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                    <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider">Timeline Metrics</h3>
                </div>
                <div class="p-6">
                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Registration Date</span>
                            <span class="text-gray-900 dark:text-white font-mono text-sm"><?php echo date('F j, Y - H:i', strtotime($data['user']->created_at)); ?></span>
                        </div>
                        <div>
                            <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-1">Email Verification</span>
                            <?php if($data['user']->email_verified == 1): ?>
                                <span class="text-green-600 dark:text-green-400 font-bold text-sm flex items-center">
                                    <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
                                    Verified
                                </span>
                            <?php else: ?>
                                <span class="text-yellow-600 dark:text-yellow-500 font-bold text-sm flex items-center">
                                    <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
                                    Pending
                                </span>
                            <?php endif; ?>
                        </div>
                        <?php if(!empty($data['user']->locked_until)): ?>
                        <div class="sm:col-span-2 bg-red-50 dark:bg-red-900/10 border border-red-200 dark:border-red-800/30 rounded-lg p-4 mt-2">
                            <span class="block text-[10px] font-bold text-red-800 dark:text-red-400 uppercase tracking-wider mb-1">Security Lockout</span>
                            <span class="text-red-700 dark:text-red-300 text-sm">Account is temporarily locked until: <strong class="font-mono"><?php echo $data['user']->locked_until; ?></strong> due to multiple failed login attempts.</span>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <div class="space-y-8">
            
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden flex flex-col">
                <div class="px-6 py-4 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex items-center">
                    <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"></path></svg>
                    <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider">Security Profile</h3>
                </div>
                <div class="p-6">
                    
                    <?php if ($data['user']->totp_enabled == 1): ?>
                        <div class="bg-green-50 dark:bg-green-900/10 border border-green-200 dark:border-green-800/30 rounded-xl p-5 mb-6 flex items-start">
                            <svg class="w-6 h-6 text-green-500 mr-3 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                            <div>
                                <h4 class="text-sm font-bold text-green-800 dark:text-green-400">2FA Active</h4>
                                <p class="text-xs text-green-700 dark:text-green-500 mt-1">This user has successfully configured Two-Factor Authentication via an authenticator app.</p>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="bg-yellow-50 dark:bg-yellow-900/10 border border-yellow-200 dark:border-yellow-800/30 rounded-xl p-5 mb-6 flex items-start">
                            <svg class="w-6 h-6 text-yellow-600 dark:text-yellow-500 mr-3 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
                            <div>
                                <h4 class="text-sm font-bold text-yellow-800 dark:text-yellow-400">2FA Inactive</h4>
                                <p class="text-xs text-yellow-700 dark:text-yellow-500/80 mt-1">This user is currently relying solely on their password for authentication.</p>
                            </div>
                        </div>
                    <?php endif; ?>
                    
                    <div class="border-t border-gray-200 dark:border-gray-700/50 pt-5 mt-2">
                        <span class="block text-[10px] font-bold text-gray-500 uppercase tracking-wider mb-2">Failed Login Attempts</span>
                        <div class="flex items-center">
                            <span class="text-3xl font-extrabold <?php echo ($data['user']->failed_attempts > 0) ? 'text-red-500' : 'text-gray-900 dark:text-white'; ?> mr-3"><?php echo $data['user']->failed_attempts; ?></span>
                            <?php if($data['user']->failed_attempts > 0): ?>
                                <span class="text-xs text-gray-500 dark:text-gray-400">Triggering lockout soon</span>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                </div>
            </div>

        </div>

    </div>

</div>

<?php require APPROOT . '/app/Views/layouts/footer.php'; ?>