import type { ConsistencyFixture } from "./types.js";

export const quotaKnownBad: ConsistencyFixture = {
  fixtureId: "consistency/quota-known-bad",
  graph: {
    nodes: {
      "quota-label": {
        id: "quota-label",
        representation: {
          source: "visible-text",
          kind: "remaining-percentage-label",
          value: "58% left",
          provenance: "observed",
        },
      },
      "quota-fill": {
        id: "quota-fill",
        representation: {
          source: "progress-geometry",
          kind: "remaining-percentage-fill",
          value: 42,
          provenance: "observed",
        },
      },
    },
    relations: {
      "quota-remaining": {
        kind: "same-semantic",
        nodes: ["quota-label", "quota-fill"],
        declaredBy: "UNI-020",
        normalization: {
          name: "remaining-percentage",
          apply(value: unknown): unknown {
            if (typeof value === "string") {
              const match = /(\d+(?:\.\d+)?)\s*%\s*left/i.exec(value);
              if (match?.[1] !== undefined) {
                return Number(match[1]);
              }
            }
            if (typeof value === "number" && Number.isFinite(value)) {
              return value;
            }
            throw new Error("cannot normalize to remaining percentage");
          },
        },
      },
    },
  },
};
