<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;
    box-sizing: border-box;
    height: 100vh;
    min-height: 100vh;
    gap: 0.36rem;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    overflow: hidden;
    padding: clamp(0.42rem, 0.65vw, 0.62rem);
    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(30rem, 1fr) minmax(30rem, 0.9fr);
    gap: 0.5rem;
    align-items: start;
    border-bottom: var(--ui-border);
    min-height: 5.35rem;
    padding-bottom: 0.34rem;
  }

  .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.35rem, 3.65vw, 3.25rem);
    font-weight: 850;
    letter-spacing: 0;
    line-height: 0.78;
    white-space: nowrap;
  }

  .dashboard-frame__modules {
    display: grid;
    grid-template-columns: minmax(15rem, 0.66fr) minmax(17rem, 1fr);
    justify-content: end;
    gap: 0.5rem;
  }

  .dashboard-frame__panels {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    align-content: start;
    gap: 0.36rem;
    min-height: 0;
    overflow: hidden;
  }

  .dashboard-frame__panels :global(.service-panel[data-model-id="runtime-health"]) {
    grid-column: 1 / -1;
  }

  .dashboard-frame__panels
    :global(.service-panel[data-model-id="runtime-health"] .panel__body) {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  @media (max-width: 780px) {
    .dashboard-frame {
      height: auto;
      min-height: 100vh;
      overflow: visible;
    }

    .dashboard-frame__header {
      grid-template-columns: 1fr;
    }

    .dashboard-frame__modules {
      grid-template-columns: 1fr;
    }

    h1 {
      white-space: normal;
    }

    .dashboard-frame__panels {
      grid-template-columns: 1fr;
      overflow: visible;
    }

    .dashboard-frame__panels
      :global(.service-panel[data-model-id="runtime-health"] .panel__body) {
      grid-template-columns: 1fr;
    }
  }
</style>
