<script setup lang="ts">
// Advice request card — three inline positions, one tap each.
import type { Advice, Decision } from '~/types/domain'
import { ROUTE_ICONS } from '~/lexicon'
import { useCollectiveStore } from '~/stores/collective'
import { useDecisionsStore } from '~/stores/decisions'

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

const col = useCollectiveStore()
const decisions = useDecisionsStore()
const error = ref('')

const POSITIONS: { value: Advice['position']; label: string; icon: string }[] = [
  { value: 'favorable', label: 'Favorable', icon: 'i-lucide-thumbs-up' },
  { value: 'reserved', label: 'Réservé', icon: 'i-lucide-minus' },
  { value: 'unfavorable', label: 'Défavorable', icon: 'i-lucide-thumbs-down' },
]

const mine = computed(() =>
  col.advices.find(a => a.decisionId === props.decision.id && a.personId === col.me?.id),
)
const mineLabel = computed(
  () => POSITIONS.find(p => p.value === mine.value?.position)?.label ?? '',
)

function give(position: Advice['position']) {
  const res = decisions.adviseOn(props.decision.id, position)
  error.value = 'ok' in res ? res.reason : ''
}
</script>

<template>
  <!-- ld-v2 -->
  <FeedItemCard
    :title="decision.title"
    :icon="ROUTE_ICONS.advice"
    tint="var(--route-advice)"
    :to="`/decisions/${decision.id}`"
  >
    <template #meta>
      <LdCountdown :ends-at="decision.windowEndsAt" :suspended-at="decision.windowSuspendedAt" />
    </template>

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

    <template #actions>
      <span v-if="mine" class="fa-done">
        <UIcon name="i-lucide-check" /> Ton avis est déposé — {{ mineLabel }}
      </span>
      <template v-else>
        <button
          v-for="p in POSITIONS"
          :key="p.value"
          class="ld-btn ld-btn--ghost fa-btn"
          type="button"
          @click="give(p.value)"
        >
          <UIcon :name="p.icon" />
          <span>{{ p.label }}</span>
        </button>
      </template>
    </template>
  </FeedItemCard>
</template>

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