<script setup lang="ts">
// <!-- ld-v2 --> Provenance d'un document — le bloc de genèse porté du v1 :
// sources datées, vote d'origine (résultats bruts, période, lien), contributeurs,
// notes (les typos des textes votés sont signalées, jamais corrigées).
// Aucune lettre de formule ici : « comprendre ce seuil » mène à l'Atelier pré-réglé.
import type { Provenance } from '~/types/domain'
import { parseModeParams } from '~/engine'
import { formatInt } from './textsModel'

const props = defineProps<{ provenance: Provenance }>()

const record = computed(() => props.provenance.voteRecord)

/** Query Atelier pré-réglée depuis le vote d'origine (lettres côté URL seulement). */
const atelierLink = computed(() => {
  const r = record.value
  if (!r) return null
  const p = parseModeParams(r.modeParams)
  const query = new URLSearchParams({
    W: String(r.result.wotSize),
    T: String(r.result.for + r.result.against),
    M: String(p.majority_pct),
    B: String(p.base_exponent),
    G: String(p.gradient_exponent),
    C: String(p.constant_base),
  })
  if (p.smith_exponent !== null) query.set('S', String(p.smith_exponent))
  return `/textes/formules?${query.toString()}`
})

const sourcesOpen = ref(false)
</script>

<template>
  <!-- ld-v2 -->
  <section class="prov ld-card">
    <header class="prov__header">
      <UIcon name="i-lucide-landmark" class="prov__header-icon" />
      <div>
        <h2 class="prov__title">Provenance</h2>
        <p class="prov__sub">d'où vient ce texte, et par quel geste il fait loi</p>
      </div>
    </header>

    <!-- Vote d'origine -->
    <div v-if="record" class="prov__vote">
      <p class="prov__vote-heading">Le vote d'origine</p>
      <div class="prov__vote-figures">
        <span class="prov__figure prov__figure--for">{{ formatInt(record.result.for) }} pour</span>
        <span class="prov__figure prov__figure--against">{{ formatInt(record.result.against) }} contre</span>
        <span v-if="record.result.invalid" class="prov__figure prov__figure--muted">
          {{ formatInt(record.result.invalid) }} invalides
        </span>
      </div>
      <p class="prov__vote-line">
        Toile de {{ formatInt(record.result.wotSize) }} membres —
        seuil requis : {{ formatInt(record.result.thresholdRequired) }} pour.
        Résultat : <strong>{{ record.result.status }}</strong>.
      </p>
      <p class="prov__vote-line prov__vote-line--muted">Période : {{ record.period }}</p>
      <div class="prov__vote-actions">
        <NuxtLink v-if="atelierLink" :to="atelierLink" class="ld-btn ld-btn--ghost prov__btn">
          <UIcon name="i-lucide-flask-conical" />
          Comprendre ce seuil
        </NuxtLink>
        <a :href="record.url" target="_blank" rel="noopener" class="ld-btn ld-btn--quiet prov__btn">
          <UIcon name="i-lucide-external-link" />
          Le fil du vote
        </a>
      </div>
    </div>

    <!-- Sources -->
    <div v-if="provenance.sources.length" class="prov__block">
      <button type="button" class="prov__toggle" @click="sourcesOpen = !sourcesOpen">
        <UIcon name="i-lucide-library" />
        {{ provenance.sources.length }} source{{ provenance.sources.length > 1 ? 's' : '' }}
        <UIcon
          name="i-lucide-chevron-down"
          class="prov__chevron"
          :class="{ 'prov__chevron--open': sourcesOpen }"
        />
      </button>
      <ul v-show="sourcesOpen" class="prov__sources">
        <li v-for="source in provenance.sources" :key="source.url" class="prov__source">
          <a :href="source.url" target="_blank" rel="noopener" class="prov__source-link">
            {{ source.title }}
          </a>
          <span v-if="source.version" class="prov__source-meta">v{{ source.version }}</span>
          <span v-if="source.date" class="prov__source-meta">{{ source.date }}</span>
        </li>
      </ul>
    </div>

    <!-- Contributeurs -->
    <div v-if="provenance.contributors?.length" class="prov__block">
      <p class="prov__block-heading">
        <UIcon name="i-lucide-users" />
        Contributeurs
      </p>
      <ul class="prov__contributors">
        <li v-for="person in provenance.contributors" :key="person" class="prov__contributor">
          {{ person }}
        </li>
      </ul>
    </div>

    <!-- Notes -->
    <details v-if="provenance.notes" class="prov__notes">
      <summary class="prov__notes-summary">
        <UIcon name="i-lucide-sticky-note" />
        Notes de provenance — les coquilles des textes votés sont signalées, jamais corrigées
      </summary>
      <p class="prov__notes-body">{{ provenance.notes }}</p>
    </details>
  </section>
