<script lang="ts">
  let { data } = $props();

  const scenarioRows = $derived(
    data.scenarios.map((scenario) => ({
      ...scenario,
      overrideKeys: Object.keys(scenario.overrides ?? {})
    }))
  );

  const approvedCount = $derived(scenarioRows.filter((scenario) => scenario.status === 'approved').length);
</script>

<section class="metric-row">
  <article class="metric-card">
    <span>Total Scenarios</span>
    <strong>{scenarioRows.length}</strong>
    <p>Saved planning workspaces</p>
  </article>

  <article class="metric-card">
    <span>Approved</span>
    <strong>{approvedCount}</strong>
    <p>Ready for pricing reference</p>
  </article>

  <article class="metric-card">
    <span>Overrides In Use</span>
    <strong>{scenarioRows.reduce((sum, row) => sum + row.overrideKeys.length, 0)}</strong>
    <p>Total override keys across all scenarios</p>
  </article>
</section>

<section class="scenario-list">
  {#each scenarioRows as scenario}
    <article class="surface-card">
      <div class="scenario-header">
        <div>
          <p class="eyebrow">Scenario</p>
          <h3>{scenario.name}</h3>
          <p>{scenario.description ?? 'No description provided yet.'}</p>
        </div>

        <span class={`status-pill ${scenario.status === 'approved' ? 'positive' : 'neutral'}`}>{scenario.status}</span>
      </div>

      <div class="scenario-grid">
        <section class="detail-card">
          <div class="detail-row">
            <span>Override count</span>
            <strong>{scenario.overrideKeys.length}</strong>
          </div>
          <div class="detail-row">
            <span>Primary state</span>
            <strong>{scenario.status}</strong>
          </div>
        </section>

        <section class="detail-card">
          <p class="eyebrow">Override Keys</p>
          {#if scenario.overrideKeys.length}
            <div class="chip-list">
              {#each scenario.overrideKeys as key}
                <span>{key}</span>
              {/each}
            </div>
          {:else}
            <p class="empty">No overrides have been defined yet.</p>
          {/if}
        </section>
      </div>

      <section class="json-card">
        <div class="json-header">
          <h4>Scenario payload</h4>
          <span>JSON view</span>
        </div>
        <pre>{JSON.stringify(scenario.overrides, null, 2)}</pre>
      </section>
    </article>
  {/each}
</section>

<style>
  h2,
  h3,
  h4,
  p,
  pre {
    margin: 0;
  }

  .eyebrow {
    color: #7f8e85;
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .page-intro,
  .metric-row,
  .scenario-list {
    margin-bottom: 1.25rem;
  }

  .page-intro h2 {
    margin: 0.35rem 0 0.45rem;
    max-width: 18ch;
    font-size: clamp(1.7rem, 3vw, 2.2rem);
    font-weight: 700;
  }

  .page-intro p:last-child,
  .metric-card p,
  .scenario-header p:last-child,
  .empty,
  .json-header span,
  pre {
    color: var(--muted);
  }

  .metric-row,
  .scenario-grid {
    display: grid;
    gap: 1rem;
  }

  .metric-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .metric-card,
  .surface-card,
  .detail-card,
  .json-card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 1.35rem;
    box-shadow: var(--shadow);
  }

  .metric-card {
    padding: 1.15rem 1.2rem;
  }

  .metric-card span {
    display: block;
    color: var(--muted);
    font-size: 0.9rem;
  }

  .metric-card strong {
    display: block;
    margin: 0.55rem 0 0.3rem;
    font-size: 1.9rem;
    font-weight: 700;
  }

  .scenario-list {
    display: grid;
    gap: 1rem;
  }

  .surface-card {
    padding: 1.2rem;
    display: grid;
    gap: 1rem;
  }

  .scenario-header,
  .detail-row,
  .json-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
  }

  .scenario-header h3 {
    margin: 0.3rem 0 0.4rem;
    font-size: 1.15rem;
    font-weight: 700;
  }

  .status-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.42rem 0.78rem;
    border-radius: 999px;
    font-size: 0.84rem;
    font-weight: 600;
    text-transform: capitalize;
  }

  .status-pill.positive {
    color: var(--green-deep);
    background: var(--green-soft);
  }

  .status-pill.neutral {
    color: #5a6c63;
    background: #edf2ef;
  }

  .scenario-grid {
    grid-template-columns: 0.7fr 1.3fr;
  }

  .detail-card,
  .json-card {
    padding: 1rem;
  }

  .detail-row + .detail-row {
    margin-top: 0.9rem;
  }

  .detail-row span {
    color: var(--muted);
  }

  .detail-row strong {
    font-size: 1rem;
    font-weight: 700;
  }

  .chip-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem;
    margin-top: 0.7rem;
  }

  .chip-list span {
    padding: 0.45rem 0.7rem;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--panel-soft);
    color: #365044;
    font-size: 0.84rem;
    font-weight: 600;
  }

  .json-header {
    margin-bottom: 0.8rem;
  }

  .json-header h4 {
    font-size: 1rem;
    font-weight: 700;
  }

  pre {
    padding: 1rem;
    border-radius: 1rem;
    background: var(--panel-soft);
    border: 1px solid var(--line);
    overflow: auto;
    font-size: 0.85rem;
    line-height: 1.55;
  }

  @media (max-width: 960px) {
    .metric-row,
    .scenario-grid {
      grid-template-columns: 1fr;
    }
  }

  @media (max-width: 760px) {
    .scenario-header,
    .detail-row,
    .json-header {
      flex-direction: column;
      align-items: flex-start;
    }
  }
</style>
