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

import type { ConfigValidationIssue } from "../schema/issues";
import type { CheckResult, 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>;
  normalize(target: RawTargetConfig): RawTargetConfig;
  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 CheckerSchemaPair {
  config: TSchema;
  expect: TSchema;
}

export interface CheckerSchemas {
  authoring: CheckerSchemaPair;
  normalized: CheckerSchemaPair;
}

export interface CheckerValidationInput {
  targets: RawTargetConfig[];
}

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