<script setup lang="ts">
import { Wrench, FlaskConical, Ticket, Sparkles, GraduationCap, Home } from 'lucide-vue-next'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Badge } from '@/components/ui/badge'

type RoleCode = 'private' | 'educator' | 'company'

interface ProductArea {
  id: string
  label: string
  icon: any
  enabled: boolean
  visible: boolean
  badge?: string
  route: string
  roleVisibility?: 'all' | RoleCode[]
}

const areas: ProductArea[] = [
  {
    id: 'start',
    label: 'Start',
    icon: Home,
    enabled: true,
    visible: true,
    route: '/',
    roleVisibility: 'all',
  },
  {
    id: 'makerspace',
    label: 'Makerspace',
    icon: Wrench,
    enabled: true,
    visible: true,
    route: '/products',
    roleVisibility: 'all',
  },
  {
    id: 'educator',
    label: 'Bildung',
    icon: GraduationCap,
    enabled: true,
    visible: true,
    badge: 'Demnächst',
    route: '/educator',
    roleVisibility: ['educator'],
  },
  {
    id: 'experimenta',
    label: 'experimenta',
    icon: Sparkles,
    enabled: true,
    visible: true,
    badge: 'Demnächst',
    route: '/experimenta',
    roleVisibility: 'all',
  },
  {
    id: 'labs',
    label: 'Labore',
    icon: FlaskConical,
    enabled: false,
    visible: false,
    badge: 'Demnächst',
    route: '/labs',
    roleVisibility: ['educator', 'company'],
  },
]

const route = useRoute()
const { activeRole } = useActiveRole()

// Filter areas by role visibility
const visibleAreas = computed(() => {
  return areas.filter(area => {
    // Legacy visible flag check
    if (!area.visible) return false

    // No role requirement = visible to all (backward compatible)
    if (!area.roleVisibility) return true

    // Explicitly set to 'all'
    if (area.roleVisibility === 'all') return true

    // Check if user's active role matches
    return area.roleVisibility.includes(activeRole.value as RoleCode)
  })
})

const currentArea = computed(() => {
  // Determine current area based on route - check visibleAreas array dynamically
  const currentPath = route.path

  // Exact match for root path
  if (currentPath === '/') {
    return visibleAreas.value.find(area => area.route === '/')?.id || ''
  }

  // Find area where route path starts with area.route
  const matchedArea = visibleAreas.value.find(area =>
    area.route !== '/' && currentPath.startsWith(area.route)
  )

  return matchedArea?.id || ''
})

function navigateToArea(area: ProductArea) {
  if (area.enabled) {
    navigateTo(area.route)
  }
}
</script>

<template>
  <div class="w-full">
    <!-- Desktop: Tabs -->
    <Tabs :model-value="currentArea" class="hidden md:block">
      <TabsList class="h-auto p-2 bg-white/5">
        <TabsTrigger v-for="area in visibleAreas" :key="area.id" :value="area.id"
          :disabled="!area.enabled" :class="[
            'gap-2 py-3 md:py-4 data-[state=active]:bg-accent data-[state=active]:text-white data-[state=active]:shadow-md',
            !area.enabled && 'opacity-50 cursor-not-allowed',
          ]" @click="navigateToArea(area)">
          <component :is="area.icon" class="h-4 w-4" />
          <span>{{ area.label }}</span>
          <Badge v-if="area.badge" :class="[
            'ml-1 text-[10px] px-1.5 py-0.5 pointer-events-none rounded-[35px]',
            currentArea === area.id
              ? 'bg-white/90 text-purple-950 border-white/50'
              : 'bg-experimenta-accent/20 text-experimenta-accent border-experimenta-accent/30'
          ]">
            {{ area.badge }}
          </Badge>
        </TabsTrigger>
      </TabsList>
    </Tabs>

    <!-- Mobile: Horizontal scroll with cards (matching desktop styling) -->
    <div class="md:hidden overflow-x-auto scrollbar-hide">
      <div class="inline-flex h-auto items-center justify-center rounded-[35px] bg-white/5 p-2 min-w-max">
        <button v-for="area in visibleAreas" :key="area.id" :disabled="!area.enabled" :class="[
          'inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-[25px] px-4 py-3 text-lg font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-0',
          currentArea === area.id
            ? 'bg-accent text-white shadow-md'
            : 'text-white/70 hover:text-white',
          !area.enabled && 'opacity-50 cursor-not-allowed',
        ]" @click="navigateToArea(area)">
          <component :is="area.icon" class="h-4 w-4" />
          <span>{{ area.label }}</span>
          <Badge v-if="area.badge" :class="[
            'ml-1 text-[10px] px-1.5 py-0.5 pointer-events-none rounded-[35px]',
            currentArea === area.id
              ? 'bg-white/90 text-purple-950 border-white/50'
              : 'bg-experimenta-accent/20 text-experimenta-accent border-experimenta-accent/30'
          ]">
            {{ area.badge }}
          </Badge>
        </button>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* Hide scrollbar but keep functionality */
.scrollbar-hide::-webkit-scrollbar {
  display: none;
}

.scrollbar-hide {
  -ms-overflow-style: none;
  scrollbar-width: none;
}
</style>
