<template>
  <div class="axis-block">
    <!-- Header -->
    <div class="flex items-center gap-3 mb-6">
      <div class="axis-icon" :class="`axis-icon--${color}`">
        <div :class="iconClass(icon)" class="h-6 w-6" />
      </div>
      <h2 class="font-display text-2xl font-bold text-white">{{ title }}</h2>
    </div>

    <!-- Items grid -->
    <div class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
      <div
        v-for="(item, i) in items"
        :key="i"
        class="axis-item card-surface"
        :class="{ 'axis-item--gestation': item.gestation }"
      >
        <!-- Clickable card body -->
        <component
          :is="itemTag(item)"
          v-bind="itemAttrs(item)"
          class="axis-item-body"
        >
          <!-- Item icon -->
          <div v-if="item.icon" class="axis-item-icon" :class="`axis-item-icon--${color}`">
            <div :class="iconClass(item.icon)" class="h-5 w-5" />
          </div>

          <h3 class="font-display text-lg font-semibold text-white mb-1">
            {{ item.label }}
            <span v-if="item.gestation" class="gestation-badge">
              <div class="i-lucide-flask-conical h-3 w-3" />
              En gestation
            </span>
          </h3>

          <p class="text-sm text-white/60 leading-relaxed">{{ item.description }}</p>
        </component>

        <!-- Actions zone (separate from card link) -->
        <div v-if="item.actions?.length" class="axis-actions">
          <button
            v-for="action in item.actions"
            :key="action.id"
            class="axis-action-btn"
            @click.stop="handleAction(action.id)"
          >
            <div :class="iconClass(action.icon)" class="h-3.5 w-3.5" />
            {{ action.label }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
interface AxisAction {
  id: string
  label: string
  icon: string
}

interface AxisItem {
  label: string
  description: string
  to?: string
  href?: string
  gestation?: boolean
  icon?: string
  actions?: AxisAction[]
  presentation?: { title: string; text: string }
}

defineProps<{
  title: string
  icon: string
  color?: 'primary' | 'accent'
  items: AxisItem[]
}>()

const emit = defineEmits<{
  'open-player': []
  'open-pdf': []
  'launch-gratewizard': []
}>()

function handleAction(id: string) {
  if (id === 'open-player') emit('open-player')
  else if (id === 'open-pdf') emit('open-pdf')
  else if (id === 'launch-gratewizard') emit('launch-gratewizard')
}

function iconClass(name: string) {
  return `i-lucide-${name}`
}

function itemTag(item: AxisItem) {
  if (item.href) return 'a'
  if (item.to) return resolveComponent('NuxtLink')
  return 'div'
}

function itemAttrs(item: AxisItem) {
  if (item.href) return { href: item.href, target: '_blank', rel: 'noopener noreferrer' }
  if (item.to) return { to: item.to }
  return {}
}
</script>

<style scoped>
.axis-block {
  padding: 0;
}

.axis-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 0.75rem;
  flex-shrink: 0;
}

.axis-icon--primary {
  background: hsl(var(--color-primary) / 0.12);
  border: 1px solid hsl(var(--color-primary) / 0.2);
  color: hsl(var(--color-primary));
}

.axis-icon--accent {
  background: hsl(var(--color-accent) / 0.12);
  border: 1px solid hsl(var(--color-accent) / 0.2);
  color: hsl(var(--color-accent));
}

.axis-item {
  display: flex;
  flex-direction: column;
  border-radius: 0.75rem;
  border: 1px solid hsl(var(--color-text) / 0.08);
  background: hsl(var(--color-surface));
  transition: border-color 0.2s, box-shadow 0.2s;
  overflow: hidden;
}

.axis-item:hover {
  border-color: hsl(var(--color-primary) / 0.25);
  box-shadow: 0 4px 24px hsl(var(--color-primary) / 0.06);
}

.axis-item--gestation {
  opacity: 0.75;
}

.axis-item--gestation:hover {
  opacity: 0.9;
}

.axis-item-body {
  display: flex;
  flex-direction: column;
  padding: 1.25rem;
  flex: 1;
  text-decoration: none;
  color: inherit;
}

.axis-item-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 0.5rem;
  margin-bottom: 0.75rem;
}

.axis-item-icon--primary {
  background: hsl(var(--color-primary) / 0.1);
  color: hsl(var(--color-primary));
}

.axis-item-icon--accent {
  background: hsl(var(--color-accent) / 0.1);
  color: hsl(var(--color-accent));
}

.gestation-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  margin-left: 0.5rem;
  padding: 0.125rem 0.5rem;
  border-radius: 9999px;
  background: hsl(var(--color-accent) / 0.12);
  color: hsl(var(--color-accent));
  font-size: 0.7rem;
  font-weight: 500;
  font-family: var(--font-mono);
  vertical-align: middle;
}

.axis-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid hsl(var(--color-text) / 0.06);
  background: hsl(var(--color-bg) / 0.4);
}

.axis-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem 0.75rem;
  border-radius: 0.5rem;
  font-size: 0.8rem;
  font-weight: 500;
  color: hsl(var(--color-text) / 0.7);
  background: hsl(var(--color-text) / 0.05);
  border: 1px solid hsl(var(--color-text) / 0.1);
  transition: all 0.2s;
  cursor: pointer;
}

.axis-action-btn:hover {
  color: hsl(var(--color-text));
  background: hsl(var(--color-primary) / 0.12);
  border-color: hsl(var(--color-primary) / 0.3);
}
</style>
