<script setup lang="ts">
/**
 * SectionLayout — Mise en page pour sections (documents, decisions, mandats).
 *
 * Structure : titre + status pills + grille (contenu + toolbox).
 * Responsive : toolbox passe sous le contenu sur mobile.
 */

export interface StatusFilter {
  id: string
  label: string
  count: number
  cssClass?: string
}

export interface ToolboxItem {
  title: string
  description: string
  actions: Array<{
    label: string
    to?: string
    onClick?: () => void
  }>
}

const props = withDefaults(
  defineProps<{
    title: string
    subtitle?: string
    statuses: StatusFilter[]
    toolboxItems?: ToolboxItem[]
    activeStatus?: string | null
  }>(),
  {
    subtitle: undefined,
    toolboxItems: undefined,
    activeStatus: null,
  },
)

const emit = defineEmits<{
  'update:activeStatus': [status: string | null]
}>()

const statusCssMap: Record<string, string> = {
  draft: 'status-prepa',
  qualification: 'status-prepa',
  candidacy: 'status-prepa',
  voting: 'status-vote',
  review: 'status-vote',
  active: 'status-vigueur',
  executed: 'status-vigueur',
  completed: 'status-vigueur',
  closed: 'status-clos',
  archived: 'status-clos',
  revoked: 'status-clos',
  reporting: 'status-vote',
}

function getStatusClass(status: StatusFilter): string {
  return status.cssClass || statusCssMap[status.id] || 'status-prepa'
}

function toggleStatus(statusId: string) {
  if (props.activeStatus === statusId) {
    emit('update:activeStatus', null)
  }
  else {
    emit('update:activeStatus', statusId)
  }
}
</script>

<template>
  <div class="section">
    <!-- Header -->
    <div class="section__header">
      <div class="section__title-block">
        <h1 class="section__title">{{ title }}</h1>
        <p v-if="subtitle" class="section__subtitle">{{ subtitle }}</p>
      </div>

      <div v-if="statuses.length > 0" class="section__pills">
        <button
          v-for="status in statuses"
          :key="status.id"
          type="button"
          class="status-pill"
          :class="[getStatusClass(status), { active: activeStatus === status.id }]"
          @click="toggleStatus(status.id)"
        >
          {{ status.label }}
          <span v-if="status.count > 0" class="section__pill-count">{{ status.count }}</span>
        </button>
      </div>
    </div>

    <!-- Body: content + toolbox -->
    <div class="section__body">
      <div class="section__main">
        <div v-if="$slots.search" class="section__search">
          <slot name="search" />
        </div>
        <div class="section__content">
          <slot />
        </div>
        <div v-if="$slots.empty" class="section__empty">
          <slot name="empty" />
        </div>
      </div>

      <aside class="section__toolbox">
        <div class="section__toolbox-head">
          <UIcon name="i-lucide-wrench" class="text-xs" />
          <span>Boite a outils</span>
        </div>
        <div v-if="$slots.toolbox" class="section__toolbox-body">
          <slot name="toolbox" />
        </div>
        <div v-else-if="toolboxItems && toolboxItems.length > 0" class="section__toolbox-body">
          <ToolboxVignette
            v-for="(item, idx) in toolboxItems"
            :key="idx"
            :title="item.title"
            :description="item.description"
          />
        </div>
        <div v-else class="section__toolbox-empty">
          Aucun outil disponible
        </div>
      </aside>
    </div>
  </div>
</template>

<style scoped>
.section {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.section__header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.section__title-block {
  flex: 1;
  min-width: 0;
}

.section__title {
  font-size: 1.375rem;
  font-weight: 700;
  color: var(--mood-text);
  letter-spacing: -0.01em;
}

.section__subtitle {
  margin-top: 0.125rem;
  font-size: 0.8125rem;
  color: var(--mood-text-muted);
}

.section__pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  align-items: center;
}

.section__pill-count {
  margin-left: 0.25rem;
  font-size: 0.5625rem;
  font-weight: 700;
  opacity: 0.7;
}

.section__body {
  display: grid;
  grid-template-columns: 1fr 16rem;
  gap: 1.25rem;
  align-items: start;
}

.section__main {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-width: 0;
}

.section__search {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.section__content {
  min-height: 12rem;
}

.section__toolbox {
  position: sticky;
  top: 4rem;
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  background: var(--mood-surface);
  border: 1px solid var(--mood-border);
  border-radius: 6px;
  padding: 0.875rem;
}

.section__toolbox-head {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.6875rem;
  font-weight: 700;
  color: var(--mood-accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.section__toolbox-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.section__toolbox-empty {
  font-size: 0.6875rem;
  color: var(--mood-text-muted);
  text-align: center;
  padding: 0.75rem 0;
}

@media (max-width: 1023px) {
  .section__body {
    grid-template-columns: 1fr;
  }
  .section__toolbox {
    position: static;
    order: 2;
  }
}
</style>
