import type { CoverageGraph } from "@elench/testkit-protocol";

export type DiscoverySelectionType = "int" | "e2e" | "scenario" | "dal" | "load" | "pw";
export type DiscoveryInternalType = "integration" | "e2e" | "scenario" | "dal" | "load";
export type DiscoveryFramework = "k6" | "playwright";

export interface DiscoveryDiagnostic {
  code: string;
  severity: "error" | "warning";
  message: string;
  path?: string;
  serviceNames?: string[];
}

export interface DiscoveryService {
  name: string;
  discovered: boolean;
  localCwd: string;
  dependsOn: string[];
  suiteCount: number;
  fileCount: number;
  activeFileCount: number;
  skippedFileCount: number;
}

export interface DiscoverySuite {
  id: string;
  service: string;
  name: string;
  displayName: string;
  selectionType: DiscoverySelectionType;
  internalType: DiscoveryInternalType;
  framework: DiscoveryFramework;
  groupLabel: string;
  fileCount: number;
  activeFileCount: number;
  skippedFileCount: number;
  dependsOn: string[];
  locks: string[];
  filePaths: string[];
}

export interface DiscoveryHistorySummary {
  firstSeenAt?: string | null;
  lastSeenAt?: string | null;
  lastRunAt?: string | null;
  runCount?: number;
  passCount?: number;
  failCount?: number;
  skipCount?: number;
  avgDurationMs?: number;
  lastStatus?: "passed" | "failed" | "skipped" | "not_run" | null;
}

export interface DiscoveryFile {
  id: string;
  path: string;
  displayName: string;
  service: string;
  suiteName: string;
  groupLabel: string;
  selectionType: DiscoverySelectionType;
  internalType: DiscoveryInternalType;
  framework: DiscoveryFramework;
  skipped: boolean;
  skipReason: string | null;
  locks: string[];
  dependsOn: string[];
  history?: DiscoveryHistorySummary;
}

export interface DiscoveryResult {
  schemaVersion: number;
  source: "testkit-discovery";
  product: {
    name: string;
    directory: string;
  };
  setupFile: string | null;
  filters: {
    service: string | null;
    types: DiscoverySelectionType[] | ["all"];
    suiteSelectors: string[];
    fileNames: string[];
    runnableOnly: boolean;
    diagnostics: "error" | "report";
  };
  services: DiscoveryService[];
  suites: DiscoverySuite[];
  files: DiscoveryFile[];
  coverageGraph: CoverageGraph | null;
  diagnostics: DiscoveryDiagnostic[];
  summary: {
    services: number;
    suites: number;
    files: number;
    activeFiles: number;
    skippedFiles: number;
    diagnostics: {
      errors: number;
      warnings: number;
    };
    byService: Record<string, number>;
    byType: Record<string, number>;
  };
  history: {
    available: boolean;
    path?: string;
  };
}

export interface DiscoverTestsOptions {
  dir?: string;
  service?: string;
  type?: string | string[];
  suite?: string | string[];
  file?: string | string[];
  runnableOnly?: boolean;
  diagnostics?: "error" | "report";
}

export declare function discoverTests(options?: DiscoverTestsOptions): Promise<DiscoveryResult>;
export declare function formatSelectionTypeLabel(type: DiscoverySelectionType): string;
export declare function formatDisplayName(value: string): string;
