<?php
// Prevent direct access
if (!defined('ABSPATH'))
    exit;

/**
 * Get Site Identity (Logo or Site Title)
 * 
 * Returns the custom logo if set, otherwise returns the site title
 * with styling matching the child-author template.
 * 
 * @return string HTML markup for logo or site title
 */
if (!function_exists('rswpthemes_awt_get_site_identity')) {
    function rswpthemes_awt_get_site_identity($location = 'header')
    {
        // Check if custom logo exists
        if (has_custom_logo()) {
            // Return the custom logo
            return get_custom_logo();
        } else {
            // Return site title with proper styling
            $site_name = get_bloginfo('name');
            if ($location === 'footer') {
                return $site_name;
            }
            return '<h1 class="text-3xl font-black text-navy tracking-tight">' . esc_html($site_name) . '</h1>';
        }
    }
}

if (!function_exists('rswpthemes_awt_get_menu_tree')) {
    function rswpthemes_awt_get_menu_tree($menu_id)
    {
        if (empty($menu_id))
            return [];
        $menu_items = wp_get_nav_menu_items($menu_id);
        if (!$menu_items)
            return [];

        $menu_tree = [];
        $children = [];

        foreach ($menu_items as $item) {
            if ($item->menu_item_parent == 0) {
                $menu_tree[$item->ID] = $item;
                $menu_tree[$item->ID]->children = [];
            } else {
                $children[] = $item;
            }
        }

        foreach ($children as $child) {
            rswpthemes_awt_add_child_to_parent($menu_tree, $children, $child);
        }

        return array_values($menu_tree);
    }
}

if (!function_exists('rswpthemes_awt_add_child_to_parent')) {
    function rswpthemes_awt_add_child_to_parent(&$menu_tree, &$all_children, $child)
    {
        foreach ($menu_tree as &$item) {
            if ($item->ID == $child->menu_item_parent) {
                if (!isset($item->children))
                    $item->children = [];
                $item->children[] = $child;
                $child->children = [];
                return;
            }
            if (!empty($item->children)) {
                if (rswpthemes_awt_search_children($item->children, $child))
                    return;
            }
        }
    }
}
if (!function_exists('rswpthemes_awt_search_children')) {
    function rswpthemes_awt_search_children(&$children, $child)
    {
        foreach ($children as &$item) {
            if ($item->ID == $child->menu_item_parent) {
                if (!isset($item->children))
                    $item->children = [];
                $item->children[] = $child;
                $child->children = [];
                return true;
            }
            if (!empty($item->children)) {
                if (rswpthemes_awt_search_children($item->children, $child))
                    return true;
            }
        }
        return false;
    }
}

/**
 * Render Desktop Menu (Dynamic with Style Arguments)
 * 
 * @param array $items Menu items tree
 * @param array $args Style configuration array
 * @return string HTML output
 */
if (!function_exists('rswpthemes_awt_render_desktop_menu')) {
    function rswpthemes_awt_render_desktop_menu($items, $args = [])
    {
        if (empty($items))
            return '';

        // Default Arguments
        $defaults = [
            'ul_class' => 'flex space-x-6 items-center list-none m-0 p-0',
            'item_class' => 'relative group',
            'link_class' => 'text-gray-700 hover:text-black transition-colors no-underline',
            'submenu_ul_class' => 'absolute top-full left-0 mt-2 w-48 bg-white shadow-lg rounded opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all duration-300 z-50',
            'submenu_item_class' => '',
            'submenu_link_class' => 'block px-4 py-2 text-sm hover:bg-gray-50 transition-colors no-underline'
        ];

        $style = wp_parse_args($args, $defaults);

        $output = '<ul class="' . esc_attr($style['ul_class']) . '">';

        foreach ($items as $item) {
            $has_children = !empty($item->children);
            $item_classes = $style['item_class'];

            $output .= '<li class="' . esc_attr($item_classes) . '">';
            $output .= '<a href="' . esc_url($item->url) . '" class="' . esc_attr($style['link_class']) . '">';
            $output .= esc_html($item->title);

            // Add dropdown arrow if needed
            if ($has_children) {
                $output .= ' <svg class="w-3 h-3 inline-block ml-1 opacity-70" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>';
            }
            $output .= '</a>';

            if ($has_children) {
                $output .= '<ul class="' . esc_attr($style['submenu_ul_class']) . '">';
                foreach ($item->children as $child) {
                    $output .= '<li class="' . esc_attr($style['submenu_item_class']) . '">';
                    $output .= '<a href="' . esc_url($child->url) . '" class="' . esc_attr($style['submenu_link_class']) . '">';
                    $output .= esc_html($child->title);
                    $output .= '</a></li>';
                }
                $output .= '</ul>';
            }
            $output .= '</li>';
        }
        $output .= '</ul>';

        return $output;
    }
}

