<script lang="ts">
  import type { ComponentType } from 'svelte';
  import AppNavSection, { type AppNavSectionItem } from '$lib/components/navigation/AppNavSection.svelte';

  export type AppSecondaryRailItem = {
    id: string;
    label: string;
    icon?: ComponentType;
    group?: string;
  };

  let {
    sectionLabel,
    identityTitle,
    identitySubtitle,
    identityAvatarText = '',
    identityIcon,
    groups,
    activeId,
    onSelect
  }: {
    sectionLabel: string;
    identityTitle: string;
    identitySubtitle: string;
    identityAvatarText?: string;
    identityIcon?: ComponentType;
    groups: { label?: string; items: AppSecondaryRailItem[] }[];
    activeId: string;
    onSelect: (id: string) => void;
  } = $props();

  function toSectionItems(items: AppSecondaryRailItem[]): AppNavSectionItem[] {
    return items.map((item) => ({
      label: item.label,
      icon: item.icon,
      active: item.id === activeId,
      onSelect: () => onSelect(item.id),
      type: 'button'
    }));
  }
</script>

<nav class="secondary-rail" aria-label={`${sectionLabel} navigation`}>
  <p class="rail-label">{sectionLabel}</p>

  <div class="rail-identity">
    <div class="rail-avatar" aria-hidden="true">
      {#if identityIcon}
        {@const IdentityIcon = identityIcon}
        <IdentityIcon size={16} strokeWidth={1.75} />
      {:else}
        {identityAvatarText}
      {/if}
    </div>
    <div class="rail-identity-text">
      <p class="identity-name">{identityTitle}</p>
      <p class="identity-role">{identitySubtitle}</p>
    </div>
  </div>

  {#each groups as group}
    <div class="rail-group">
      <AppNavSection label={group.label ?? ''} ariaLabel={group.label ?? `${sectionLabel} section`} items={toSectionItems(group.items)} />
    </div>
  {/each}
</nav>

<style>
  .secondary-rail {
    position: sticky;
    top: 0;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    height: 100%;
    min-height: calc(100vh - 8.5rem);
    padding: 0;
    background: color-mix(in srgb, var(--panel-soft) 60%, var(--color-bg-surface));
    border-right: 1px solid var(--line);
    overflow-y: auto;
    --nav-section-label-color: var(--color-text-muted);
    --nav-section-label-size: 0.66rem;
    --nav-section-label-spacing: 0.14em;
    --nav-item-color: var(--color-text-secondary);
    --nav-item-size: 0.88rem;
    --nav-item-weight: 450;
    --nav-item-hover-bg: var(--color-surface-hover);
    --nav-item-hover-color: var(--color-text-primary);
    --nav-item-active-bg: color-mix(in srgb, var(--color-brand) 11%, transparent);
    --nav-item-active-color: var(--color-brand-hover);
    --nav-item-active-weight: 560;
    --nav-item-active-marker: color-mix(in srgb, var(--color-brand) 32%, transparent);
    --nav-icon-color: var(--color-text-muted);
    --nav-icon-hover-color: var(--color-text-secondary);
    --nav-icon-active-color: var(--color-brand);
  }

  .rail-label {
    margin: 0;
    padding: 1rem 1rem 0.15rem;
    color: color-mix(in srgb, var(--muted) 88%, #a3aea7);
    font-size: 0.64rem;
    font-weight: 700;
    letter-spacing: 0.14em;
    text-transform: uppercase;
  }

  .rail-identity {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0 1rem 1.15rem;
    border-bottom: 1px solid color-mix(in srgb, var(--line) 78%, transparent);
  }

  .rail-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 2.15rem;
    height: 2.15rem;
    border-radius: 50%;
    background: color-mix(in srgb, var(--panel) 80%, #edf2ee);
    color: #6b786f;
    font-size: 0.82rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    border: 1px solid color-mix(in srgb, var(--line) 72%, transparent);
  }

  .rail-identity-text {
    min-width: 0;
  }

  .identity-name {
    margin: 0;
    font-size: 0.8rem;
    font-weight: 600;
    color: #526059;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .identity-role {
    margin: 0;
    font-size: 0.72rem;
    color: #8a9790;
  }

  .rail-group {
    display: grid;
    gap: 0.08rem;
    padding: 0.1rem 0.7rem 0;
  }

  .rail-group + .rail-group {
    padding-top: 1rem;
  }

  .rail-group :global(.nav-section-label) {
    margin-top: 0;
    margin-left: 0.3rem;
    margin-right: 0.3rem;
  }

  @media (max-width: 980px) {
    .secondary-rail {
      position: static;
      min-height: auto;
      height: auto;
      overflow: visible;
      border-right: none;
    }

    .rail-group {
      gap: 0.3rem;
      padding-top: 0;
    }

    .rail-group :global(.nav-section-label) {
      width: 100%;
      margin-left: 0;
      margin-right: 0;
    }

    .rail-group :global(.nav-list) {
      display: flex;
      flex-wrap: wrap;
      gap: 0.3rem;
    }

    .rail-group :global(.nav-list a),
    .rail-group :global(.nav-button) {
      width: auto;
      padding-right: 0.9rem;
    }
  }
</style>
