import type { TSchema } from "@sinclair/typebox";

import type { ConfigValidationIssue } from "../schema/issues";
import type { CheckResult, DefaultsConfig, RawTargetConfig, ResolvedTargetBase } from "../types";

export type Checker<TResolved extends ResolvedTargetBase = ResolvedTargetBase> = CheckerDefinition<TResolved>;

export interface CheckerContext {
  signal: AbortSignal;
}

export interface CheckerDefinition<TResolved extends ResolvedTargetBase = ResolvedTargetBase> {
  buildDetail(observation: Record<string, unknown>): null | string;
  readonly configKey: string;
  execute(target: TResolved, ctx: CheckerContext): Promise<CheckResult>;
  resolve(target: RawTargetConfig, context: ResolveContext): TResolved;
  readonly schemas: CheckerSchemas;
  serialize(target: TResolved): { config: string; target: string };
  readonly type: string;
  validate(input: CheckerValidationInput): ConfigValidationIssue[];
}

export interface CheckerSchemas {
  config: TSchema;
  defaults: TSchema;
  expect: TSchema;
}

export interface CheckerValidationInput {
  defaults: DefaultsConfig;
  targets: RawTargetConfig[];
}

export interface ResolveContext {
  configDir: string;
  defaultIntervalMs: number;
  defaults: DefaultsConfig;
  defaultTimeoutMs: number;
}
