import type { CheckResult as ApiCheckResult, CheckFailure } from "../../shared/api";

export interface CheckResult extends ApiCheckResult {
  targetId: string;
}

export interface DefaultsConfig {
  [checkerKey: string]: unknown;
  interval?: string;
  timeout?: string;
}

export interface ExecutionConfig {
  maxConcurrentChecks?: number;
}

export type JsonValue = boolean | JsonValue[] | null | number | string | { [key: string]: JsonValue };

export interface LoggingConfig {
  console?: LoggingConsoleConfig;
  file?: LoggingFileConfig;
  level?: LogLevel;
}

export interface LoggingConsoleConfig {
  level?: LogLevel;
}

export interface LoggingFileConfig {
  level?: LogLevel;
  path?: string;
  rotation?: LoggingFileRotationConfig;
}

export interface LoggingFileRotationConfig {
  frequency?: RotationFrequency;
  maxFiles?: number;
  size?: string;
}

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

export interface ProbeConfig {
  defaults?: DefaultsConfig;
  probes?: ProbesConfig;
  server?: ServerConfig;
  targets: RawTargetConfig[];
  variables?: Record<string, VariableValue>;
}

export interface ProbesConfig {
  execution?: ExecutionConfig;
}

export interface RawTargetConfig {
  [configKey: string]: unknown;
  description?: null | string;
  expect?: unknown;
  group?: string;
  id: string;
  interval?: string;
  name?: null | string;
  timeout?: string;
  type: string;
}

export interface ResolvedLoggingConfig {
  consoleLevel: LogLevel;
  fileLevel: LogLevel;
  filePath: string;
  rotationFrequency: RotationFrequency;
  rotationMaxFiles: number;
  rotationSizeBytes: number;
  rotationSizeRaw: string;
}

export interface ResolvedTargetBase {
  [key: string]: unknown;
  description: null | string;
  expect?: unknown;
  group: string;
  id: string;
  intervalMs: number;
  name: null | string;
  rawExpect?: unknown;
  timeoutMs: number;
  type: string;
}

export type RotationFrequency = "daily" | "hourly" | "weekly";

export interface ServerConfig {
  listen?: ServerListenConfig;
  logging?: LoggingConfig;
  storage?: ServerStorageConfig;
}

export interface ServerListenConfig {
  host?: string;
  port?: number;
}

export interface ServerStorageConfig {
  dataDir?: string;
  retention?: string;
}

export interface StoredCheckResult {
  duration_ms: null | number;
  failure: null | string;
  id: number;
  matched: number;
  observation: null | string;
  target_id: string;
  timestamp: string;
}

export interface StoredTarget {
  active: number;
  config: string;
  description: null | string;
  expect: null | string;
  grp: string;
  id: string;
  interval_ms: number;
  name: null | string;
  target: string;
  timeout_ms: number;
  type: string;
}

export type VariableValue = boolean | number | string;

export type { CheckFailure };
