// Types for the EDM "Test the flow" QA check system.
//
// Each check is a self-contained module (id, label, description, run()). The
// drawer renders a checkbox per check from the registry and runs the selected
// ones against the template currently in the editor — frontend only, no API.

export type CheckStatus = "pass" | "warning" | "fail";

// A link surfaced in a result so QA can see/open it. `href` may be a merge tag
// (e.g. *|UNSUB|*); when `isMergeTag` is true there's no "Open" button.
export interface CheckLink {
  label: string;
  href: string;
  present: boolean;
  isMergeTag: boolean;
}

// A merge-tag variable found in the template.
export interface CheckVariable {
  token: string; // "*|FNAME|*"
  name: string; // "FNAME"
  count: number;
  kind: "merge" | "conditional"; // IF:/ELSE/END: → conditional
}

// An image asset found in the template, with a validity flag.
export interface CheckImage {
  src: string;
  ext: string; // "png" | "jpg" | ...
  ok: boolean; // allowed extension + https + no spaces + not broken
  alt: string;
  hasSpaces: boolean; // filename contains spaces or %20
  broken?: boolean;   // image failed to load (undefined = not checked)
}

export interface CheckResult {
  status: CheckStatus;
  // Specific findings shown under the status badge.
  messages: string[];
  // Optional structured detail rendered by the panel.
  links?: CheckLink[];
  variables?: CheckVariable[];
  images?: CheckImage[];
}

// Everything a check needs about the template under test. The HTML is parsed
// once (via DOMParser) and shared as `doc` so checks don't each re-parse.
export interface TemplateContext {
  name: string;
  subject: string;
  htmlContent: string;
  plainContent: string;
  doc: Document;
}

export interface EdmCheck {
  id: string;
  label: string;
  description: string;
  run(ctx: TemplateContext): CheckResult | Promise<CheckResult>;
}

// Placeholder result for checks whose rules aren't defined yet. Filling in a
// check tomorrow means replacing its `run` body — nothing else changes.
export const todoResult = (label: string): CheckResult => ({
  status: "warning",
  messages: [`TODO: "${label}" rule not defined yet.`],
});
