// Monitoring Types

export interface LogEntry {
  id: string;
  timestamp: string;
  level: string; // "debug", "info", "warn", "error"
  message: string;
  caller: string;
  process: string; // "api" or "worker"
  fields: Record<string, unknown>;
}

export interface CPUStats {
  usage_percent: number;
  num_cpu: number;
  load_avg_1: number;
  load_avg_5: number;
  load_avg_15: number;
}

export interface MemoryStats {
  // System memory
  used_bytes: number;
  total_bytes: number;
  usage_percent: number;
  // Go heap
  heap_alloc: number;
  heap_sys: number;
  heap_inuse: number;
}

export interface DiskStats {
  used_bytes: number;
  total_bytes: number;
  free_bytes: number;
  usage_percent: number;
}

export interface RuntimeStats {
  goroutines: number;
  num_gc: number;
  last_gc_pause_ns: number;
  uptime_seconds: number;
}

export interface EndpointStats {
  count: number;
  avg_latency_ms: number;
  p95_latency_ms: number;
  error_rate: number;
}

export interface HTTPStats {
  requests_total: number;
  requests_per_minute: number;
  avg_latency_ms: number;
  error_rate: number;
  by_endpoint: Record<string, EndpointStats>;
  by_status_code: Record<number, number>;
}

export interface QueueStats {
  pending: number;
  active: number;
  scheduled: number;
  retry: number;
  archived: number;
  completed: number;
  failed: number;
}

export interface AsynqStats {
  queues: Record<string, QueueStats>;
}

export interface SystemStats {
  timestamp: string;
  process: string; // "api" or "worker"
  cpu: CPUStats;
  memory: MemoryStats;
  disk: DiskStats;
  runtime: RuntimeStats;
  http?: HTTPStats; // API only
  asynq?: AsynqStats; // Worker only
}

export interface LogFilters {
  level?: string;
  process?: string;
  search?: string;
  limit?: number;
  offset?: number;
}

export interface LogsResponse {
  logs: LogEntry[];
  total: number;
  offset: number;
  limit: number;
}

export interface StatsResponse {
  api?: SystemStats;
  worker?: SystemStats;
}

export type LogLevel = "debug" | "info" | "warn" | "error";

export const LOG_LEVELS: LogLevel[] = ["debug", "info", "warn", "error"];

export const LOG_LEVEL_COLORS: Record<LogLevel, string> = {
  debug: "text-gray-500",
  info: "text-blue-500",
  warn: "text-yellow-500",
  error: "text-red-500",
};

export const LOG_LEVEL_BG_COLORS: Record<LogLevel, string> = {
  debug: "bg-gray-100 text-gray-800",
  info: "bg-blue-100 text-blue-800",
  warn: "bg-yellow-100 text-yellow-800",
  error: "bg-red-100 text-red-800",
};
