<template>
  <div class="codev-carto">

    <header class="carto-header">
      <h1>Carto entraide</h1>
      <p class="carto-subtitle">
        <template v-if="pending">Chargement...</template>
        <template v-else>
          {{ fiches.length }} fiche{{ fiches.length !== 1 ? 's' : '' }} - clique sur un nom pour voir le detail
        </template>
      </p>
    </header>

    <div class="show-labels-bar">
      <button
        type="button"
        :class="{ active: showLabels }"
        @click="showLabels = !showLabels"
      >
        {{ showLabels ? 'Masquer besoins/offres' : 'Montrer besoins/offres' }}
      </button>
    </div>

    <ClientOnly>
      <CodevGraph
        :fiches="fiches"
        :matches="matches"
        :mode="mode"
        :show-labels="showLabels"
        @select-fiche="onSelectFiche"
      />
      <template #fallback>
        <div class="graph-fallback">Chargement du graphe...</div>
      </template>
    </ClientOnly>

    <!-- Bandeau info mode actif -->
    <div v-if="mode !== 'none'" class="mode-banner">
      <span>
        Mode {{ MODE_LABELS[mode] }} actif -
        {{ matches.length }} connexion{{ matches.length !== 1 ? 's' : '' }} trouvee{{ matches.length !== 1 ? 's' : '' }}.
      </span>
      <button class="banner-clear" @click="setMode('none')" type="button">Effacer</button>
    </div>

    <!-- Boutons matching -->
    <div class="matching-controls">
      <button
        :class="{ active: mode === 'solution' }"
        style="--mode-color: #22c55e"
        @click="setMode('solution')"
        type="button"
      >
        Solution
        <span class="hint">besoin - offre</span>
      </button>
      <button
        :class="{ active: mode === 'alliance' }"
        style="--mode-color: #f97316"
        @click="setMode('alliance')"
        type="button"
      >
        Alliance
        <span class="hint">besoins partages</span>
      </button>
      <button
        :class="{ active: mode === 'surprise' }"
        style="--mode-color: #3b82f6"
        @click="setMode('surprise')"
        type="button"
      >
        Surprise
        <span class="hint">offres partagees</span>
      </button>
      <button
        v-if="mode !== 'none'"
        class="reset"
        @click="setMode('none')"
        type="button"
      >
        Effacer
      </button>
    </div>

    <!-- FAB ajouter une fiche -->
    <NuxtLink to="/codev/fiche" class="fab-add" title="Ajouter ma fiche" aria-label="Ajouter une fiche">
      +
    </NuxtLink>

  </div>
</template>

<script setup lang="ts">
import type { CodevFiche, CodevMatch } from '~/types/codev'
import { computeMatches } from '~/utils/codev/matching'

useHead({ title: 'Carto - Co-developpement' })

const { data, pending } = await useFetch<{ list: CodevFiche[] }>('/api/codev/fiches')
const fiches = computed(() => data.value?.list ?? [])

const matches = ref<CodevMatch[]>([])
const mode = ref<'none' | 'solution' | 'alliance' | 'surprise'>('none')
const showLabels = ref(false)

const MODE_LABELS: Record<string, string> = {
  solution: 'Solution',
  alliance: 'Alliance',
  surprise: 'Surprise',
}

function setMode(newMode: 'none' | 'solution' | 'alliance' | 'surprise') {
  mode.value = newMode
  if (newMode === 'none') {
    matches.value = []
  } else {
    matches.value = computeMatches(fiches.value, newMode)
  }
}

function onSelectFiche(id: number) {
  navigateTo(`/codev/fiche?id=${id}`)
}
</script>

<style scoped>
.codev-carto {
  min-height: 100vh;
  background: var(--nav-bg, #fafafa);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 1rem 2rem;
  gap: 1rem;
  max-width: 100%;
  box-sizing: border-box;
}

/* ── En-tete ── */

.carto-header {
  text-align: center;
  padding-bottom: 0.5rem;
}

.carto-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--nav-text, #1a1a2e);
  margin: 0 0 0.375rem;
}

.carto-subtitle {
  font-size: 0.9rem;
  color: var(--nav-text-muted, #6b7280);
  margin: 0;
}

/* ── Fallback ── */

.graph-fallback {
  width: 100%;
  height: 70vh;
  min-height: 320px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--nav-text-muted, #6b7280);
  font-size: 0.9rem;
  background: var(--nav-bg-alt, #f3f4f6);
  border-radius: 12px;
}

/* ── Bandeau mode actif ── */

.mode-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem 0.875rem;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 8px;
  font-size: 0.875rem;
  color: #166534;
  flex-wrap: wrap;
}

.banner-clear {
  font-size: 0.8rem;
  font-weight: 600;
  color: #166534;
  background: transparent;
  border: 1px solid #166534;
  border-radius: 6px;
  padding: 0.2rem 0.6rem;
  cursor: pointer;
  white-space: nowrap;
}

.banner-clear:hover {
  background: #166534;
  color: #fff;
}

/* ── Boutons matching ── */

.matching-controls {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 8px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(8px);
  border-top: 1px solid #e5e7eb;
  margin: 0 -1rem -2rem;
}

.matching-controls button {
  flex: 1;
  padding: 12px 8px;
  border: 1px solid #d0d4dc;
  border-radius: 8px;
  background: white;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.matching-controls button .hint {
  font-size: 11px;
  color: #6b7280;
  font-weight: normal;
}

.matching-controls button.active {
  background: var(--mode-color, #1B4436);
  color: white;
  border-color: transparent;
}

.matching-controls button.active .hint {
  color: rgba(255, 255, 255, 0.8);
}

.matching-controls button.reset {
  flex: 0 0 auto;
  padding: 12px 16px;
  background: #f3f4f6;
  border-color: #d0d4dc;
  color: #374151;
  font-size: 13px;
}

.matching-controls button.reset:hover {
  background: #e5e7eb;
}

@media (max-width: 500px) {
  .matching-controls {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    margin: 0 -0.75rem -1.5rem;
  }

  .matching-controls button.reset {
    grid-column: span 2;
  }
}

/* ── Toggle besoins/offres ── */

.show-labels-bar {
  display: flex;
  justify-content: center;
  margin-bottom: 8px;
}

.show-labels-bar button {
  border: 1px solid #d0d4dc;
  border-radius: 8px;
  padding: 8px 16px;
  background: white;
  font-size: 13px;
  cursor: pointer;
  color: #374151;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.show-labels-bar button.active {
  background: #1B4436;
  color: white;
  border-color: transparent;
}

/* ── FAB ajouter ── */

.fab-add {
  position: fixed;
  bottom: 80px;
  right: 16px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #1B4436;
  color: white;
  font-size: 28px;
  font-weight: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.18);
  z-index: 100;
  transition: transform 0.15s, opacity 0.15s;
  line-height: 1;
}

.fab-add:hover {
  transform: scale(1.08);
  opacity: 0.92;
}

/* ── Mobile ── */

@media (max-width: 600px) {
  .codev-carto {
    padding: 1rem 0.75rem 1.5rem;
  }

  .carto-header h1 {
    font-size: 1.25rem;
  }
}
</style>
