---
/**
 * Auto-injected /exercises route (v4.4.0).
 *
 * Gated on `routes.exercises: true`. Reads `src/data/exercises.json`
 * (emitted by `book-scaffold build-exercises`); renders ordered list
 * grouped by chapter with deep links into the chapter routes.
 *
 * Graceful skip: if exercises.json doesn't exist, renders an instructions
 * page (doesn't fail the build).
 */
import Base from '../layouts/Base.astro';
import { normalizeBase } from '../src/lib/nav-href';
import bookConfig from 'virtual:book-scaffold/book-config';
import { selectBookArtifact } from '../src/lib/corpus';

interface Props { bookId?: string; }
const { bookId } = Astro.props;

// v4.4.0 fix: use Vite's import.meta.glob with a project-root-relative
// path (same lesson as tips.astro). `/src/data/...` is consumer-project-
// rooted in Vite's resolver and works across all consumer build contexts.
type ExerciseIndex = Record<string, Array<{ id: string; problem: string }>>;
const exModules = import.meta.glob<{ default: unknown }>(
  '/src/data/exercises.json',
  { eager: true },
);
const exEntry = exModules['/src/data/exercises.json'];
const byChapter = exEntry
  ? selectBookArtifact<ExerciseIndex>(
      exEntry.default,
      bookConfig.corpus,
      bookId,
      'src/data/exercises.json',
    )
  : {};
const loadError = exEntry
  ? null
  : 'src/data/exercises.json not found — run `npx book-scaffold build-exercises` to generate.';

const chapters = Object.keys(byChapter).sort();
const total = chapters.reduce((sum, c) => sum + byChapter[c].length, 0);

// #142: chapter deep-links must prefix the deploy base.
const baseUrl = normalizeBase(import.meta.env.BASE_URL);
---
<Base title="Exercises" description="All exercises in this book, grouped by chapter with deep links." bookId={bookId}>
  <article class="prose">
    <h1>Exercises</h1>
    {loadError && (
      <p class="exercises-empty">{loadError}</p>
    )}
    {!loadError && total === 0 && (
      <p class="exercises-empty">
        No exercises defined yet. Add <code>&lt;Exercise id="..."&gt;...&lt;/Exercise&gt;</code> to a chapter
        and run <code>npx book-scaffold build-exercises</code>.
      </p>
    )}
    {total > 0 && (
      <p class="exercises-summary">
        {total} exercise{total === 1 ? '' : 's'} across {chapters.length} chapter{chapters.length === 1 ? '' : 's'}.
      </p>
    )}
    {chapters.map((chapter) => (
      <section class="exercises-chapter" id={`chapter-${chapter}`}>
        <h2 class="exercises-chapter-title">
          Chapter: <a href={`${baseUrl}chapters/${bookId ? `${bookId}/` : ''}${chapter}/`}>{chapter}</a>
        </h2>
        <ol class="exercises-list">
          {byChapter[chapter].map((ex) => (
            <li class="exercises-item">
              <a href={`${baseUrl}chapters/${bookId ? `${bookId}/` : ''}${chapter}/#exercise-${ex.id}`} class="exercises-link">
                <strong class="exercises-id">Exercise {ex.id}</strong>
                <span class="exercises-preview">{ex.problem.slice(0, 120)}{ex.problem.length > 120 ? '…' : ''}</span>
              </a>
            </li>
          ))}
        </ol>
      </section>
    ))}
  </article>
</Base>
