<script setup lang="ts">
/**
 * SectionLayout — Mise en page pour sections.
 *
 * Desktop (≥1024px) : 2 colonnes, toolbox sticky à droite, toujours visible.
 * Mobile/tablette : toolbox en USlideover droit, bouton flottant.
 */

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
    toolboxTitle?: string
  }>(),
  {
    subtitle: undefined,
    toolboxItems: undefined,
    activeStatus: null,
    toolboxTitle: 'Boîte à outils',
  },
)

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

const toolboxOpen = ref(false)

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__header-left">
        <h1 class="section__title">{{ title }}</h1>
        <p v-if="subtitle" class="section__subtitle">{{ subtitle }}</p>
      </div>
      <!-- Mobile toolbox trigger -->
      <button
        class="section__toolbox-fab lg:hidden"
        :class="{ 'section__toolbox-fab--active': toolboxOpen }"
        @click="toolboxOpen = true"
      >
        <UIcon name="i-lucide-wrench" />
        <span>Outils</span>
      </button>
    </div>

    <!-- Body: content + toolbox -->
    <div class="section__body">
      <div class="section__main">
        <!-- Status pills -->
        <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 v-if="$slots.search" class="section__search">
          <slot name="search" />
        </div>
        <div class="section__content">
          <slot />
        </div>
      </div>

      <!-- Desktop toolbox sidebar (≥1024px) -->
      <aside class="section__toolbox">
        <div class="section__toolbox-head">
          <UIcon name="i-lucide-wrench" class="section__toolbox-head-icon" />
          <span>{{ toolboxTitle }}</span>
        </div>
        <div class="section__toolbox-body">
          <div v-if="$slots.toolbox">
            <slot name="toolbox" />
          </div>
          <div v-else-if="toolboxItems && toolboxItems.length > 0">
            <ToolboxVignette
              v-for="(item, idx) in toolboxItems"
              :key="idx"
              :title="item.title"
            />
          </div>
          <div v-else class="section__toolbox-empty">
            Aucun outil disponible
          </div>
        </div>
      </aside>
    </div>

    <!-- Mobile toolbox: USlideover from right -->
    <USlideover
      v-model:open="toolboxOpen"
      side="right"
      :title="toolboxTitle"
      :ui="{
        width: 'max-w-sm',
        header: { padding: 'p-4' },
        body: { padding: 'p-4' },
      }"
    >
      <template #body>
        <div class="section__toolbox-slideover">
          <div v-if="$slots.toolbox">
            <slot name="toolbox" />
          </div>
          <div v-else-if="toolboxItems && toolboxItems.length > 0">
            <ToolboxVignette
              v-for="(item, idx) in toolboxItems"
              :key="idx"
              :title="item.title"
            />
          </div>
          <div v-else class="section__toolbox-empty">
            Aucun outil disponible
          </div>
        </div>
      </template>
    </USlideover>
  </div>
</template>

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

@media (min-width: 640px) {
  .section { gap: 1.5rem; }
}

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

.section__header-left {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.section__title {
  font-size: 1.375rem;
  font-weight: 800;
  color: var(--mood-text);
  letter-spacing: -0.02em;
  margin: 0;
}

@media (min-width: 640px) {
  .section__title { font-size: 1.75rem; }
}

.section__subtitle {
  font-size: 0.875rem;
  color: var(--mood-text-muted);
  font-weight: 500;
  margin: 0;
  line-height: 1.5;
}

@media (min-width: 640px) {
  .section__subtitle { font-size: 1rem; }
}

/* Mobile toolbox trigger */
.section__toolbox-fab {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.5rem 0.875rem;
  font-size: 0.8125rem;
  font-weight: 700;
  color: var(--mood-accent);
  background: var(--mood-accent-soft);
  border-radius: 20px;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  white-space: nowrap;
}

.section__toolbox-fab:hover {
  transform: translateY(-1px);
  box-shadow: 0 3px 10px var(--mood-shadow);
}

.section__toolbox-fab--active {
  background: var(--mood-accent);
  color: var(--mood-accent-text);
}

/* Body layout */
.section__body {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1.5rem;
  align-items: start;
}

@media (min-width: 1024px) {
  .section__body {
    grid-template-columns: 1fr 30rem;
  }
}

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

/* Status pills */
.section__pills {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 2px;
}

.section__pills::-webkit-scrollbar { display: none; }

@media (min-width: 640px) {
  .section__pills {
    flex-wrap: wrap;
    overflow-x: visible;
  }
}

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

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

@media (max-width: 639px) {
  .section__search { flex-direction: column; }
}

.section__content { min-height: 12rem; }

/* Desktop toolbox sidebar */
.section__toolbox {
  display: none;
  position: sticky;
  top: 4.5rem;
  align-self: start;
  flex-direction: column;
  background: var(--mood-surface);
  border-radius: 16px;
  max-height: calc(100vh - 5.5rem);
  box-shadow: 0 4px 24px var(--mood-shadow);
}

@media (min-width: 1024px) {
  .section__toolbox { display: flex; }
}

.section__toolbox-head {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.875rem 1rem 0.625rem;
  font-size: 0.8125rem;
  font-weight: 800;
  color: var(--mood-accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  flex-shrink: 0;
}

.section__toolbox-head-icon {
  font-size: 0.875rem;
}

.section__toolbox-body {
  padding: 0 0.75rem 0.875rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow: hidden;
}

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

/* Slideover content */
.section__toolbox-slideover {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
</style>
