/**
 * desktop/src/logger.ts — B3: tiny structured logger (GLUE).
 *
 * A one-line-per-message logger the glue modules (settings-store, embedded-
 * server, and the Electron main files) inject instead of calling console.* —
 * the project bans console.log everywhere (coding-style). Info goes to stdout;
 * warn/error go to stderr. error() appends the underlying Error's message so a
 * failure's cause is visible without a stack dump. No dependencies, no state.
 */

export interface Logger {
  info(msg: string): void
  warn(msg: string): void
  error(msg: string, err?: unknown): void
}

/** Build a Logger that prefixes every line with `[scope]` and the level. */
export function createLogger(scope: string): Logger {
  return {
    info(msg: string): void {
      process.stdout.write(`[${scope}] INFO: ${msg}\n`)
    },
    warn(msg: string): void {
      process.stderr.write(`[${scope}] WARN: ${msg}\n`)
    },
    error(msg: string, err?: unknown): void {
      // Only Error carries a reliable .message; other thrown values are opaque.
      const detail = err instanceof Error ? ` — ${err.message}` : ''
      process.stderr.write(`[${scope}] ERROR: ${msg}${detail}\n`)
    },
  }
}
