<template>
    <div
        class="border-l-4"
        :class="{
            'border-green-600': isActive,
            'hover:bg-gray-700 border-transparent': !isActive
        }">
        <div class="flex w-full" @click="toggleExpanded">
            <RouterLink
                :to="to"
                class="flex-grow px-4 py-2 flex items-center"
                :class="{
                    'text-green-600 bg-gray-800': isActive
                }">
                <div class="flex items-center flex-grow">
                    <component :is="icon" class="w-5 h-5 mr-3" />
                    <span>{{ text }}</span>
                </div>
                <component :is="expanded ? ChevronUpIcon : ChevronDownIcon" class="w-4 h-4" />
            </RouterLink>
        </div>

        <ul v-if="subItems.length > 0" class="ml-8 mt-2 space-y-4" v-show="expanded">
            <SubMenuItem
                v-for="(subItem, index) in subItems"
                :key="index"
                :text="subItem.text"
                :to="subItem.to"
                @click="subItem.onClick" />
        </ul>
    </div>
</template>

<script setup>
    import { ref, watch } from 'vue';
    import { useRoute } from 'vue-router';
    import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/vue/24/outline';
    import SubMenuItem from './SubMenuItem.vue';
    import { useMenuStore } from '../../../stores/menuStore';

    const props = defineProps({
        id: {
            type: String,
            required: true
        },
        icon: {
            type: Function,
            required: true
        },
        text: {
            type: String,
            required: true
        },
        to: {
            type: String,
            required: true
        },
        subItems: {
            type: Array,
            default: () => []
        },
        isActive: {
            type: Boolean,
            required: true
        }
    });

    const menuStore = useMenuStore();
    const expanded = ref(false);
    const route = useRoute();

    const isRouteMatching = path => {
        return props.subItems.some(item => path.startsWith(item.to)) || path === props.to;
    };

    watch(
        [() => route.path, () => menuStore.activeMenuId],
        ([newPath, newMenuId]) => {
            if (isRouteMatching(newPath)) {
                expanded.value = true;
                menuStore.setActiveMenu(props.id);
            } else if (newMenuId !== props.id) {
                expanded.value = false;
            }
        },
        { immediate: true }
    );

    const toggleExpanded = () => {
        if (expanded.value) {
            menuStore.setActiveMenu(null);
        } else {
            menuStore.setActiveMenu(props.id);
        }
        expanded.value = !expanded.value;
    };
</script>
