'use strict';

/**
 * Windsurf adapter — writes to .windsurfrules
 * Windsurf reads .windsurfrules automatically in every workspace.
 *
 * Contract:
 *   format(context, opts?) → string
 *   outputPath(cwd) → string
 */

const path = require('path');

const name = 'windsurf';

/**
 * Format context for Windsurf rules file.
 * @param {string} context - Raw signature context string
 * @param {object} [opts]
 * @param {string} [opts.version] - SigMap version string
 * @returns {string}
 */
function format(context, opts = {}) {
  if (!context || typeof context !== 'string') return '';
  const version   = opts.version || 'unknown';
  const timestamp = new Date().toISOString();
  const meta      = _confidenceMeta(opts);
  const header = [
    `# Code signatures — generated by SigMap v${version}`,
    `# Updated: ${timestamp}`,
    `# ${meta}`,
    `# Regenerate: node gen-context.js`,
    '#',
    '# SigMap: before answering, run: sigmap ask "<query>" — finds relevant files',
    '# SigMap: after config changes, run: sigmap validate',
    '# SigMap: to verify answers, run: sigmap judge --response <file>',
    '',
  ].join('\n');
  return header + context;
}

function _confidenceMeta(opts) {
  const parts = [`version=${opts.version || 'unknown'}`];
  if (opts.confidence)    parts.push(`confidence=${opts.confidence}`);
  if (opts.coverage != null) parts.push(`coverage=${opts.coverage}%`);
  if (opts.dropped  != null) parts.push(`dropped=${opts.dropped}`);
  if (opts.commit)        parts.push(`commit=${opts.commit}`);
  return `sigmap: ${parts.join(' ')}`;
}

/**
 * Return the output file path for this adapter.
 * @param {string} cwd - Project root
 * @returns {string}
 */
function outputPath(cwd) {
  return path.join(cwd, '.windsurfrules');
}

module.exports = { name, format, outputPath };
