<script setup lang="ts">
// <!-- ld-v2 --> Carte mandat — titulaire, domaine, échéance, prochain rapport,
// COMPTEURS BRUTS (Δ7 : aucune jauge, aucun agrégat — des faits comptés).
import type { Mandate } from '~/types/domain'
import { useCollectiveStore } from '~/stores/collective'
import {
  MANDATE_STATUS_LABELS,
  MANDATE_STATUS_PILL,
  formatDay,
  mandateFacts,
} from './mandateUi'

const props = defineProps<{ mandate: Mandate }>()

const col = useCollectiveStore()

const holder = computed(() => col.people.find(p => p.id === props.mandate.holderId))
const domainCircles = computed(() =>
  props.mandate.domain.circleIds
    .map(id => col.circles.find(c => c.id === id))
    .filter(c => c !== undefined),
)
const facts = computed(() => mandateFacts(props.mandate, col.decisions, col.objections))
</script>

<template>
  <!-- ld-v2 -->
  <NuxtLink :to="`/mandats/${mandate.id}`" class="mcard ld-card ld-card--hover">
    <div class="mcard__head">
      <LdAvatarStack v-if="holder" :people="[{ person: holder }]" :size="34" />
      <div class="mcard__who">
        <p class="mcard__title">{{ mandate.title }}</p>
        <p v-if="holder" class="mcard__holder">{{ holder.displayName }}</p>
      </div>
      <span class="status-pill" :class="MANDATE_STATUS_PILL[mandate.status]">
        {{ MANDATE_STATUS_LABELS[mandate.status] }}
      </span>
    </div>

    <div class="mcard__domain">
      <span v-for="c in domainCircles" :key="c.id" class="mcard__chip">
        <UIcon name="i-lucide-circle-dashed" />
        {{ c.name }}
      </span>
      <span v-for="tag in mandate.domain.tags" :key="tag" class="mcard__tag">#{{ tag }}</span>
    </div>

    <div class="mcard__meta">
      <span class="mcard__meta-item">
        <UIcon name="i-lucide-calendar-range" />
        jusqu'au {{ formatDay(mandate.endsAt) }}
      </span>
      <span v-if="facts.nextReportDueAt" class="mcard__meta-item mcard__meta-item--due">
        <UIcon name="i-lucide-file-clock" />
        rapport dû le {{ formatDay(facts.nextReportDueAt) }}
      </span>
    </div>

    <p class="mcard__facts">{{ facts.line }}</p>
  </NuxtLink>
</template>

<style scoped>
.mcard {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.125rem 1.25rem;
  text-decoration: none;
  color: var(--mood-text);
}
.mcard__head {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
}
.mcard__who { flex: 1; min-width: 0; }
.mcard__title {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 700;
  line-height: 1.3;
}
.mcard__holder {
  margin: 0.125rem 0 0;
  font-size: 0.8125rem;
  color: var(--mood-text-muted);
}
.mcard__domain {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  align-items: center;
}
.mcard__chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--mood-accent);
  background: var(--mood-accent-soft);
  padding: 2px 10px;
  border-radius: var(--r-pill);
}
.mcard__tag {
  font-size: 0.8125rem;
  color: var(--mood-text-muted);
}
.mcard__meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem 1rem;
}
.mcard__meta-item {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.8125rem;
  color: var(--mood-text-muted);
}
.mcard__meta-item--due { color: var(--mood-status-fenetre); font-weight: 600; }
.mcard__facts {
  margin: 0;
  padding-top: 0.625rem;
  font-size: 0.875rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--mood-text);
  box-shadow: 0 -1px 0 color-mix(in srgb, var(--mood-accent) 10%, transparent);
}
</style>
