<script setup lang="ts">
/**
 * /textes — la bibliothèque des documents sous vote permanent.
 * Pacte épinglé en tête, cartes documents (clauses, sessions en cours,
 * provenance résumée), lien vers l'Atelier des formules, filtre gravées 井.
 */
import type { TextDoc } from '~/types/domain'
import { useCollectiveStore } from '~/stores/collective'
import { PACT_BADGE, PACT_SUBTITLE } from '~/lexicon'
import {
  engravedCount,
  liveClauseGauges,
  type ClauseSessionGauge,
} from '~/components/texts/textsModel'

const store = useCollectiveStore()

interface DocEntry {
  doc: TextDoc
  clauseCount: number
  gauges: ClauseSessionGauge[]
  engraved: number
}

function entryFor(doc: TextDoc): DocEntry {
  const clauses = store.clauses.filter(c => c.docId === doc.id)
  return {
    doc,
    clauseCount: clauses.length,
    gauges: liveClauseGauges(clauses, store.decisions, store.sessions, store.votes),
    engraved: engravedCount(clauses, store.decisions),
  }
}

const pactEntry = computed((): DocEntry | null =>
  store.pactDoc ? entryFor(store.pactDoc) : null)

const referenceEntries = computed((): DocEntry[] =>
  store.docs
    .filter(d => d.id !== store.pactDoc?.id)
    .sort((a, b) => a.title.localeCompare(b.title, 'fr'))
    .map(entryFor))

// ── Filtre gravées 井 ──
const onlyEngraved = ref(false)
const visibleEntries = computed(() =>
  onlyEngraved.value
    ? referenceEntries.value.filter(entry => entry.engraved > 0)
    : referenceEntries.value)
const pactVisible = computed(() =>
  pactEntry.value !== null && (!onlyEngraved.value || pactEntry.value.engraved > 0))

const liveSessionTotal = computed(() =>
  (pactEntry.value?.gauges.length ?? 0)
  + referenceEntries.value.reduce((n, entry) => n + entry.gauges.length, 0))
</script>

<template>
  <!-- ld-v2 -->
  <div class="texts-page">
    <header class="texts-page__header">
      <div>
        <h1 class="texts-page__title">Textes</h1>
        <p class="texts-page__subtitle">la bibliothèque des documents sous vote permanent</p>
      </div>
      <button
        type="button"
        class="texts-page__filter"
        :class="{ 'texts-page__filter--on': onlyEngraved }"
        @click="onlyEngraved = !onlyEngraved"
      >
        <span class="texts-page__filter-seal">井</span>
        gravées
      </button>
    </header>

    <p v-if="liveSessionTotal > 0" class="texts-page__pulse">
      <UIcon name="i-lucide-activity" />
      {{ liveSessionTotal }} session{{ liveSessionTotal > 1 ? 's' : '' }} de vote en cours
      sur ces textes — le document vit.
    </p>

    <!-- Le Pacte, épinglé -->
    <section v-if="pactVisible && pactEntry" class="texts-page__pact">
      <div class="texts-page__pact-banner">
        <UIcon name="i-lucide-pin" class="texts-page__pin" />
        <span class="texts-page__pact-badge">{{ PACT_BADGE }}</span>
        <span class="texts-page__pact-subtitle">{{ PACT_SUBTITLE }}</span>
      </div>
      <TextDocCard
        :doc="pactEntry.doc"
        :clause-count="pactEntry.clauseCount"
        :gauges="pactEntry.gauges"
        :engraved="pactEntry.engraved"
        pinned
      />
    </section>

    <!-- Les documents de référence -->
    <section v-if="visibleEntries.length" class="texts-page__grid">
      <TextDocCard
        v-for="entry in visibleEntries"
        :key="entry.doc.id"
        :doc="entry.doc"
        :clause-count="entry.clauseCount"
        :gauges="entry.gauges"
        :engraved="entry.engraved"
      />
    </section>

    <p v-else-if="onlyEngraved" class="texts-page__empty">
      Aucun texte gravé pour l'instant — la gravure vient avec les décisions qui comptent.
    </p>
    <p v-else-if="!pactEntry" class="texts-page__empty">
      Aucun texte ici pour l'instant — les documents naissent des décisions.
    </p>

    <!-- L'Atelier des formules -->
    <NuxtLink to="/textes/formules" class="texts-page__atelier ld-card ld-card--hover">
      <span class="texts-page__atelier-icon">
        <UIcon name="i-lucide-flask-conical" />
      </span>
      <span class="texts-page__atelier-text">
        <span class="texts-page__atelier-title">L'Atelier des formules</span>
        <span class="texts-page__atelier-sub">
          la pédagogie du seuil — pourquoi 94 pour quand 120 votent sur 7 224
        </span>
      </span>
      <UIcon name="i-lucide-arrow-right" class="texts-page__atelier-arrow" />
    </NuxtLink>
  </div>
