'use strict';

/**
 * Scaffold persistence (IMPL.md §6.2 — 2d).
 *
 * Renders an accepted scaffold proposal to markdown and locates the on-disk
 * record (`.context/scaffold/latest.md`) so the `create` pipeline and agents can
 * read back the convention-matched proposal. Pure render; zero-dependency,
 * bundle-safe.
 */

const path = require('path');

/** Path to the persisted scaffold record. */
function scaffoldPath(cwd) {
  return path.join(cwd, '.context', 'scaffold', 'latest.md');
}

/**
 * Render an accepted scaffold decision to a markdown record.
 * @param {object} decision a `proposeScaffold` result with `ok: true`
 * @param {object} [opts]
 * @param {string} [opts.timestamp] ISO timestamp (caller supplies — keeps this pure)
 * @returns {string}
 */
function renderScaffoldMarkdown(decision, opts = {}) {
  const d = decision || {};
  const p = d.proposal || {};
  const pct = (n) => `${Math.round((n || 0) * 100)}%`;
  const lines = [
    `# Scaffold — ${d.name || '(unnamed)'}`,
    '',
    `- **Conventions:** ${d.tier || 'unknown'} (${pct(d.confidence)})`,
    `- **File:** \`${p.filename || ''}\` (${p.namingStyle || ''})`,
    `- **Export style:** ${p.exportStyle || ''}`,
    `- **Test file:** \`${p.testFile || ''}\`${p.testFramework ? ` (${p.testFramework})` : ''}`,
  ];
  if (d.warning) lines.push(`- **Warning:** ${d.warning}`);
  lines.push('');
  lines.push(`<sub>Generated by \`sigmap scaffold\`${opts.timestamp ? ` · ${opts.timestamp}` : ''}.</sub>`);
  lines.push('');
  return lines.join('\n');
}

module.exports = { scaffoldPath, renderScaffoldMarkdown };
