import winston from 'winston';
import path from 'path';
import fs from 'fs';

// 确保日志目录存在
const logDir = path.join(process.cwd(), 'logs');
if (!fs.existsSync(logDir)) {
  fs.mkdirSync(logDir, { recursive: true });
}

// 日志格式
const logFormat = winston.format.printf(({ level, message, timestamp, stack, ...meta }) => {
  const metaStr = Object.keys(meta).length > 0 ? `\n${JSON.stringify(meta, null, 2)}` : '';
  return `${timestamp} [${level}]: ${stack || message}${metaStr}`;
});

// 创建日志传输配置
const transports: winston.transport[] = [
  // 控制台输出
  new winston.transports.Console({
    format: winston.format.combine(
      winston.format.colorize(),
      winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
      winston.format.simple()
    ),
    level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
  }),

  // 错误日志文件
  new winston.transports.File({
    filename: path.join(logDir, 'error.log'),
    level: 'error',
    format: winston.format.combine(
      winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
      winston.format.errors({ stack: true }),
      logFormat
    ),
    maxsize: 10 * 1024 * 1024, // 10MB
    maxFiles: 10,
  }),

  // 常规日志文件
  new winston.transports.File({
    filename: path.join(logDir, 'combined.log'),
    format: winston.format.combine(
      winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
      winston.format.errors({ stack: true }),
      logFormat
    ),
    maxsize: 10 * 1024 * 1024, // 10MB
    maxFiles: 10,
  }),

  // HTTP 请求日志
  new winston.transports.File({
    filename: path.join(logDir, 'http.log'),
    level: 'http',
    format: winston.format.combine(
      winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
      logFormat
    ),
    maxsize: 10 * 1024 * 1024,
    maxFiles: 5,
  }),
];

// 创建日志记录器
const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || 'debug',
  transports,
  exitOnError: false,
});

// HTTP 请求日志中间件
export function httpLogger(ctx: any, next: any) {
  const start = Date.now();
  
  return next().then(() => {
    const ms = Date.now() - start;
    logger.http({
      type: 'request',
      method: ctx.method,
      url: ctx.url,
      status: ctx.status,
      duration: `${ms}ms`,
      ip: ctx.ip,
      userAgent: ctx.headers['user-agent'],
    });
  }).catch((err: any) => {
    const ms = Date.now() - start;
    logger.error({
      type: 'request_error',
      method: ctx.method,
      url: ctx.url,
      status: err.status || 500,
      duration: `${ms}ms`,
      error: err.message,
      stack: err.stack,
    });
    throw err;
  });
}

// 便捷方法
export const log = {
  info: (message: string, meta?: any) => logger.info(message, meta),
  warn: (message: string, meta?: any) => logger.warn(message, meta),
  error: (message: string, meta?: any) => logger.error(message, meta),
  debug: (message: string, meta?: any) => logger.debug(message, meta),
  http: (message: string, meta?: any) => logger.http(message, meta),
};

export default logger;
