<script setup lang="ts">
// <!-- ld-v2 --> MON historique de re-votes — la chaîne supersedes n'est jamais
// publique : elle n'est visible que de son auteur·e, ici, dépliée à la demande.
import { useCollectiveStore } from '~/stores/collective'
import { BLANK_VOTE, NUANCED_LABELS } from '~/lexicon'
import type { Id, NuancedValue, Vote, VoteSession } from '~/types/domain'

const props = defineProps<{ session: VoteSession; voterId: Id }>()
const col = useCollectiveStore()

const mine = computed<Vote[]>(() =>
  col.votes
    .filter(v => v.sessionId === props.session.id && v.voterId === props.voterId)
    .sort((a, b) => (a.createdAt < b.createdAt ? 1 : -1)),
)

function label(vote: Vote): string {
  if (vote.value === 'for') return 'pour'
  if (vote.value === 'against') return 'refus argumenté'
  if (typeof vote.value === 'number') return NUANCED_LABELS[vote.value as NuancedValue]
  if (vote.values) return vote.values.map(v => v.toLocaleString('fr-FR', { maximumFractionDigits: 2 })).join(' · ')
  if (vote.choicePersonId) return col.people.find(p => p.id === vote.choicePersonId)?.displayName ?? '—'
  return BLANK_VOTE
}
function when(iso: string): string {
  return new Date(iso).toLocaleString('fr-FR', { day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit' })
}
</script>

<template>
  <!-- ld-v2 -->
  <details v-if="mine.length" class="vh">
    <summary>
      <UIcon name="i-lucide-history" />
      <span>Mon historique de re-votes ({{ mine.length }}) — visible par moi seul</span>
    </summary>
    <ol class="vh__list">
      <li v-for="(vote, i) in mine" :key="vote.id" :class="{ 'vh__old': i > 0 }">
        <span class="vh__when">{{ when(vote.createdAt) }}</span>
        <span class="vh__label">{{ label(vote) }}</span>
        <span v-if="i === 0" class="vh__active">actif</span>
        <span v-if="vote.comment" class="vh__comment">{{ vote.comment }}</span>
      </li>
    </ol>
  </details>
</template>

<style scoped>
.vh { background: var(--mood-bg); border-radius: var(--r-input); padding: 0.6rem 0.9rem; }
.vh summary {
  display: flex; align-items: center; gap: 0.45rem; cursor: pointer; user-select: none;
  font-size: 0.8125rem; font-weight: 700; color: var(--mood-text-muted);
}
.vh__list { list-style: none; margin: 0.6rem 0 0; padding: 0; display: flex; flex-direction: column; gap: 0.4rem; }
.vh__list li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.5rem; font-size: 0.875rem; }
.vh__old { opacity: 0.6; }
.vh__old .vh__label { text-decoration: line-through; }
.vh__when { font-size: 0.75rem; color: var(--mood-text-muted); font-variant-numeric: tabular-nums; }
.vh__label { font-weight: 700; }
.vh__active {
  font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--mood-status-vigueur); background: var(--mood-status-vigueur-bg);
  padding: 1px 8px; border-radius: var(--r-pill);
}
.vh__comment { width: 100%; color: var(--mood-text-muted); font-size: 0.8125rem; }
</style>
