import fs from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import util from 'node:util'

import { PROJECT_ROOT, runtimeConfig } from '../config/runtime.js'
import type { HttpErrorLike } from './http.js'

const LOG_LEVEL_PRIORITY = {
  debug: 10,
  info: 20,
  warn: 30,
  error: 40,
}

const LEVEL_LABELS = {
  debug: 'DEBUG',
  info: 'INFO ',
  warn: 'WARN ',
  error: 'ERROR',
}

const ANSI = {
  reset: '\u001B[0m',
  dim: '\u001B[2m',
  bold: '\u001B[1m',
  gray: '\u001B[90m',
  cyan: '\u001B[36m',
  blue: '\u001B[34m',
  yellow: '\u001B[33m',
  red: '\u001B[31m',
}

const DATA_ROOT = resolveDataRoot()
const LOG_DIR = path.join(DATA_ROOT, 'logs')
const ACTIVE_LOG_LEVEL = normalizeLogLevel(runtimeConfig.logging?.level)
const LOG_RETENTION_DAYS = 7
const LEGACY_LOG_FILES = new Set(['app.log', 'webhook.log'])

let writeQueue = Promise.resolve()
let lastCleanupDateKey = ''

export function createRequestId(prefix = 'req') {
  return `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`
}

export function logDebug(scope, message, detail = undefined) {
  return writeLog('debug', scope, message, detail)
}

export function logInfo(scope, message, detail = undefined) {
  return writeLog('info', scope, message, detail)
}

export function logWarn(scope, message, detail = undefined) {
  return writeLog('warn', scope, message, detail)
}

export function logError(scope, message, detail = undefined) {
  return writeLog('error', scope, message, detail)
}

export function logWebhook(scope, message, detail = undefined, { level = 'info' } = {}) {
  return writeLog(level, scope, message, detail, { channel: 'webhook' })
}

function writeLog(level, scope, message, detail, { channel = 'app' } = {}) {
  if (!shouldWriteLog(level, ACTIVE_LOG_LEVEL)) {
    return null
  }

  const entry = {
    time: new Date().toISOString(),
    level: normalizeLogLevel(level),
    scope: String(scope || 'app').trim() || 'app',
    message: String(message || '').trim() || '-',
    pid: process.pid,
    detail: normalizeLogValue(detail),
  }

  writeConsole(entry)
  enqueueFileWrite(entry, resolveLogFilePath(channel, entry.time))
  return entry
}

function enqueueFileWrite(entry, filePath) {
  const line = formatLogEntry(entry, { color: false })
  const dateKey = extractLogDateKey(entry.time)

  writeQueue = writeQueue
    .then(async () => {
      await fs.mkdir(LOG_DIR, { recursive: true })
      await fs.appendFile(filePath, `${line}\n`, 'utf8')
      await cleanupExpiredLogsIfNeeded(dateKey)
    })
    .catch((error) => {
      const reason = error instanceof Error ? error.message : String(error || '未知错误')
      console.error('[logger] failed to write log file:', reason)
    })
}

function writeConsole(entry) {
  const logger = resolveConsoleMethod(entry.level)
  logger(formatLogEntry(entry, { color: supportsAnsiColor() }))
}

function resolveConsoleMethod(level) {
  const normalized = normalizeLogLevel(level)

  if (normalized === 'error') {
    return console.error
  }

  if (normalized === 'warn') {
    return console.warn
  }

  return console.log
}

export function normalizeLogLevel(level) {
  const normalized = String(level || '').trim().toLowerCase()
  return LOG_LEVEL_PRIORITY[normalized] != null ? normalized : 'info'
}

export function shouldWriteLog(level, configuredLevel = ACTIVE_LOG_LEVEL) {
  const normalizedLevel = normalizeLogLevel(level)
  const normalizedConfigured = normalizeLogLevel(configuredLevel)

  return LOG_LEVEL_PRIORITY[normalizedLevel] >= LOG_LEVEL_PRIORITY[normalizedConfigured]
}

