<script lang="ts">
  import type { UiDashboardPreview } from "../types";
  import ModuleCard from "./ModuleCard.svelte";
  import ServicePanel from "./ServicePanel.svelte";
  import StatusStrip from "./StatusStrip.svelte";
  import TelemetryGrid from "./TelemetryGrid.svelte";

  const generatedTitleId = $props.id();

  let {
    dashboard,
    titleId = `${generatedTitleId}-title`,
  }: {
    dashboard: UiDashboardPreview;
    titleId?: string;
  } = $props();
</script>

<main class="dashboard-frame" aria-labelledby={titleId}>
  <header class="dashboard-frame__header">
    <div>
      {#if dashboard.eyebrow}
        <p>{dashboard.eyebrow}</p>
      {/if}
      <h1 id={titleId}>{dashboard.title}</h1>
      {#if dashboard.subtitle}
        <span>{dashboard.subtitle}</span>
      {/if}
    </div>
    {#if dashboard.modules.length}
      <div class="dashboard-frame__modules">
        {#each dashboard.modules as module (module.id)}
          <ModuleCard {module} />
        {/each}
      </div>
    {/if}
  </header>

  <TelemetryGrid cards={dashboard.telemetry} />

  <section class="dashboard-frame__panels" aria-label="Service groups">
    {#each dashboard.serviceGroups as group (group.id)}
      <ServicePanel {group} />
    {/each}
  </section>

  <StatusStrip id={dashboard.statusStripId} items={dashboard.statusItems} />
</main>

<style>
  .dashboard-frame {
    display: grid;
    min-height: 100vh;
    gap: var(--ui-space-3);
    padding: clamp(0.75rem, 1.3vw, 1.25rem);
    background:
      linear-gradient(90deg, transparent 0 49%, rgba(255, 255, 255, 0.08) 50%, transparent 51%),
      rgba(0, 0, 0, 0.18);
  }

  .dashboard-frame__header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: var(--ui-space-4);
    align-items: start;
    border-bottom: var(--ui-border);
    padding-bottom: var(--ui-space-3);
  }

  .dashboard-frame__header p,
  .dashboard-frame__header span,
  h1 {
    margin: 0;
    text-transform: uppercase;
  }

  .dashboard-frame__header p,
  .dashboard-frame__header span {
    color: var(--ui-color-muted);
    font-size: 0.78rem;
    font-weight: 800;
  }

  h1 {
    max-width: 100%;
    overflow-wrap: anywhere;
    font-family: var(--ui-font-display);
    font-size: clamp(2.45rem, 5vw, 4.2rem);
    font-weight: 850;
    letter-spacing: 0;
    line-height: 0.85;
  }

  .dashboard-frame__modules {
    display: flex;
    flex-wrap: wrap;
    justify-content: end;
    gap: var(--ui-space-3);
  }

  .dashboard-frame__panels {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr));
    gap: var(--ui-space-3);
  }

  @media (max-width: 780px) {
    .dashboard-frame__header {
      grid-template-columns: 1fr;
    }

    .dashboard-frame__modules {
      justify-content: stretch;
    }
  }
</style>