</template>

<style scoped>
.texts-page {
  display: flex;
  flex-direction: column;
  gap: clamp(1rem, 3vw, 1.5rem);
  max-width: 56rem;
  margin: 0 auto;
  padding-bottom: 4rem;
}

.texts-page__header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.texts-page__title {
  font-size: clamp(1.5rem, 5vw, 1.9rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--mood-text);
}
.texts-page__subtitle { font-size: 0.875rem; color: var(--mood-text-muted); margin-top: 2px; }

.texts-page__filter {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  min-height: 2.25rem;
  padding: 0.35rem 1rem;
  border-radius: var(--r-pill);
  font-size: 0.8125rem;
  font-weight: 700;
  background: var(--mood-surface);
  color: var(--mood-text-muted);
  box-shadow: var(--shadow-card);
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}
.texts-page__filter:hover { transform: translateY(-1px); }
.texts-page__filter--on {
  background: var(--mood-status-vigueur-bg);
  color: var(--mood-status-vigueur);
  box-shadow: 0 0 0 2px var(--mood-status-vigueur);
}
.texts-page__filter-seal { font-size: 1rem; font-weight: 800; }

.texts-page__pulse {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  align-self: flex-start;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--mood-status-vote);
  background: var(--mood-status-vote-bg);
  padding: 0.35rem 0.9rem;
  border-radius: var(--r-pill);
}

.texts-page__pact { display: flex; flex-direction: column; gap: 0.5rem; }
.texts-page__pact-banner {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.texts-page__pin { color: var(--mood-accent); font-size: 0.95rem; }
.texts-page__pact-badge {
  font-size: 0.6875rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  padding: 3px 10px;
  border-radius: var(--r-pill);
  background: var(--mood-accent);
  color: var(--mood-accent-text);
}
.texts-page__pact-subtitle {
  font-size: 0.8125rem;
  font-style: italic;
  color: var(--mood-text-muted);
}

.texts-page__grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.9rem;
}
@media (min-width: 768px) { .texts-page__grid { grid-template-columns: 1fr 1fr; } }

.texts-page__empty {
  font-size: 0.875rem;
  color: var(--mood-text-muted);
  font-style: italic;
  padding: 1rem 0;
}

.texts-page__atelier {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: clamp(0.9rem, 3vw, 1.2rem);
  text-decoration: none;
  color: inherit;
}
.texts-page__atelier-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: var(--r-icon);
  background: var(--mood-accent-soft);
  color: var(--mood-accent);
  font-size: 1.3rem;
  flex-shrink: 0;
}
.texts-page__atelier-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.texts-page__atelier-title { font-size: 1rem; font-weight: 800; color: var(--mood-text); }
.texts-page__atelier-sub { font-size: 0.8125rem; color: var(--mood-text-muted); }
.texts-page__atelier-arrow {
  margin-left: auto;
  color: var(--mood-text-muted);
  flex-shrink: 0;
}
</style>
