// Copyright (c) 2026 Hellmai Ltd
// SPDX-License-Identifier: AGPL-3.0-only

/**
 * Section Headings Constants
 *
 * Centralized section heading defaults for backlog.md and status.md
 * Eliminates magic strings scattered throughout wu-* tools
 *
 * Usage: Use getSectionHeadingsWithDefaults() to get frontmatter-configured
 * headings with sensible fallbacks if frontmatter is missing/malformed
 *
 * WU-2698 (INIT-058 L4-prep narrow): moved from @lumenflow/core/src/section-headings.ts.
 * The `getSectionHeadings` helper (previously imported from core's backlog-parser) is
 * inlined here to keep the pack core-free.
 */

import { BACKLOG_SECTIONS, STATUS_SECTIONS } from './wu-ui-constants.js';

/**
 * Section configuration from frontmatter.
 */
interface SectionConfig {
  heading?: string;
}

/**
 * Extract section headings from frontmatter.
 *
 * Inlined from @lumenflow/core/src/backlog-parser.ts (WU-2698) so that this
 * module does not depend on core during the INIT-058 L4-prep migration.
 *
 * @param {object|null} frontmatter - Parsed frontmatter object.
 * @returns {object} Map of section names to heading strings (e.g., {ready: "## 🚀 Ready"}).
 */
function getSectionHeadings(frontmatter: Record<string, unknown> | null): Record<string, string> {
  if (!frontmatter) {
    return {};
  }
  const sections = (frontmatter as { sections?: Record<string, SectionConfig> }).sections;
  if (!sections) {
    return {};
  }

  const headings: Record<string, string> = {};
  for (const [sectionName, sectionConfig] of Object.entries(sections)) {
    if (sectionConfig && typeof sectionConfig.heading === 'string') {
      headings[sectionName] = sectionConfig.heading;
    }
  }

  return headings;
}

/**
 * Default section headings (fallbacks when frontmatter is missing)
 * Re-exports from wu-ui-constants.ts for backwards compatibility
 */
export const DEFAULT_SECTION_HEADINGS = {
  backlog: {
    ready: BACKLOG_SECTIONS.READY,
    in_progress: BACKLOG_SECTIONS.IN_PROGRESS,
    blocked: BACKLOG_SECTIONS.BLOCKED,
    done: BACKLOG_SECTIONS.DONE,
  },
  status: {
    in_progress: STATUS_SECTIONS.IN_PROGRESS,
    completed: STATUS_SECTIONS.COMPLETED,
    blocked: STATUS_SECTIONS.BLOCKED,
  },
};

/**
 * Get section headings with frontmatter override + defaults
 *
 * Replaces scattered pattern: headings.done || '## ✅ Done'
 * Centralizes fallback logic for consistent heading resolution
 *
 * @param {object|null} frontmatter - Parsed frontmatter from backlog.md/status.md
 * @param {'backlog'|'status'} docType - Document type
 * @returns {object} Section headings (configured or default)
 */
export function getSectionHeadingsWithDefaults(
  frontmatter: Record<string, unknown> | null,
  docType: keyof typeof DEFAULT_SECTION_HEADINGS = 'backlog',
) {
  const defaults = DEFAULT_SECTION_HEADINGS[docType] as {
    ready?: string;
    in_progress?: string;
    blocked?: string;
    done?: string;
    completed?: string;
  };
  const configured = frontmatter ? getSectionHeadings(frontmatter) : {};

  return {
    ready: configured.ready || defaults.ready || DEFAULT_SECTION_HEADINGS.backlog.ready,
    in_progress:
      configured.in_progress ||
      defaults.in_progress ||
      DEFAULT_SECTION_HEADINGS.backlog.in_progress,
    blocked: configured.blocked || defaults.blocked || DEFAULT_SECTION_HEADINGS.backlog.blocked,
    done: configured.done || defaults.done || DEFAULT_SECTION_HEADINGS.backlog.done,
    completed:
      configured.completed || defaults.completed || DEFAULT_SECTION_HEADINGS.status.completed,
  };
}