</template>

<style scoped>
.prov {
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  padding: clamp(1rem, 3vw, 1.5rem);
}
.prov__header { display: flex; align-items: center; gap: 0.75rem; }
.prov__header-icon {
  font-size: 1.4rem;
  color: var(--mood-accent);
  background: var(--mood-accent-soft);
  border-radius: var(--r-icon);
  padding: 0.5rem;
  box-sizing: content-box;
}
.prov__title { font-size: 1.05rem; font-weight: 800; color: var(--mood-text); }
.prov__sub { font-size: 0.8125rem; color: var(--mood-text-muted); }

.prov__vote {
  border-radius: var(--r-input);
  background: color-mix(in srgb, var(--mood-accent) 5%, transparent);
  padding: 0.9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.prov__vote-heading {
  font-size: 0.6875rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--mood-text-muted);
}
.prov__vote-figures { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.prov__figure {
  padding: 3px 11px;
  border-radius: var(--r-pill);
  font-size: 0.8125rem;
  font-weight: 700;
}
.prov__figure--for { background: var(--mood-status-vigueur-bg); color: var(--mood-status-vigueur); }
.prov__figure--against { background: var(--mood-status-revoque-bg); color: var(--mood-status-revoque); }
.prov__figure--muted { background: var(--mood-status-clos-bg); color: var(--mood-status-clos); }
.prov__vote-line { font-size: 0.875rem; color: var(--mood-text); }
.prov__vote-line--muted { font-size: 0.8125rem; color: var(--mood-text-muted); }
.prov__vote-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.25rem; }
.prov__btn { font-size: 0.8125rem; min-height: 2rem; padding: 0.35rem 0.9rem; text-decoration: none; }

.prov__block { display: flex; flex-direction: column; gap: 0.5rem; }
.prov__block-heading,
.prov__toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.875rem;
  font-weight: 700;
  color: var(--mood-text);
}
.prov__toggle { background: none; cursor: pointer; padding: 0; align-self: flex-start; }
.prov__chevron { transition: transform 0.2s ease; color: var(--mood-text-muted); }
.prov__chevron--open { transform: rotate(180deg); }

.prov__sources { display: flex; flex-direction: column; gap: 0.3rem; }
.prov__source { display: flex; align-items: baseline; gap: 0.5rem; flex-wrap: wrap; }
.prov__source-link {
  font-size: 0.8125rem;
  color: var(--mood-accent);
  text-decoration: none;
}
.prov__source-link:hover { text-decoration: underline; }
.prov__source-meta { font-size: 0.6875rem; color: var(--mood-text-muted); font-family: monospace; }

.prov__contributors { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.prov__contributor {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: var(--r-pill);
  background: color-mix(in srgb, var(--mood-secondary) 12%, transparent);
  color: var(--mood-secondary);
}

.prov__notes-summary {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--mood-text-muted);
  cursor: pointer;
  user-select: none;
}
.prov__notes-body {
  margin-top: 0.6rem;
  font-size: 0.8125rem;
  line-height: 1.6;
  color: var(--mood-text-muted);
  white-space: pre-wrap;
}
</style>
