import { defineConfig } from 'astro/config';
import starlight from '@astrojs/starlight';
import mermaid from 'astro-mermaid';

// Codex P0 fix (post-Phase 2 review): .md link stripping happens via
// scripts/post-build-strip-md-links.mjs (run after `astro build` in the
// build script). Astro markdown.remarkPlugins did not pick up the custom
// plugin in this Starlight + custom-glob-loader setup; post-build HTML
// rewrite is reliable.

// W1 scaffold. Production configuration filled in across W2-W11:
//   W2: content collection loader pointing in-place at docs/
//   W4: sidebar (Option C hybrid: manual top-level, autogenerate within)
//   W5: customCss reference
//   W6: Mermaid integration (D4: lazy-load with code-split fallback) - DONE
//   W9: redirects: from mkdocs.yml redirect_maps
//   W11: site + base verified against gh-pages deploy

export default defineConfig({
  site: 'https://product-on-purpose.github.io',
  base: '/pm-skills',
  // W9 (Redirect mapping): port of mkdocs.yml redirect_maps entries.
  // W13 B2.5: destinations now include the /pm-skills/ base path. Astro does
  // NOT auto-prepend the base to redirect destinations (verified in dist
  // output); without the prefix the meta-refresh lands at the bare GitHub
  // Pages domain (product-on-purpose.github.io) which is not the pm-skills
  // project page, producing a "Site not found" error. Sources stay relative
  // to the site root because Astro's redirect-source side IS base-aware.
  // 2026-05-06 deferral re-decided after live-impact evidence (Site not
  // found, not just "broken link").
  redirects: {
    '/bundles/': '/pm-skills/workflows/',
    '/bundles/feature-kickoff/': '/pm-skills/workflows/feature-kickoff/',
    '/bundles/lean-startup/': '/pm-skills/workflows/lean-startup/',
    '/bundles/triple-diamond/': '/pm-skills/workflows/triple-diamond/',
    '/concepts/triple-diamond/': '/pm-skills/concepts/triple-diamond-delivery-process/',
    '/concepts/versioning/': '/pm-skills/reference/pm-skill-versioning/',
    '/concepts/comparisons/': '/pm-skills/guides/pm-skill-comparisons/',
    '/concepts/skill-anatomy/': '/pm-skills/reference/pm-skill-anatomy/',
    '/concepts/skill-lifecycle/': '/pm-skills/guides/pm-skill-lifecycle/',
    '/guides/creating-skills/': '/pm-skills/guides/creating-pm-skills/',
    '/guides/authoring-pm-skills/': '/pm-skills/guides/creating-pm-skills/',
    '/guides/mcp-setup/': '/pm-skills/guides/mcp-integration/',
  },
  integrations: [
    // W6 (D4): astro-mermaid renders client-side per-page (only loads the
    // mermaid bundle on pages that contain code blocks). MUST come BEFORE
    // starlight per the integration-order rule in astro-mermaid README.
    // autoTheme follows site theme (Starlight light/dark).
    //
    // W13 M1 (Mermaid beautification, Layer 1): brand the line color and
    // font family via themeVariables. autoTheme remains true so Mermaid
    // still switches its base theme between 'default' (light) and 'dark'
    // (dark) following Starlight's data-theme attribute. themeVariables
    // are applied on top of whichever base theme is active, so the brand
    // overrides work in both modes. Indigo lineColor (#5C7CFA) matches
    // the favicon Triple Diamond mark; system-ui font matches Starlight
    // body text for visual consistency.
    mermaid({
      theme: 'default',
      autoTheme: true,
      mermaidConfig: {
        themeVariables: {
          lineColor: '#5C7CFA',
          fontFamily: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif',
          fontSize: '14px',
        },
      },
    }),
    starlight({
      title: 'pm-skills',
      description: 'PM skills for AI agents',
      editLink: {
        baseUrl: 'https://github.com/product-on-purpose/pm-skills/edit/main/',
      },
      // W5 (Custom CSS port): minimal port; only .mermaid keeps. See
      // src/styles/custom.css for rationale + retired-selector inventory.
      customCss: ['./src/styles/custom.css'],
      // W4 (D3 Option C): manual top-level section ordering and labels;
      // autogenerate within sections. Order mirrors mkdocs.yml nav top level.
      // Per-section label drift handled via frontmatter sidebar.label overrides
      // (see docs/reference/README.md for the canonical example).
      // Per Q1-A: Changelog and Tags are top-level sidebar entries (Starlight
      // convention), not nested under a Home group. Homepage reachable via logo.
      //
      // v2.16.0 Starlight 0.39 BREAKING CHANGE: autogenerated sidebar group
      // shorthand was removed. All entries with `label` + `autogenerate` must
      // wrap autogenerate in an `items` array:
      //   OLD: { label: 'X', autogenerate: { directory: 'docs/X' } }
      //   NEW: { label: 'X', items: [{ autogenerate: { directory: 'docs/X' } }] }
      // Wrapping pattern applied to every entry below. See Starlight 0.39
      // release notes for migration details.
      //
      // IMPORTANT: autogenerate directory paths are PREFIXED WITH 'docs/'.
      // This is a D2-Option-B consequence: our custom glob loader (src/content.config.ts)
      // mounts ./docs in-place; entry.filePath retains the 'docs/' prefix.
      // Starlight's autogenerate logic in node_modules/@astrojs/starlight/utils/navigation.ts
      // tries to strip the path src/content/docs/ from filePaths to compute "directory"
      // matches, but for our entries the strip is a no-op. So we match on the actual
      // filePath structure: docs/<section>. Do not "fix" these prefixes.
      // Tracked at collection.ts: "We still rely on the content collection folder
      // structure to be fixed for now" (Starlight upstream limitation).
      sidebar: [
        { slug: 'changelog' },
        // Tags page removed from sidebar (Codex P2): Material's tags plugin
        // had no Starlight equivalent; the page rendered empty. Per OQ-M3,
        // tags-as-feature deferred to v2.15+. The page itself is marked
        // draft: true so it doesn't build.
        {
          label: 'Getting Started',
          items: [{ autogenerate: { directory: 'docs/getting-started' } }],
        },
        {
          // Skills section: manual phase ordering (Triple Diamond) and
          // capitalized labels. Phase index.md files are generator output
          // (scripts/generate-skill-pages.py) so adding sidebar frontmatter
          // there would be overwritten on regen. Within each phase,
          // autogenerate handles the skill leaves.
          label: 'Skills',
          items: [
            // Skills section overview (slug refs use post-base path; no 'docs/' prefix here)
            'skills',
            {
              label: 'Discover',
              items: [{ autogenerate: { directory: 'docs/skills/discover' } }],
            },
            {
              label: 'Define',
              items: [{ autogenerate: { directory: 'docs/skills/define' } }],
            },
            {
              label: 'Develop',
              items: [{ autogenerate: { directory: 'docs/skills/develop' } }],
            },
            {
              label: 'Deliver',
              items: [{ autogenerate: { directory: 'docs/skills/deliver' } }],
            },
            {
              label: 'Measure',
              items: [{ autogenerate: { directory: 'docs/skills/measure' } }],
            },
            {
              label: 'Iterate',
              items: [{ autogenerate: { directory: 'docs/skills/iterate' } }],
            },
            {
              label: 'Foundation',
              items: [{ autogenerate: { directory: 'docs/skills/foundation' } }],
            },
            {
              label: 'Utility',
              items: [{ autogenerate: { directory: 'docs/skills/utility' } }],
            },
          ],
        },
        {
          label: 'Workflows',
          items: [{ autogenerate: { directory: 'docs/workflows' } }],
        },
        {
          label: 'Guides',
          items: [{ autogenerate: { directory: 'docs/guides' } }],
        },
        {
          label: 'Concepts',
          items: [{ autogenerate: { directory: 'docs/concepts' } }],
        },
        {
          label: 'Showcase',
          items: [{ autogenerate: { directory: 'docs/showcase' } }],
        },
        {
          label: 'Reference',
          items: [{ autogenerate: { directory: 'docs/reference' } }],
        },
        {
          label: 'Contributing',
          items: [{ autogenerate: { directory: 'docs/contributing' } }],
        },
        {
          label: 'Releases',
          items: [{ autogenerate: { directory: 'docs/releases' } }],
        },
        {
          // W7: 115 library sample pages mounted under /samples/. Collapsed
          // by default to avoid sidebar clutter (each sample is a leaf within
          // a per-skill subgroup, so the section expands to 40 subgroups).
          // W13 B2.5: hybrid items (overview slug + library autogen) so the
          // /samples/ section root resolves to a real overview page rather
          // than 404. Mirrors the Skills section pattern (slug ref + autogen).
          label: 'Samples',
          collapsed: true,
          items: [
            'samples',
            {
              label: 'Library',
              items: [{
                autogenerate: {
                  directory: 'library/skill-output-samples',
                  collapsed: true,
                },
              }],
            },
          ],
        },
      ],
    }),
  ],
});
