export declare const TESTKIT_BROWSER_PROTOCOL_VERSION = 1;
export declare const TESTKIT_COVERAGE_GRAPH_VERSION = 1;

export type BrowserTargetKind = "testId" | "role" | "text" | "css" | "xpath" | "component";
export type BrowserConfidence = "low" | "medium" | "high";
export type BrowserFailureState = "failing" | "healthy" | "unavailable";
export type BrowserCoverageState = "covered" | "missing" | "unavailable";

export type CoverageNodeKind =
  | "page_view"
  | "ui_surface"
  | "ui_action"
  | "client_request"
  | "api_route"
  | "server_action"
  | "server_capability"
  | "data_capability"
  | "test_file";

export type CoverageEdgeKind =
  | "contains"
  | "renders"
  | "triggers"
  | "requests"
  | "handles"
  | "delegates_to"
  | "covers";

export type CoverageEvidenceSource = "convention" | "static" | "runtime";

export interface BrowserTarget {
  kind: BrowserTargetKind;
  value: string;
  label?: string;
  confidence?: BrowserConfidence;
}

export interface BrowserMetadata {
  label?: string;
  origins?: string[];
  routes?: string[];
  targets?: BrowserTarget[];
}

export interface BrowserMetadataDocument {
  schemaVersion: number;
  browser: BrowserMetadata;
}

export interface CoverageGraphNode {
  id: string;
  kind: CoverageNodeKind;
  service: string;
  label: string;
  filePath?: string;
  route?: string;
  method?: string;
  path?: string;
  target?: BrowserTarget | null;
  metadata?: Record<string, string | number | boolean | null>;
}

export interface CoverageGraphEdge {
  id: string;
  kind: CoverageEdgeKind;
  from: string;
  to: string;
  confidence?: BrowserConfidence;
  metadata?: Record<string, string | number | boolean | null>;
}

export interface CoverageEvidence {
  id: string;
  source: CoverageEvidenceSource;
  confidence?: BrowserConfidence;
  service: string;
  suiteName: string;
  selectionType: string;
  framework: string;
  testFilePath: string;
  coveredNodeIds: string[];
  details?: {
    requestPaths?: string[];
    route?: string;
  };
}

export interface CoverageGraph {
  schemaVersion: number;
  nodes: CoverageGraphNode[];
  edges: CoverageGraphEdge[];
  evidence: CoverageEvidence[];
}

export interface BridgeProductRef {
  name: string;
  directory: string;
}

export interface BridgeServiceRef {
  name: string;
  baseUrl: string;
  origin: string;
}

export interface BrowserMatchResponse {
  protocolVersion: number;
  url: string;
  origin: string;
  route: string;
  matched: boolean;
  product: BridgeProductRef;
  service: BridgeServiceRef | null;
}

export interface BridgeRunSummary {
  artifactAvailable: boolean;
  generatedAt: string | null;
  status: string | null;
}

export interface BridgeSupportingTestRef {
  service: string;
  suite: string;
  type: string;
  framework: string;
  filePath: string;
  label: string;
  status?: "passed" | "failed" | "skipped" | "not_run";
  error?: string | null;
}

export interface BridgeCoverageViaNodeRef {
  id: string;
  kind: CoverageNodeKind;
  label: string;
  route?: string;
  method?: string;
  path?: string;
}

export interface FailureOverlayEntry {
  id: string;
  kind: CoverageNodeKind;
  label: string;
  service: string;
  route?: string | null;
  targets: BrowserTarget[];
  failedTests: BridgeSupportingTestRef[];
  viaNodes: BridgeCoverageViaNodeRef[];
}

export interface CoverageOverlayEntry {
  id: string;
  kind: CoverageNodeKind;
  label: string;
  service: string;
  route?: string | null;
  targets: BrowserTarget[];
  supportingTests: BridgeSupportingTestRef[];
  viaNodes: BridgeCoverageViaNodeRef[];
  confidence: BrowserConfidence;
}

export interface PageOverlayResponse {
  protocolVersion: number;
  page: {
    url: string;
    origin: string;
    route: string;
  };
  match: BrowserMatchResponse;
  run: BridgeRunSummary;
  summary: {
    failureState: BrowserFailureState;
    coverageState: BrowserCoverageState;
    relatedFailureCount: number;
    relatedCoverageCount: number;
  };
  failures: FailureOverlayEntry[];
  coverage: CoverageOverlayEntry[];
}

export interface BridgeErrorResponse {
  protocolVersion: number;
  error: {
    code: string;
    message: string;
  };
}

export declare function normalizeBrowserTarget(value: unknown): BrowserTarget | null;
export declare function normalizeBrowserMetadata(value: unknown): BrowserMetadata | null;
export declare function normalizeBrowserMetadataDocument(value: unknown): BrowserMetadataDocument | null;
export declare function normalizeCoverageGraphNode(value: unknown): CoverageGraphNode | null;
export declare function normalizeCoverageGraphEdge(value: unknown): CoverageGraphEdge | null;
export declare function normalizeCoverageEvidence(value: unknown): CoverageEvidence | null;
export declare function normalizeCoverageGraph(value: unknown): CoverageGraph | null;
export declare function normalizePageOverlayResponse(value: unknown): PageOverlayResponse | null;
export declare function isPageOverlayResponse(value: unknown): value is PageOverlayResponse;
export declare function createBridgeErrorResponse(
  code: string,
  message: string
): BridgeErrorResponse;
