'use strict';

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

const path = require('path');

const name = 'cursor';

/**
 * Format context for Cursor 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`,
    '',
  ].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, '.cursorrules');
}

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