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

export const ASSETS_DETECTOR_VERSION = "1.0.0";

export const ASSETS_RULE_IDS = ["UI-070", "UI-071", "UI-072"] as const;

export type AssetsRuleId = (typeof ASSETS_RULE_IDS)[number];

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

export type Rect = {
  x: number;
  y: number;
  width: number;
  height: number;
};

export type ImageObservation = {
  locator: string;
  src: string;
  currentSrc: string;
  naturalWidth: number;
  naturalHeight: number;
  complete: boolean;
  rect: Rect;
  visible: boolean;
  alt: string;
  decorative: boolean;
};

export type ContainerObservation = {
  locator: string;
  rect: Rect;
  visible: boolean;
  area: number;
  visibleDescendantCount: number;
  visibleDescendantLocators: string[];
  backgroundColor: string;
  backgroundImage: string;
  hasBackgroundMedia: boolean;
  associatedFailures: string[];
};

export type SiblingNorm = {
  locator: string;
  opacity: number;
  cursor: string;
  pointerEvents: string;
  semanticallyDisabled: boolean;
};

export type DisabledStylingObservation = {
  locator: string;
  opacity: number;
  cursor: string;
  pointerEvents: string;
  semanticallyDisabled: boolean;
  accessibleExplanation: string | null;
  siblingNorms: SiblingNorm[];
  looksDisabled: boolean;
  unexplained: boolean;
};

export type AssetsProbe = {
  images: ImageObservation[];
  containers: ContainerObservation[];
  disabledStyling: DisabledStylingObservation[];
};

export type AssetsArtifactRef = {
  relativePath: string;
  contentHash: string;
  mediaType: string;
  dimensions?: { width: number; height: number };
};

export type AssetsCellEvidence = CellEvidence & {
  assetsProbe: AssetsProbe;
  assetsArtifacts: {
    fullScreenshot: AssetsArtifactRef;
  };
};

export type AssetsFixture = {
  fixtureId: string;
  ruleId: AssetsRuleId;
  variant: AssetsFixtureVariant;
  route: string;
  html: string;
  cell?: Partial<MatrixCell>;
  intentionalContractId?: string;
};

export type AssetsViolationFact = {
  ruleId: AssetsRuleId;
  kind: string;
  locator: string;
  measurement: Record<string, unknown>;
  summary: string;
};

export type Ui001EvidenceRecord = {
  ruleId: AssetsRuleId;
  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: AssetsViolationFact;
};
