<?php
if (!defined('ABSPATH')) exit;

class ASSI_Sidebar {

    public static function getSidebarMenuHtml() {
        $html = '';
        if (defined('ASSI_PLUGIN_MENU_ITEMS')) {
            $menu = unserialize(ASSI_PLUGIN_MENU_ITEMS);
            if (is_array($menu) && !empty($menu)) {
                $segmentedMenu = array();
                foreach ($menu as $item) {
                    if (!isset($item['ass_sidebar']) || !$item['ass_sidebar']) {
                        continue;
                    }
                    $segmentedMenu[$item['ass_sidebar']][] = $item;
                }
                foreach ($segmentedMenu as $segment => $items) {
                    $html .= '<div class="ass-sidebar-segment d-flex flex-column gap-2 mt-2">';
                    $html .= '<p class="text-uppercase text-black-50 mb-0 fw-medium">' . sanitize_text_field($segment) . '</p>';
                    $html .= '<div class="ass-sidebar-subsegment d-flex flex-column justify-content-center align-items-start gap-2">';
        
                    foreach ($items as $entry) {
                        $html .= '<button class="btn d-flex align-items-center gap-2 ass-sidebar-menu-item ass-transparent-border" data-content="' . esc_attr($entry['id']) . '">';
                        $html .= '<i class="ass-sidebar-icon bi ' . esc_attr($entry['ass_icon']) . '" style="font-size: 24px;"></i>';
                        $html .= '<div class="d-flex flex-column align-items-start ass-max-w-150">';
                        $html .= '<p class="mb-0">' . sanitize_text_field($entry['display_name']) . '</p>';
                        $html .= '</div>';
                        if (isset($entry['ass_badge']) && !empty($entry['ass_badge'])) {
                            $html .= '<div class="badge bg-primary rounded">';
                            $html .= '<p class="mb-0">' . sanitize_text_field($entry['ass_badge']) . '</p>';
                            $html .= '</div>';
                        }
                        if ($entry['id'] == 'notification') {
                            $html .= '<div class="ass-sidebar-notification-badge badge bg-secondary rounded" style="display:none;">';
                            $html .= '<p class="ass-sidebar-notification-badge-count mb-0">0</p>';
                            $html .= '</div>';
                        }
                        $html .= '</button>';
                    }
        
                    $html .= '</div>';
                    $html .= '</div>';
                }
            }
        }
        return $html;
    }
}