export function formatLogEntry(entry, { color = false } = {}) {
  const normalizedEntry = {
    time: String(entry?.time || new Date().toISOString()),
    level: normalizeLogLevel(entry?.level),
    scope: String(entry?.scope || 'app').trim() || 'app',
    message: String(entry?.message || '').trim() || '-',
    pid: Number(entry?.pid || process.pid),
    detail: entry?.detail,
  }
  const timestamp = colorize(formatLogTimestamp(normalizedEntry.time), ANSI.gray, color)
  const level = colorize(LEVEL_LABELS[normalizedEntry.level], resolveLevelColor(normalizedEntry.level), color)
  const scope = colorize(normalizedEntry.scope, ANSI.cyan, color)
  const message = colorize(normalizedEntry.message, ANSI.bold, color)
  const separator = colorize('|', ANSI.dim, color)
  const { inline, block } = splitDetailPayload(normalizedEntry.detail)
  const inlineFields = formatInlineFields({
    pid: normalizedEntry.pid,
    ...inline,
  })
  const firstLine = [timestamp, level, scope, message, separator, inlineFields].filter(Boolean).join(' ')

  if (!block) {
    return firstLine
  }

  const inspected = util.inspect(block, {
    depth: 8,
    colors: color,
    compact: false,
    breakLength: 120,
    maxArrayLength: 100,
  })

  return `${firstLine}\n${indentBlock(inspected, '  ')}`
}

export function resolveLogFilePath(channel = 'app', time = new Date().toISOString()) {
  const normalizedChannel = String(channel || '').trim() === 'webhook' ? 'webhook' : 'app'
  return path.join(LOG_DIR, `${normalizedChannel}-${extractLogDateKey(time)}.log`)
}

export function resolveExpiredLogFilenames(
  fileNames = [],
  referenceTime = new Date().toISOString(),
  retentionDays = LOG_RETENTION_DAYS,
) {
  const normalizedRetentionDays = Math.max(1, Number(retentionDays) || LOG_RETENTION_DAYS)
  const cutoffDateKey = toDateKey(offsetDate(referenceTime, -(normalizedRetentionDays - 1)))

  return (Array.isArray(fileNames) ? fileNames : [])
    .filter((fileName) => shouldDeleteLogFileByDate(fileName, cutoffDateKey))
}

export function formatLogTimestamp(value) {
  const date = new Date(value)

  if (Number.isNaN(date.getTime())) {
    return String(value || '').trim() || new Date().toISOString()
  }

  return `${formatLocalDate(date)} ${formatLocalTime(date)}.${String(date.getMilliseconds()).padStart(3, '0')} ${formatTimezoneOffset(date)}`
}

function resolveDataRoot() {
  const configured = String(runtimeConfig.data?.root || '').trim()
  return configured ? path.resolve(configured) : path.resolve(PROJECT_ROOT, 'data')
}

function normalizeLogValue(value) {
  if (typeof value === 'undefined') {
    return undefined
  }

  try {
    return JSON.parse(
      JSON.stringify(value, (_key, current) => {
        if (current instanceof Error) {
          const currentError = current as Error & HttpErrorLike
          return {
            name: current.name,
            message: current.message,
            stack: current.stack,
            statusCode: currentError.statusCode,
            errorCode: currentError.errorCode,
          }
        }

        if (typeof current === 'bigint') {
          return String(current)
        }

        return current
      }),
    )
  } catch {
    return String(value)
  }
}

function splitDetailPayload(detail) {
  if (typeof detail === 'undefined') {
    return {
      inline: {},
      block: null,
    }
  }

  if (isScalarLogValue(detail)) {
    return {
      inline: { detail },
      block: null,
    }
  }

  if (Array.isArray(detail)) {
    return {
      inline: {},
      block: detail,
    }
  }

  if (detail && typeof detail === 'object') {
    const inline = {}
    const block = {}

    for (const [key, value] of Object.entries(detail)) {
      if (isScalarLogValue(value)) {
        inline[key] = value
        continue
      }

      block[key] = value
    }

    return {
      inline,
      block: Object.keys(block).length > 0 ? block : null,
    }
  }

  return {
    inline: { detail: String(detail) },
    block: null,
  }
}

