<script setup lang="ts">
// <!-- ld-v2 --> /mandats — la carte des pouvoirs confiés : qui peut décider
// quoi, jusqu'à quand. Cartes par statut, compteurs bruts (Δ7 : aucune jauge).
import type { Mandate } from '~/types/domain'
import { useCollectiveStore } from '~/stores/collective'
import { MANDATES_TITLE, MANDATE_CLAIM } from '~/lexicon'
import { MANDATE_STATUS_LABELS } from '~/components/mandates/mandateUi'

const col = useCollectiveStore()

const ORDER: Mandate['status'][] = ['active', 'proposed', 'expired', 'revoked']

const groups = computed(() =>
  ORDER
    .map(status => ({
      status,
      label: MANDATE_STATUS_LABELS[status],
      items: col.mandates
        .filter(m => m.status === status)
        .sort((a, b) => (a.endsAt < b.endsAt ? -1 : 1)),
    }))
    .filter(g => g.items.length > 0),
)
</script>

<template>
  <!-- ld-v2 -->
  <div class="mpage">
    <header class="mpage__header">
      <div>
        <h1 class="mpage__title">{{ MANDATES_TITLE }}</h1>
        <p class="mpage__sub">qui peut décider quoi, jusqu'à quand</p>
      </div>
      <NuxtLink to="/mandats/nouveau" class="ld-btn">
        <UIcon name="i-lucide-key-round" />
        {{ MANDATE_CLAIM }}
      </NuxtLink>
    </header>

    <template v-if="groups.length">
      <section v-for="group in groups" :key="group.status" class="mpage__group">
        <h2 class="mpage__group-title">
          {{ group.label }}
          <span class="mpage__count">{{ group.items.length }}</span>
        </h2>
        <div class="mpage__grid">
          <MandateCard v-for="m in group.items" :key="m.id" :mandate="m" />
        </div>
      </section>
    </template>

    <div v-else class="mpage__empty ld-card">
      <UIcon name="i-lucide-key-round" class="mpage__empty-icon" />
      <p class="mpage__empty-title">Aucun pouvoir confié pour l'instant.</p>
      <p class="mpage__empty-text">
        Un mandat naît d'une décision : un rôle, un domaine, une durée bornée —
        et des comptes à rendre.
      </p>
      <NuxtLink to="/mandats/nouveau" class="ld-btn">
        <UIcon name="i-lucide-key-round" />
        {{ MANDATE_CLAIM }}
      </NuxtLink>
    </div>
  </div>
</template>

<style scoped>
.mpage {
  max-width: 64rem;
  margin: 0 auto;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
}
.mpage__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.mpage__title {
  margin: 0;
  font-size: clamp(1.375rem, 3.5vw, 1.75rem);
  font-weight: 800;
  letter-spacing: -0.01em;
}
.mpage__sub {
  margin: 0.25rem 0 0;
  font-size: 0.9375rem;
  font-style: italic;
  color: var(--mood-text-muted);
}
.mpage__group { display: flex; flex-direction: column; gap: 0.875rem; }
.mpage__group-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
  font-size: 0.8125rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--mood-text-muted);
}
.mpage__count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.375rem;
  height: 1.375rem;
  padding: 0 0.375rem;
  border-radius: var(--r-pill);
  background: var(--mood-accent-soft);
  color: var(--mood-accent);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}
.mpage__grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}
@media (min-width: 768px) {
  .mpage__grid { grid-template-columns: 1fr 1fr; }
}
.mpage__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.625rem;
  padding: 3rem 1.5rem;
  text-align: center;
}
.mpage__empty-icon { font-size: 2rem; color: var(--mood-accent); }
.mpage__empty-title { margin: 0; font-size: 1.0625rem; font-weight: 700; }
.mpage__empty-text {
  margin: 0 0 0.625rem;
  max-width: 26rem;
  font-size: 0.9375rem;
  color: var(--mood-text-muted);
  line-height: 1.55;
}
</style>
