function generateMongoNativeService(context) {
  const { service } = context;
  if (!service) throw new Error("Service is required in template context");
  const serviceName = service.name;
  const operationsCode = generateOperations(service);
  const usesDb = /\bgetDb\b|\bgetCollection\b/.test(operationsCode);
  const hasEvents = service.publishes && service.publishes.length > 0 || service.subscribes && service.subscribes.length > 0;
  return `/**
 * ${serviceName}
 * Abstract business logic service (MongoDB native driver)
 * ${service.description || ""}
 */
${usesDb ? `import { getDb, getCollection } from '../db/mongoClient.js';` : ""}
${hasEvents ? `import { eventBus } from '../events/eventBus.js';` : ""}

export class ${serviceName} {
${operationsCode}
}

const ${lowerFirst(serviceName)} = new ${serviceName}();
export const ${lowerFirst(serviceName)}Instance = ${lowerFirst(serviceName)};
export default ${lowerFirst(serviceName)};
`;
}
function lowerFirst(s) {
  return s.charAt(0).toLowerCase() + s.slice(1);
}
function generateOperations(service) {
  const ops = service.operations;
  if (!ops || Array.isArray(ops) && ops.length === 0 || !Array.isArray(ops) && Object.keys(ops).length === 0) {
    return `
  /**
   * Default service entrypoint \u2014 replace with real operations once declared
   * on the service's spec.
   */
  public async execute(_params: any = {}): Promise<any> {
    throw new Error('${service.name}.execute is not implemented');
  }
`;
  }
  const entries = Array.isArray(ops) ? ops.map((op) => [op.name, op]) : Object.entries(ops);
  return entries.map(([name, op]) => generateOperation(name, op, service.name)).join("\n");
}
function generateOperation(operationName, operation, serviceName) {
  const stepsHeader = operation.steps && operation.steps.length > 0 ? operation.steps.map((s) => `   *   - ${typeof s === "string" ? s : s.action || JSON.stringify(s)}`).join("\n") : "   *   (no spec steps declared)";
  return `
  /**
   * ${operationName}
   * ${operation.description || ""}
   *
   * Spec steps:
${stepsHeader}
   */
  public async ${operationName}(_args: any = {}): Promise<any> {
    // TODO (#43 follow-up): translate spec steps into native MongoDB driver
    // calls. For now this is a stub so realize completes and the service
    // surface is callable for parity tests.
    throw new Error('${serviceName}.${operationName} is not implemented');
  }
`;
}
export {
  generateMongoNativeService as default
};