function formatInlineFields(fields) {
  return Object.entries(fields)
    .filter(([, value]) => typeof value !== 'undefined' && value !== '')
    .map(([key, value]) => `${key}=${formatInlineValue(value)}`)
    .join(' ')
}

function formatInlineValue(value) {
  if (value === null) {
    return 'null'
  }

  if (typeof value === 'number' || typeof value === 'boolean') {
    return String(value)
  }

  const text = String(value || '')
  return /^[A-Za-z0-9_./:@-]+$/.test(text) ? text : JSON.stringify(text)
}

function isScalarLogValue(value) {
  return value === null || ['string', 'number', 'boolean'].includes(typeof value)
}

function indentBlock(text, indent) {
  return String(text || '')
    .split('\n')
    .map((line) => `${indent}${line}`)
    .join('\n')
}

function supportsAnsiColor() {
  return process.env.NO_COLOR !== '1' && Boolean(process.stdout.isTTY || process.stderr.isTTY)
}

function resolveLevelColor(level) {
  switch (normalizeLogLevel(level)) {
    case 'debug':
      return ANSI.blue
    case 'warn':
      return ANSI.yellow
    case 'error':
      return ANSI.red
    default:
      return ANSI.cyan
  }
}

function colorize(text, ansiCode, enabled) {
  if (!enabled || !ansiCode) {
    return text
  }

  return `${ansiCode}${text}${ANSI.reset}`
}

function formatLocalDate(date) {
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

function formatLocalTime(date) {
  return `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`
}

function formatTimezoneOffset(date) {
  const totalMinutes = -date.getTimezoneOffset()
  const sign = totalMinutes >= 0 ? '+' : '-'
  const absoluteMinutes = Math.abs(totalMinutes)
  const hours = String(Math.floor(absoluteMinutes / 60)).padStart(2, '0')
  const minutes = String(absoluteMinutes % 60).padStart(2, '0')
  return `${sign}${hours}:${minutes}`
}

async function cleanupExpiredLogsIfNeeded(dateKey) {
  if (!dateKey || dateKey === lastCleanupDateKey) {
    return
  }

  lastCleanupDateKey = dateKey

  try {
    const fileNames = await fs.readdir(LOG_DIR)
    const expired = resolveExpiredLogFilenames(fileNames, `${dateKey}T00:00:00.000Z`)
    await Promise.all(expired.map((fileName) => fs.rm(path.join(LOG_DIR, fileName), { force: true })))
  } catch (error) {
    const reason = error instanceof Error ? error.message : String(error || '未知错误')
    console.error('[logger] failed to cleanup old log files:', reason)
  }
}

function extractLogDateKey(time) {
  const normalized = String(time || '').trim()
  return /^\d{4}-\d{2}-\d{2}/.test(normalized) ? normalized.slice(0, 10) : toDateKey(normalized)
}

function toDateKey(value) {
  const date = new Date(value)

  if (Number.isNaN(date.getTime())) {
    return new Date().toISOString().slice(0, 10)
  }

  return date.toISOString().slice(0, 10)
}

function offsetDate(value, offsetDays) {
  const date = new Date(value)
  date.setUTCDate(date.getUTCDate() + Number(offsetDays || 0))
  return date
}

function shouldDeleteLogFileByDate(fileName, cutoffDateKey) {
  if (LEGACY_LOG_FILES.has(String(fileName || '').trim())) {
    return true
  }

  const matched = /^(app|webhook)-(\d{4}-\d{2}-\d{2})\.log$/.exec(String(fileName || '').trim())

  if (!matched) {
    return false
  }

  return matched[2] < cutoffDateKey
}
