<?php
/**
 * RUHONDO BEACH RESORT HMS + POS System
 * Access Denied Page
 * Developer: JOACHIM'S INNOTECH
 */

// Initialize system
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/config/constants.php';
require_once dirname(__DIR__) . '/config/database.php';
require_once dirname(__DIR__) . '/config/helpers.php';
require_once dirname(__DIR__) . '/config/auth.php';

// Set page title
$page_title = 'Access Denied';

// Require authentication (user must be logged in to see this page)
require_auth();

// Get current user
$current_user = current_user();

// Include header
require_once dirname(__DIR__) . '/views/partials/header.php';
?>

<!-- Content Start -->
<div class="access-denied-page min-vh-75 d-flex align-items-center justify-content-center py-5">
    <div class="text-center">
        <!-- Lock Icon with Animation -->
        <div class="mb-4 position-relative">
            <div class="lock-icon">
                <i class="fas fa-lock fa-5x text-danger"></i>
                <i class="fas fa-exclamation-circle fa-2x text-warning position-absolute" style="bottom: -10px; right: -10px;"></i>
            </div>
        </div>
        
        <!-- Error Code -->
        <div class="error-code text-muted mb-3">403</div>
        
        <!-- Main Message -->
        <h2 class="error-message mb-3">Access Denied</h2>
        
        <!-- Description -->
        <p class="error-description text-muted mb-4 mx-auto" style="max-width: 500px;">
            Sorry, you don't have permission to access this page. 
            If you believe this is an error, please contact your system administrator.
        </p>
        
        <!-- User Info Card -->
        <div class="user-info-card bg-light rounded p-4 mb-4 mx-auto" style="max-width: 400px;">
            <div class="d-flex align-items-center mb-3">
                <div class="avatar mr-3">
                    <?php if (!empty($current_user['profile_image'])): ?>
                        <img src="<?php echo uploads_url($current_user['profile_image']); ?>" 
                             alt="User Avatar" class="rounded-circle" width="50" height="50">
                    <?php else: ?>
                        <div class="avatar-placeholder bg-primary text-white rounded-circle d-flex align-items-center justify-content-center" 
                             style="width: 50px; height: 50px;">
                            <?php echo strtoupper(substr($current_user['first_name'] ?? 'U', 0, 1) . substr($current_user['last_name'] ?? 'S', 0, 1)); ?>
                        </div>
                    <?php endif; ?>
                </div>
                <div class="user-details text-left">
                    <h6 class="mb-1"><?php echo escape_html($current_user['first_name'] . ' ' . $current_user['last_name']); ?></h6>
                    <p class="text-muted small mb-0">
                        <i class="fas fa-tag mr-1"></i> Role: <?php echo get_user_role_name($current_user['role_id']); ?>
                    </p>
                    <p class="text-muted small mb-0">
                        <i class="fas fa-envelope mr-1"></i> <?php echo escape_html($current_user['email']); ?>
                    </p>
                </div>
            </div>
            
            <!-- Permission Required -->
            <?php if (isset($_GET['permission'])): ?>
                <div class="alert alert-warning small mb-0">
                    <i class="fas fa-key mr-1"></i>
                    Required Permission: <strong><?php echo escape_html($_GET['permission']); ?></strong>
                </div>
            <?php endif; ?>
        </div>
        
        <!-- Action Buttons -->
        <div class="action-buttons">
            <div class="d-flex justify-content-center flex-wrap">
                <a href="javascript:history.back()" class="btn btn-outline-secondary btn-lg mr-2 mb-2">
                    <i class="fas fa-arrow-left mr-2"></i> Go Back
                </a>
                <a href="<?php echo base_url('dashboard.php'); ?>" class="btn btn-primary btn-lg mr-2 mb-2">
                    <i class="fas fa-tachometer-alt mr-2"></i> Dashboard
                </a>
                <a href="<?php echo base_url('profile.php'); ?>" class="btn btn-info btn-lg mb-2">
                    <i class="fas fa-user mr-2"></i> My Profile
                </a>
            </div>
        </div>
        
        <!-- Contact Support -->
        <div class="contact-support mt-5 pt-4 border-top">
            <p class="text-muted mb-3">Need help? Contact your administrator or support team:</p>
            <div class="support-contacts">
                <a href="mailto:support@ruhondo.com" class="btn btn-outline-primary btn-sm mr-2">
                    <i class="fas fa-envelope mr-1"></i> support@ruhondo.com
                </a>
                <a href="tel:+250788888888" class="btn btn-outline-primary btn-sm">
                    <i class="fas fa-phone mr-1"></i> +250 788 888 888
                </a>
            </div>
        </div>
    </div>
</div>

<!-- Additional CSS -->
<style>
    .access-denied-page {
        animation: fadeIn 0.5s ease;
    }
    
    .lock-icon {
        position: relative;
        display: inline-block;
        animation: shake 0.5s ease-in-out;
    }
    
    .error-code {
        font-size: 6rem;
        font-weight: 700;
        line-height: 1;
        opacity: 0.2;
    }
    
    .error-message {
        font-size: 2.5rem;
        font-weight: 600;
        color: #dc3545;
    }
    
    .error-description {
        font-size: 1.1rem;
        line-height: 1.6;
    }
    
    .avatar-placeholder {
        font-weight: 600;
        font-size: 1.2rem;
    }
    
    .user-info-card {
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        transition: transform 0.3s ease;
    }
    
    .user-info-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 12px rgba(0,0,0,0.15);
    }
    
    .action-buttons .btn {
        min-width: 150px;
        transition: all 0.3s ease;
    }
    
    .action-buttons .btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    }
    
    .contact-support {
        opacity: 0.8;
        transition: opacity 0.3s ease;
    }
    
    .contact-support:hover {
        opacity: 1;
    }
    
    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(20px);
        }
        to {
            opacity: 1;
            transform: translateY(0);
        }
    }
    
    @keyframes shake {
        0%, 100% { transform: translateX(0); }
        10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
        20%, 40%, 60%, 80% { transform: translateX(5px); }
    }
    
    @media (max-width: 768px) {
        .error-code {
            font-size: 4rem;
        }
        
        .error-message {
            font-size: 2rem;
        }
        
        .error-description {
            font-size: 1rem;
        }
        
        .action-buttons .btn {
            width: 100%;
            margin: 0.25rem 0 !important;
        }
        
        .user-info-card {
            margin: 0 1rem;
        }
    }
    
    /* Dark mode support */
    @media (prefers-color-scheme: dark) {
        .user-info-card {
            background-color: #2c2c2c !important;
            color: #f5f5f5;
        }
        
        .user-info-card .text-muted {
            color: #aaa !important;
        }
        
        .contact-support .text-muted {
            color: #aaa !important;
        }
    }
</style>

<!-- JavaScript -->
<script>
    $(document).ready(function() {
        // Log access denied attempt (optional)
        console.log('Access denied for user: <?php echo $current_user['user_id']; ?> on page: ' + window.location.href);
        
        // Add animation on page load
        setTimeout(function() {
            $('.lock-icon').css('animation', 'none');
        }, 500);
    });
</script>

<?php
// Include footer
require_once dirname(__DIR__) . '/views/partials/footer.php';

/**
 * Helper function to get user role name
 */
function get_user_role_name($role_id) {
    static $roles = null;
    
    if ($roles === null) {
        $roles = db_fetch_all("SELECT role_id, role_name FROM roles");
        $roles = array_column($roles, 'role_name', 'role_id');
    }
    
    return $roles[$role_id] ?? 'Unknown';
}
?>