/**
 * Render Submenus (Level 2, 3, 4 - Corrected Logic)
 */

if (!function_exists('rswpthemes_awt_render_desktop_submenu')) {
    function rswpthemes_awt_render_desktop_submenu($items, $level, $link_color = '')
    {
        if (empty($items))
            return '';

        $output = '';
        foreach ($items as $item) {
            $has_children = !empty($item->children);

            // 🔥 FIX: Tailwind JIT needs full class strings to exist in the file.
            // We cannot use dynamic concatenation like 'group/level' . $level

            $li_group_class = '';
            $ul_hover_class = '';

            if ($level === 2) {
                // Level 2 (Fiction -> Thriller)
                $li_group_class = 'group/level2';
                $ul_hover_class = 'group-hover/level2:opacity-100 group-hover/level2:visible group-hover/level2:translate-x-0';
            } elseif ($level === 3) {
                // Level 3 (Thriller -> Crime)
                $li_group_class = 'group/level3';
                $ul_hover_class = 'group-hover/level3:opacity-100 group-hover/level3:visible group-hover/level3:translate-x-0';
            } else {
                // Fallback / Level 4+
                $li_group_class = 'group/level4';
                $ul_hover_class = 'group-hover/level4:opacity-100 group-hover/level4:visible group-hover/level4:translate-x-0';
            }

            $output .= '<li class="relative ' . $li_group_class . ' border-b border-gray-50 last:border-0">';

            $output .= '<a href="' . esc_url($item->url) . '" class="flex justify-between items-center px-5 py-3 hover:bg-blue-sky/10 hover:text-blue-sky transition-colors text-navy/80">';
            $output .= esc_html($item->title);

            if ($has_children) {
                // Right Arrow
                $output .= '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>';
            }
            $output .= '</a>';

            // Nested Dropdown (Level 3 & 4)
            if ($has_children && $level < 4) {
                // Note: We are using the $ul_hover_class variable which contains the full strings
                $output .= '<ul class="absolute list-none top-0 left-full w-56 bg-white rounded-lg shadow-xl border border-gray-100 opacity-0 invisible ' . $ul_hover_class . ' transition-all duration-300 transform translate-x-2 ml-2 z-50">';
                $output .= rswpthemes_awt_render_desktop_submenu($item->children, $level + 1, $link_color);
                $output .= '</ul>';
            }

            $output .= '</li>';
        }
        return $output;
    }
}
/**
 * Render Mobile Menu (Accordion Style)
 * 
 * @param array $items Menu items tree
 * @param array $style_args Style configuration array
 * @return string HTML output
 */
if (!function_exists('rswpthemes_awt_render_mobile_menu')) {
    function rswpthemes_awt_render_mobile_menu($items, $style_args = [])
    {
        if (empty($items))
            return '';

        // Default style configuration
        $defaults = [
            'link_class' => 'block text-navy/80 px-4 py-2 hover:bg-gray-50 rounded-lg hover:text-blue-sky transition-colors',
            'submenu_border' => 'border-gray-100',
            'summary_class' => 'flex justify-between items-center px-4 py-2 hover:bg-gray-50 rounded-lg cursor-pointer marker:content-none',
            'container_class' => 'flex list-none flex-col p-1 space-y-0 font-bold'
        ];

        // Merge with provided args
        $args = wp_parse_args($style_args, $defaults);

        $output = '<ul class="' . esc_attr($args['container_class']) . '">';

        foreach ($items as $item) {
            $has_children = !empty($item->children);

            if ($has_children) {
                $output .= '<li>';
                $output .= '<details class="group">';
                $output .= '<summary class="' . esc_attr($args['summary_class']) . '">';
                $output .= esc_html($item->title);
                $output .= '<svg class="w-4 h-4 transition-transform group-open:rotate-180" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>';
                $output .= '</summary>';
                $output .= '<div class="pl-1 mt-0 space-y-0 border-l-2 ' . esc_attr($args['submenu_border']) . '">';
                $output .= rswpthemes_awt_render_mobile_menu($item->children, $style_args);
                $output .= '</div>';
                $output .= '</details>';
                $output .= '</li>';
            } else {
                $output .= '<li>';
                $output .= '<a href="' . esc_url($item->url) . '" class="' . esc_attr($args['link_class']) . '">';
                $output .= esc_html($item->title);
                $output .= '</a>';
                $output .= '</li>';
            }
        }

        $output .= '</ul>';
        return $output;
    }
}
