<script setup lang="ts">
// Open vote card — miniature participation gauge + link to the vote room.
// The gauge shows who already spoke, never a result (results live in the room).
import type { Decision, VoteSession } from '~/types/domain'
import { ROUTE_ICONS } from '~/lexicon'
import { useCollectiveStore } from '~/stores/collective'
import { useDecisionsStore } from '~/stores/decisions'

const props = defineProps<{ session: VoteSession; decision?: Decision }>()

const col = useCollectiveStore()
const decisions = useDecisionsStore()

const voted = computed(() => decisions.activeVotes(props.session.id).length)
const total = computed(() => Math.max(props.session.corpusSize, 1))
const pct = computed(() => Math.min(100, Math.round((voted.value / total.value) * 100)))
const iVoted = computed(() =>
  decisions.activeVotes(props.session.id).some(v => v.voterId === col.me?.id),
)
const title = computed(() => props.decision?.title ?? 'Décision en vote')
const roomLink = computed(() =>
  props.decision ? `/decisions/${props.decision.id}/vote` : undefined,
)
</script>

<template>
  <!-- ld-v2 -->
  <FeedItemCard
    :title="title"
    :icon="ROUTE_ICONS.collective"
    tint="var(--route-collective)"
    :to="roomLink"
  >
    <template #meta>
      <LdCountdown :ends-at="session.closesAt" />
      <span>{{ voted }} / {{ total }} se sont prononcés</span>
    </template>

    <div class="fv-gauge" role="img" :aria-label="`Participation : ${pct} %`">
      <div class="fv-gauge__fill" :style="{ width: pct + '%' }" />
    </div>

    <template #actions>
      <NuxtLink v-if="roomLink" :to="roomLink" class="ld-btn fv-btn">
        <UIcon name="i-lucide-door-open" />
        <span>{{ iVoted ? 'Revoir mon vote' : 'Entrer dans la salle' }}</span>
      </NuxtLink>
    </template>
  </FeedItemCard>
</template>

<style scoped>
.fv-gauge {
  height: 6px;
  border-radius: 3px;
  background: var(--mood-status-vote-bg);
  overflow: hidden;
}
.fv-gauge__fill {
  height: 100%;
  border-radius: 3px;
  background: var(--mood-status-vote);
  transition: width 0.3s ease;
}
.fv-btn { font-size: 0.875rem; padding: 0.4rem 1rem; text-decoration: none; }
</style>
