<script setup lang="ts">
// <!-- ld-v2 --> Session block: link to the vote room, arrested-list sentence
// (« Qui vote : N personnes — liste arrêtée le … »), frozen banner while the
// steward's crystallization gesture is awaited, outcome once closed.
import type { Decision } from '~/types/domain'
import { FROZEN_BANNER, WHO_VOTES } from '~/lexicon'
import { useCollectiveStore } from '~/stores/collective'
import { dateFr } from './decisionUi'

const props = defineProps<{ decision: Decision }>()

const col = useCollectiveStore()

const session = computed(() =>
  col.sessions
    .filter(s => s.decisionId === props.decision.id)
    .sort((a, b) => (a.opensAt < b.opensAt ? 1 : -1))[0])

const protocol = computed(() =>
  session.value
    ? col.protocols.find(p => p.id === session.value!.protocolId)
    : undefined)

const whoVotes = computed(() =>
  session.value
    ? WHO_VOTES(session.value.corpusSize, dateFr(session.value.opensAt))
    : '')

const crystallizedBy = computed(() => {
  const s = session.value
  if (!s?.crystallizedById) return ''
  const who = col.people.find(p => p.id === s.crystallizedById)?.displayName ?? '—'
  return `Cristallisée par ${who} le ${dateFr(s.crystallizedAt)}`
})

const outcomeLabel = computed(() => {
  switch (session.value?.outcome) {
    case 'adopted': return 'adoptée'
    case 'rejected': return 'rejetée'
    case 'tie': return 'égalité — vous départagez, jamais l\'outil'
    default: return ''
  }
})
</script>

<template>
  <!-- ld-v2 -->
  <section v-if="session" class="ses">
    <div class="ses__head">
      <h2 class="ses__title">Session de vote</h2>
      <span v-if="protocol" class="ses__protocol">{{ protocol.name }}</span>
    </div>

    <p class="ses__who">{{ whoVotes }}</p>

    <p v-if="session.status === 'frozen'" class="ses__frozen">
      <UIcon name="i-lucide-stamp" />
      <span>{{ FROZEN_BANNER }}</span>
    </p>

    <template v-if="session.status === 'closed'">
      <p class="ses__result">
        Résultat : <strong>{{ outcomeLabel }}</strong>
        <span class="ses__result-date">— clôture le {{ dateFr(session.closesAt) }}</span>
      </p>
      <p v-if="crystallizedBy" class="ses__crystal">{{ crystallizedBy }}</p>
    </template>

    <div v-else class="ses__actions no-print">
      <NuxtLink :to="`/decisions/${decision.id}/vote`" class="ld-btn">
        <UIcon name="i-lucide-vote" />
        <span>{{ session.status === 'open' ? 'Entrer dans la salle de vote' : 'Voir la salle de vote' }}</span>
      </NuxtLink>
      <LdCountdown v-if="session.status === 'open'" :ends-at="session.closesAt" />
    </div>
  </section>
</template>

<style scoped>
.ses { display: flex; flex-direction: column; gap: 0.75rem; }
.ses__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.ses__title {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 800;
  letter-spacing: -0.01em;
}
.ses__protocol {
  font-size: 0.8125rem;
  font-weight: 700;
  color: var(--mood-status-vote);
  background: var(--mood-status-vote-bg);
  padding: 3px 10px;
  border-radius: var(--r-pill);
}
.ses__who {
  margin: 0;
  font-size: 0.9375rem;
  font-weight: 600;
}
.ses__frozen {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: fit-content;
  padding: 0.5rem 0.875rem;
  border-radius: var(--r-input);
  font-weight: 700;
  font-size: 0.875rem;
  color: var(--mood-status-fige);
  background: var(--mood-status-fige-bg);
}
.ses__result { margin: 0; font-size: 0.9375rem; }
.ses__result-date { color: var(--mood-text-muted); font-size: 0.875rem; }
.ses__crystal {
  margin: 0;
  font-size: 0.875rem;
  color: var(--mood-text-muted);
}
.ses__actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.625rem;
}
</style>
