<script lang="ts">
  import { CalendarDays, CalendarRange, Carrot, Gauge, TrendingUp, Wheat } from 'lucide-svelte';

  import { MIX_RANGES } from '$lib/components/throughput/utils';

  let {
    today,
    weekRangeLabel,
    heroStats,
    mixTotals,
    mixRangeKey = $bindable<(typeof MIX_RANGES)[number]['key']>('4w'),
    formatDate,
    formatNumber
  }: {
    today: string;
    weekRangeLabel: string;
    heroStats: { today: number; thisWeek: number; avgFourWeek: number };
    mixTotals: { horse: number; grain: number };
    mixRangeKey?: (typeof MIX_RANGES)[number]['key'];
    formatDate: (value: string) => string;
    formatNumber: (value: number | null | undefined, digits?: number) => string;
  } = $props();
</script>

<header class="throughput-summary" aria-label="Throughput summary">
  <div class="summary-heading">
    <span class="summary-icon"><Gauge size={17} strokeWidth={2.2} /></span>
    <h2>Throughput Overview</h2>
    <div class="range-select" role="group" aria-label="Customer mix date range">
      {#each MIX_RANGES as range (range.key)}
        <button
          type="button"
          class="range-option"
          class:active={mixRangeKey === range.key}
          aria-pressed={mixRangeKey === range.key}
          onclick={() => (mixRangeKey = range.key)}
        >{range.label}</button>
      {/each}
    </div>
  </div>
  <dl class="facts">
    <div class="fact">
      <dt><span class="fact-icon"><CalendarDays size={16} strokeWidth={2.2} /></span>Today</dt>
      <dd>{formatNumber(heroStats.today)} <span class="fact-unit">kg</span></dd>
      <p class="fact-sub">{formatDate(today)}</p>
    </div>
    <div class="fact">
      <dt><span class="fact-icon"><CalendarRange size={16} strokeWidth={2.2} /></span>This week</dt>
      <dd>{formatNumber(heroStats.thisWeek)} <span class="fact-unit">kg</span></dd>
      <p class="fact-sub">{weekRangeLabel}</p>
    </div>
    <div class="fact">
      <dt><span class="fact-icon"><TrendingUp size={16} strokeWidth={2.2} /></span>4-week average</dt>
      <dd>{formatNumber(heroStats.avgFourWeek)} <span class="fact-unit">kg</span></dd>
      <p class="fact-sub">Per week, last 4 weeks</p>
    </div>
  </dl>
  <dl class="mix-facts" aria-label="Throughput by customer">
    <div class="fact">
      <dt><span class="fact-icon"><Carrot size={16} strokeWidth={2.2} /></span>Horse Mix</dt>
      <dd>{formatNumber(mixTotals.horse)} <span class="fact-unit">kg</span></dd>
      <p class="fact-sub">PHF Horsemix · {MIX_RANGES.find((r) => r.key === mixRangeKey)?.sub ?? 'last 4 weeks'}</p>
    </div>
    <div class="fact">
      <dt><span class="fact-icon"><Wheat size={16} strokeWidth={2.2} /></span>Grain Mix</dt>
      <dd>{formatNumber(mixTotals.grain)} <span class="fact-unit">kg</span></dd>
      <p class="fact-sub">All other customers · {MIX_RANGES.find((r) => r.key === mixRangeKey)?.sub ?? 'last 4 weeks'}</p>
    </div>
  </dl>
</header>

<style>
  .throughput-summary {
    display: grid;
    padding: 0;
    border: 1px solid color-mix(in srgb, var(--color-brand) 18%, var(--color-border));
    border-radius: 0.95rem;
    background:
      radial-gradient(circle at top right, color-mix(in srgb, var(--color-brand) 12%, transparent), transparent 45%),
      linear-gradient(180deg, color-mix(in srgb, var(--color-brand) 5%, var(--color-bg-surface)), var(--color-bg-surface));
  }

  .summary-heading {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.55rem 0.75rem;
    padding: 1rem 1rem 0;
  }

  .summary-heading h2 {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    letter-spacing: 0.01em;
  }

  .summary-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.9rem;
    height: 1.9rem;
    border-radius: 0.65rem;
    background: var(--color-brand-tint);
    color: var(--color-brand);
  }

  .range-select {
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
    margin-left: auto;
    padding: 0.28rem;
    border: 1px solid color-mix(in srgb, var(--color-brand) 14%, var(--color-border));
    border-radius: 0.8rem;
    background: color-mix(in srgb, var(--color-bg-surface) 55%, transparent);
  }

  .range-option {
    padding: 0.5rem 0.95rem;
    border: 0;
    border-radius: 0.6rem;
    background: transparent;
    color: var(--color-text-secondary);
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color 140ms ease, color 140ms ease, box-shadow 140ms ease;
  }

  .range-option:hover {
    color: var(--color-text-primary);
  }

  .range-option.active {
    background: var(--color-brand);
    color: #fff;
    box-shadow: 0 8px 18px -14px color-mix(in srgb, var(--color-brand) 85%, transparent);
  }

  .facts,
  .mix-facts {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.8rem;
    padding: 1rem;
  }

  .mix-facts {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding-top: 0;
  }

  .fact {
    container-type: inline-size;
    position: relative;
    display: grid;
    gap: 0.28rem;
    min-height: 7rem;
    padding: 1rem 1rem 0.9rem;
    border: 1px solid color-mix(in srgb, var(--color-brand) 12%, var(--color-border));
    border-radius: 0.95rem;
    background: color-mix(in srgb, var(--color-bg-surface) 84%, white);
    box-shadow: 0 12px 36px -28px rgba(15, 23, 42, 0.28);
  }

  .fact dt {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    margin: 0;
    font-size: 0.83rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--color-text-secondary);
  }

  .fact-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.7rem;
    height: 1.7rem;
    border-radius: 0.6rem;
    background: var(--color-brand-tint);
    color: var(--color-brand);
  }

  .fact dd {
    margin: 0;
    font-size: clamp(1.85rem, 4.5cqi, 2.3rem);
    font-weight: 700;
    line-height: 1.05;
    letter-spacing: -0.03em;
    color: var(--color-text-primary);
    font-variant-numeric: tabular-nums;
  }

  .fact-unit {
    font-size: 0.45em;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--color-text-secondary);
  }

  .fact-sub {
    margin: 0.05rem 0 0;
    color: var(--color-text-secondary);
    font-size: 0.88rem;
    line-height: 1.35;
  }

  @media (min-width: 1024px) {
    .throughput-summary {
      grid-template-columns: repeat(5, minmax(0, 1fr));
      gap: 0.75rem;
      padding: 1rem;
    }

    .facts,
    .mix-facts {
      display: contents;
    }

    .summary-heading {
      grid-column: 1 / -1;
      padding: 0;
    }

    .fact {
      min-height: 7rem;
      padding: 0.9rem 0.85rem;
    }

    .fact-sub {
      font-size: 0.78rem;
    }
  }

  @media (max-width: 760px) {
    .facts {
      grid-template-columns: 1fr;
      padding: 0.9rem;
    }

    .mix-facts {
      grid-template-columns: 1fr;
      padding: 0 0.9rem 0.9rem;
    }
  }
</style>
