<?php
/**
 * RUHONDO BEACH RESORT HMS + POS System
 * Breadcrumbs Partial
 * Developer: JOACHIM'S INNOTECH
 */

// Get current URI path
$current_uri = $_SERVER['REQUEST_URI'];
$base_path = parse_url(BASE_URL, PHP_URL_PATH) ?: '/';
$path = str_replace($base_path, '', parse_url($current_uri, PHP_URL_PATH));
$segments = array_filter(explode('/', $path));

// Breadcrumb definitions for known paths
$breadcrumb_titles = [
    // Core
    'dashboard.php' => 'Dashboard',
    'profile.php' => 'My Profile',
    'notifications.php' => 'Notifications',
    'access-denied.php' => 'Access Denied',
    '404.php' => 'Page Not Found',
    
    // Admin
    'admin' => 'Administration',
    'users' => 'User Management',
    'roles.php' => 'Roles & Permissions',
    'audit-logs.php' => 'Audit Logs',
    'settings' => 'System Settings',
    'resort.php' => 'Resort Information',
    'tax-currency.php' => 'Tax & Currency',
    'outlets.php' => 'POS Outlets',
    'payment-methods.php' => 'Payment Methods',
    'email-sms.php' => 'Email & SMS',
    'backup-restore.php' => 'Backup & Restore',
    
    // Front Desk
    'frontdesk' => 'Front Desk',
    'reservations' => 'Reservations',
    'checkin' => 'Check-In',
    'checkout' => 'Check-Out',
    'folio' => 'Guest Folio',
    'documents' => 'Documents',
    
    // Rooms
    'rooms' => 'Rooms Management',
    'room-types' => 'Room Types',
    'rates' => 'Rates & Plans',
    'availability' => 'Availability',
    'calendar.php' => 'Calendar',
    
    // Guests
    'guests' => 'Guest Management',
    'history' => 'Guest History',
    'blacklist.php' => 'Blacklist',
    
    // POS
    'pos' => 'Point of Sale',
    'outlets' => 'Outlets',
    'categories' => 'Categories',
    'products' => 'Products',
    'sales' => 'Sales',
    'shifts' => 'Shift Management',
    'reports' => 'Reports',
    'pos.php' => 'POS Terminal',
    'cart-ajax.php' => 'Cart',
    'checkout.php' => 'Checkout',
    'receipt.php' => 'Receipt',
    'receipt-pdf.php' => 'PDF Receipt',
    'room-charge.php' => 'Room Charge',
    'void-refund.php' => 'Void/Refund',
    'open.php' => 'Open Shift',
    'close.php' => 'Close Shift',
    'reconciliation.php' => 'Reconciliation',
    'daily-sales.php' => 'Daily Sales',
    'cashier-sales.php' => 'Cashier Sales',
    'product-sales.php' => 'Product Sales',
    
    // Inventory
    'inventory' => 'Inventory Management',
    'suppliers' => 'Suppliers',
    'purchases' => 'Purchase Orders',
    'stock' => 'Stock Management',
    'movements.php' => 'Stock Movements',
    'adjustments.php' => 'Stock Adjustments',
    'transfers.php' => 'Stock Transfers',
    'low-stock.php' => 'Low Stock Alert',
    'create-po.php' => 'Create Purchase Order',
    'receive.php' => 'Receive Stock',
    
    // Housekeeping
    'housekeeping' => 'Housekeeping',
    'tasks' => 'Cleaning Tasks',
    'cleaning' => 'Cleaning',
    'maintenance' => 'Maintenance',
    'schedule.php' => 'Schedule',
    'room-status.php' => 'Room Status',
    'tickets.php' => 'Maintenance Tickets',
    
    // Billing
    'billing' => 'Billing & Invoicing',
    'invoices' => 'Invoices',
    'payments' => 'Payments',
    'debtor' => 'Debtors',
    'statement.php' => 'Statement',
    'pdf.php' => 'PDF Invoice',
    'email.php' => 'Email Invoice',
    
    // Reports
    'occupancy.php' => 'Occupancy Report',
    'revenue.php' => 'Revenue Report',
    'adr-revpar.php' => 'ADR & RevPAR',
    'room-status.php' => 'Room Status Report',
    'reservations.php' => 'Reservations Report',
    'guest-analytics.php' => 'Guest Analytics',
    'inventory-valuation.php' => 'Inventory Valuation',
    'audit.php' => 'Audit Trail',
    'export-excel.php' => 'Export to Excel',
    'export-pdf.php' => 'Export to PDF',
    
    // Common actions
    'index.php' => 'List',
    'create.php' => 'Create New',
    'edit.php' => 'Edit',
    'view.php' => 'View Details',
    'cancel.php' => 'Cancel',
    'assign-room.php' => 'Assign Room',
    'start.php' => 'Start',
    'complete.php' => 'Complete',
    'add-charge.php' => 'Add Charge',
    'move-charge.php' => 'Move Charge',
    'registration-card.php' => 'Registration Card',
    'confirmation-email.php' => 'Confirmation Email',
    'images.php' => 'Images',
    'barcode.php' => 'Barcode',
    'status.php' => 'Status',
    'seasons.php' => 'Seasons',
    'rate-plans.php' => 'Rate Plans',
    'price-rules.php' => 'Price Rules',
    'ajax.php' => 'AJAX Handler',
    'add.php' => 'Add Payment',
    'refund.php' => 'Process Refund',
    'update.php' => 'Update'
];

