import type { CellEvidence } from "../../../playwright/src/cell-runner.js";
import type { MatrixCell } from "../../../schema/src/records/context.js";

export const RENDERED_DETECTOR_VERSION = "1.0.0";

export const RENDERED_RULE_IDS = [
  "UI-010",
  "UI-011",
  "UI-012",
  "UI-013",
  "UI-014",
  "UI-015",
  "UI-016",
  "UI-017",
  "UI-018",
] as const;

export type RenderedRuleId = (typeof RENDERED_RULE_IDS)[number];

export type RenderedFixtureVariant =
  | "positive"
  | "negative"
  | "intentional-exception"
  | "rtl"
  | "changed-fingerprint";

export type VisibleTextObservation = {
  locator: string;
  text: string;
  normalizedText: string;
};

export type LandmarkObservation = {
  role: string;
  locator: string;
  accessibleName: string;
  hasAccessibleContent: boolean;
};

export type LoadingIndicatorObservation = {
  locator: string;
  visible: boolean;
  ariaBusy: boolean;
};

export type HydrationDiagnostic = {
  kind: "console" | "dom" | "identity-mismatch";
  message: string;
  locator?: string;
};

export type RenderedProbe = {
  htmlLang: string;
  htmlDir: string;
  textDirection: string;
  documentScroll: {
    scrollWidth: number;
    clientWidth: number;
  };
  visibleTexts: VisibleTextObservation[];
  landmarks: LandmarkObservation[];
  loadingIndicators: LoadingIndicatorObservation[];
  hydrationDiagnostics: HydrationDiagnostic[];
  codeExampleLocators: string[];
};

export type RenderedCellEvidence = CellEvidence & {
  renderedProbe: RenderedProbe;
};

export type RenderedFixture = {
  fixtureId: string;
  ruleId: RenderedRuleId;
  variant: RenderedFixtureVariant;
  route: string;
  html: string;
  cell?: Partial<MatrixCell>;
  intentionalContractId?: string;
};

export type RenderedViolationFact = {
  ruleId: RenderedRuleId;
  kind: string;
  locator: string;
  measurement: Record<string, string | number | boolean>;
  summary: string;
};

export type Ui001EvidenceRecord = {
  ruleId: RenderedRuleId;
  route: string;
  url: string;
  role: string;
  locale: string;
  viewport: MatrixCell["viewport"];
  state: MatrixCell["state"];
  actions: MatrixCell["actions"];
  timestampPhase: string;
  targetLocator: string;
  measuredEvidence: Record<string, unknown>;
  screenshotRefs: string[];
  detectorVersion: string;
  classificationBasis: string;
  violation: RenderedViolationFact;
};
