import { describe, expect, it, jest, afterEach, beforeEach } from "@jest/globals"
import { Logger, LogLevel } from "../logger.js"

describe("Logger child inheritance", () => {
  it("inherits configuration from parent logger", () => {
    const parentLogger = new Logger()
    parentLogger.updateConfig({
      level: LogLevel.DEBUG,
      enableConsole: false,
      enableSentry: false,
      enablePostHog: false,
      serviceName: "test-service",
    })

    const childLogger = parentLogger.child({ requestId: "req-123" })

    expect(childLogger.getConfig()).toEqual(parentLogger.getConfig())
  })
})

describe("Logger production debug output", () => {
  const originalNodeEnv = process.env.NODE_ENV
  const originalLogLevel = process.env.LOG_LEVEL

  afterEach(() => {
    process.env.NODE_ENV = originalNodeEnv
    process.env.LOG_LEVEL = originalLogLevel
    jest.restoreAllMocks()
  })

  it("emits debug logs in production when log level permits", () => {
    process.env.NODE_ENV = "production"
    process.env.LOG_LEVEL = "debug"

    const prodLogger = new Logger()
    prodLogger.updateConfig({
      level: LogLevel.DEBUG,
      enableConsole: true,
      enableSentry: false,
      enablePostHog: false,
    })

    const consoleSpy = jest.spyOn(console, "debug").mockImplementation(() => {})
    const entry = (prodLogger as any).formatLogEntry(LogLevel.DEBUG, "Test debug log", {
      requestId: "req-abc",
    })

    ;(prodLogger as any).writeToConsole(entry)

    expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Test debug log"))
  })
})