// Generate breadcrumbs
function generate_breadcrumbs($segments, $titles) {
    $breadcrumbs = [];
    $path = '';
    
    // Add home
    $breadcrumbs[] = [
        'title' => '<i class="fas fa-home"></i> Home',
        'url' => base_url('dashboard.php'),
        'active' => false
    ];
    
    // Build breadcrumbs from segments
    foreach ($segments as $key => $segment) {
        $path .= '/' . $segment;
        
        // Remove file extension for title lookup
        $title_key = $segment;
        if (strpos($segment, '.php') !== false) {
            $title_key = $segment; // Keep full filename for PHP files
        }
        
        // Determine title
        if (isset($titles[$title_key])) {
            $title = $titles[$title_key];
        } elseif (isset($titles[$segment])) {
            $title = $titles[$segment];
        } else {
            // Format segment as title
            $title = ucwords(str_replace(['-', '_'], ' ', $segment));
        }
        
        // Check if this is the last segment
        $is_last = ($key === array_key_last($segments));
        
        // Determine URL (don't link last item)
        if ($is_last) {
            $url = null;
        } else {
            // Try to find an index.php in this directory
            $test_path = $path . '/index.php';
            if (file_exists($_SERVER['DOCUMENT_ROOT'] . $test_path)) {
                $url = base_url(ltrim($test_path, '/'));
            } else {
                $url = base_url(ltrim($path, '/'));
            }
        }
        
        $breadcrumbs[] = [
            'title' => $title,
            'url' => $url,
            'active' => $is_last
        ];
    }
    
    return $breadcrumbs;
}

$breadcrumbs = generate_breadcrumbs($segments, $breadcrumb_titles);
?>

<nav aria-label="breadcrumb">
    <ol class="breadcrumb float-sm-right">
        <?php foreach ($breadcrumbs as $index => $crumb): ?>
            <?php if ($crumb['active']): ?>
                <li class="breadcrumb-item active" aria-current="page">
                    <?php echo $crumb['title']; ?>
                </li>
            <?php else: ?>
                <li class="breadcrumb-item">
                    <a href="<?php echo $crumb['url']; ?>">
                        <?php echo $crumb['title']; ?>
                    </a>
                </li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ol>
</nav>

<!-- Add CSS for breadcrumbs -->
<style>
    .breadcrumb {
        background: transparent;
        padding: 0.5rem 1rem;
        margin-bottom: 0;
        font-size: 0.9rem;
    }
    
    .breadcrumb-item + .breadcrumb-item::before {
        content: "›";
    }
    
    .breadcrumb-item a {
        color: #007bff;
        text-decoration: none;
    }
    
    .breadcrumb-item a:hover {
        text-decoration: underline;
    }
    
    .breadcrumb-item.active {
        color: #6c757d;
    }
    
    @media (max-width: 576px) {
        .breadcrumb {
            font-size: 0.8rem;
            padding: 0.3rem 0.5rem;
        }
        
        .breadcrumb-item .fas {
            font-size: 0.8rem;
        }
    }
</style>

<!-- JSON-LD structured data for breadcrumbs -->
<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [
        <?php foreach ($breadcrumbs as $index => $crumb): ?>
        {
            "@type": "ListItem",
            "position": <?php echo $index + 1; ?>,
            "name": "<?php echo strip_tags($crumb['title']); ?>",
            "item": "<?php echo $crumb['url'] ?? current_url(); ?>"
        }<?php echo $index < count($breadcrumbs) - 1 ? ',' : ''; ?>
        <?php endforeach; ?>
    ]
}
</script>