<script setup lang="ts">
// « L'épreuve du réel » card — « Le réel a-t-il suivi ? », three inline answers.
import type { Decision } from '~/types/domain'
import { REVIEW_QUESTION, REVIEW_VERDICTS } from '~/lexicon'
import { useDecisionsStore } from '~/stores/decisions'

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

const decisions = useDecisionsStore()
const error = ref('')
const answered = ref<'confirmed' | 'revise' | 'revoke' | null>(null)

// Aliased: the lexicon export name is a code identifier, kept out of templates.
const ANSWER_LABELS = REVIEW_VERDICTS
const ANSWERS = [
  { value: 'confirmed', icon: 'i-lucide-check' },
  { value: 'revise', icon: 'i-lucide-pencil' },
  { value: 'revoke', icon: 'i-lucide-undo-2' },
] as const

function answer(value: 'confirmed' | 'revise' | 'revoke') {
  const res = decisions.reviewVerdict(props.decision.id, value)
  if ('ok' in res) {
    error.value = res.reason
    return
  }
  error.value = ''
  answered.value = value
}
</script>

<template>
  <!-- ld-v2 -->
  <FeedItemCard
    :title="decision.title"
    icon="i-lucide-flask-conical"
    tint="var(--mood-tertiary)"
    :to="`/decisions/${decision.id}`"
  >
    <template #meta>
      <span class="fr-question">{{ REVIEW_QUESTION }}</span>
    </template>

    <p v-if="error" class="fr-error">{{ error }}</p>

    <template #actions>
      <span v-if="answered" class="fr-done">
        <UIcon name="i-lucide-check" /> {{ ANSWER_LABELS[answered] }} — c'est noté
      </span>
      <template v-else>
        <button
          v-for="a in ANSWERS"
          :key="a.value"
          class="ld-btn ld-btn--ghost fr-btn"
          type="button"
          @click="answer(a.value)"
        >
          <UIcon :name="a.icon" />
          <span>{{ ANSWER_LABELS[a.value] }}</span>
        </button>
      </template>
    </template>
  </FeedItemCard>
</template>

<style scoped>
.fr-question { font-weight: 700; font-style: italic; color: var(--mood-tertiary, var(--mood-accent)); }
.fr-error { margin: 0; font-size: 0.8125rem; color: var(--mood-error); }
.fr-done {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--mood-status-vigueur);
}
.fr-btn { font-size: 0.875rem; padding: 0.4rem 0.875rem; }
</style>
