/**
 * Centralized logging utility for FitAI Mobile App
 * Provides structured logging with appropriate log levels
 *
 * In production, logs can be sent to a remote logging service
 * For now, we use console with prefixes for easy filtering
 */

/**
 * Log levels
 */
type LogLevel = "debug" | "info" | "warn" | "error";

/**
 * Check if we're in development mode
 */
const isDevelopment = __DEV__;

/**
 * Log level configuration
 * - Development: debug and above
 * - Production: info and above
 */
const LOG_LEVELS: Record<LogLevel, number> = {
  debug: 0,
  info: 1,
  warn: 2,
  error: 3,
};

const currentLogLevel: LogLevel = isDevelopment ? "debug" : "info";
const currentLogLevelValue = LOG_LEVELS[currentLogLevel];

/**
 * Check if a log level should be output
 */
function shouldLog(level: LogLevel): boolean {
  return LOG_LEVELS[level] >= currentLogLevelValue;
}

/**
 * Format log message with timestamp and level
 */
function formatMessage(level: LogLevel, message: string): string {
  const timestamp = new Date().toISOString();
  return `[${timestamp}] [${level.toUpperCase()}] ${message}`;
}

/**
 * Centralized logger
 */
export const log = {
  /**
   * Log debug information (only in development)
   */
  debug: (message: string, data?: Record<string, unknown>) => {
    if (shouldLog("debug")) {
      console.log(formatMessage("debug", message), data || "");
    }
  },

  /**
   * Log general information
   */
  info: (message: string, data?: Record<string, unknown>) => {
    if (shouldLog("info")) {
      console.log(formatMessage("info", message), data || "");
    }
  },

  /**
   * Log warnings
   */
  warn: (message: string, data?: Record<string, unknown>) => {
    if (shouldLog("warn")) {
      console.warn(formatMessage("warn", message), data || "");
    }
  },

  /**
   * Log errors with optional error object
   */
  error: (message: string, error?: unknown, data?: Record<string, unknown>) => {
    if (shouldLog("error")) {
      const errorInfo: Record<string, unknown> = {
        ...(data || {}),
      };

      if (error instanceof Error) {
        errorInfo.error = {
          name: error.name,
          message: error.message,
          stack: error.stack,
        };
      } else if (error) {
        errorInfo.error = error;
      }

      console.error(formatMessage("error", message), errorInfo);
    }
  },

  /**
   * Log API requests
   */
  apiRequest: (method: string, url: string, data?: Record<string, unknown>) => {
    log.debug(`API ${method} ${url}`, data);
  },

  /**
   * Log API responses
   */
  apiResponse: (
    method: string,
    url: string,
    status: number,
    data?: Record<string, unknown>,
  ) => {
    const level = status >= 400 ? "error" : status >= 300 ? "warn" : "info";
    if (level === "error") {
      log.error(`API ${method} ${url} - ${status}`, undefined, data);
    } else if (level === "warn") {
      log.warn(`API ${method} ${url} - ${status}`, data);
    } else {
      log.debug(`API ${method} ${url} - ${status}`, data);
    }
  },

  /**
   * Log authentication events
   */
  auth: (event: string, data?: Record<string, unknown>) => {
    log.info(`Auth: ${event}`, data);
  },

  /**
   * Log navigation events
   */
  navigation: (screen: string, data?: Record<string, unknown>) => {
    log.debug(`Navigation: ${screen}`, data);
  },
};

export default log;

/**
 * TODO: In production, consider integrating with:
 * - Sentry for error tracking
 * - LogRocket for session replay
 * - Custom logging service for analytics
 */
