<script setup lang="ts">
/**
 * SectionLayout — Mise en page reutilisable pour les sections (documents, decisions, mandats).
 *
 * Structure : titre + status filter pills en haut,
 * puis une grille principale (contenu) + barre laterale "Boite a outils".
 * Responsive : sur mobile la boite a outils passe sous le contenu principal.
 */

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]
}>()

/** Map status id to CSS class for status pills. */
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-layout">
    <!-- Header: title + status pills -->
    <div class="section-layout__header">
      <div class="section-layout__title-block">
        <h1 class="section-layout__title">
          {{ title }}
        </h1>
        <p v-if="subtitle" class="section-layout__subtitle">
          {{ subtitle }}
        </p>
      </div>

      <div v-if="statuses.length > 0" class="section-layout__status-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="status-pill__count">
            {{ status.count }}
          </span>
        </button>
      </div>
    </div>

    <!-- Main content area: list + toolbox sidebar -->
    <div class="section-layout__body">
      <!-- Left: search + list -->
      <div class="section-layout__main">
        <!-- Search / sort bar slot -->
        <div v-if="$slots.search" class="section-layout__search">
          <slot name="search" />
        </div>

        <!-- Main list content -->
        <div class="section-layout__content">
          <slot />
        </div>

        <!-- Empty state slot -->
        <div v-if="$slots.empty" class="section-layout__empty">
          <slot name="empty" />
        </div>
      </div>

      <!-- Right: toolbox sidebar -->
      <aside class="section-layout__toolbox">
        <div class="section-layout__toolbox-header">
          <UIcon name="i-lucide-wrench" class="text-sm" />
          <span>Boite a outils</span>
        </div>

        <!-- Custom toolbox slot or default vignettes -->
        <div v-if="$slots.toolbox" class="section-layout__toolbox-content">
          <slot name="toolbox" />
        </div>
        <div v-else-if="toolboxItems && toolboxItems.length > 0" class="section-layout__toolbox-content">
          <ToolboxVignette
            v-for="(item, idx) in toolboxItems"
            :key="idx"
            :title="item.title"
            :description="item.description"
          />
        </div>
        <div v-else class="section-layout__toolbox-empty">
          <p>Aucun outil disponible</p>
        </div>
      </aside>
    </div>
  </div>
</template>

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

.section-layout__header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}

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

.section-layout__title {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--mood-text);
  line-height: 1.2;
}

.section-layout__subtitle {
  margin-top: 0.25rem;
  font-size: 0.875rem;
  color: var(--mood-text-muted);
}

.section-layout__status-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

/* Active state ring for pills */
.status-pill.active {
  outline: 2px solid var(--mood-accent);
  outline-offset: 2px;
}

.status-pill__count {
  margin-left: 0.375rem;
  font-size: 0.625rem;
  font-weight: 700;
  opacity: 0.8;
}

.section-layout__body {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 1.5rem;
  align-items: start;
}

.section-layout__main {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0;
}

.section-layout__search {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}

.section-layout__content {
  min-height: 200px;
}

.section-layout__toolbox {
  position: sticky;
  top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  background: var(--mood-surface);
  border: 1px solid var(--mood-border);
  border-radius: 0.75rem;
  padding: 1rem;
}

.section-layout__toolbox-header {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.8125rem;
  font-weight: 700;
  color: var(--mood-accent);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.section-layout__toolbox-content {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
}

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

/* Responsive: on mobile, toolbox goes below */
@media (max-width: 1023px) {
  .section-layout__body {
    grid-template-columns: 1fr;
  }

  .section-layout__toolbox {
    position: static;
    order: 2;
  }
}
</style>
