import { REGEXPP_VERSION, SYNTAX_PROFILE } from "../version";
import type { RegexExecutionResult } from "../regex/model/match";
import type { RegexSyntaxResult } from "../regex/model/syntax";
import type { RegexEngineCapabilities } from "../regex/model/flavour";

function capability(
  capabilities: RegexEngineCapabilities | undefined,
  key: keyof RegexEngineCapabilities,
  unavailableDetail?: string,
): string {
  if (!capabilities) return "Awaiting first engine result";
  if (capabilities[key]) return "Available";
  return unavailableDetail
    ? `Unavailable — ${unavailableDetail}`
    : "Unavailable";
}

function offsetLabel(
  unit: RegexExecutionResult["engine"]["offsetUnit"] | undefined,
): string {
  switch (unit) {
    case "utf16":
      return "UTF-16 code units";
    case "code-point":
      return "Unicode code points";
    case "utf8-byte":
      return "UTF-8 bytes";
    case "byte":
      return "Bytes";
    default:
      return "Awaiting first engine result";
  }
}

export function CapabilityPanel({
  syntax,
  execution,
}: {
  readonly syntax?: RegexSyntaxResult;
  readonly execution?: RegexExecutionResult;
}) {
  const capabilities = execution?.engine.capabilities;
  const rows = [
    [
      "Flavour",
      execution?.engine.flavour ??
        syntax?.root.support.flavour ??
        "ECMAScript (awaiting workers)",
    ],
    [
      "Syntax provider",
      syntax
        ? `${syntax.provider.id} ${syntax.provider.version}`
        : `regexpp ${REGEXPP_VERSION} (awaiting syntax worker)`,
    ],
    ["Syntax profile", SYNTAX_PROFILE],
    [
      "Provider coverage",
      syntax
        ? `${syntax.coverage.status}: ${syntax.coverage.summary}`
        : "Awaiting syntax worker result",
    ],
    [
      "Execution engine",
      execution?.engine.engineName ?? "Awaiting first engine result",
    ],
    [
      "Engine/runtime version",
      execution?.engine.engineVersion ?? "Shown after first execution",
    ],
    ["Native offsets", offsetLabel(execution?.engine.offsetUnit)],
    ["Compilation", capability(capabilities, "compilation")],
    ["Matching", capability(capabilities, "matching")],
    ["Replacement", capability(capabilities, "replacement")],
    ["Named captures", capability(capabilities, "namedCaptures")],
    [
      "Capture history",
      capability(
        capabilities,
        "captureHistory",
        "the adapter exposes only the final retained capture",
      ),
    ],
    [
      "Actual execution trace",
      capability(
        capabilities,
        "actualTrace",
        "the adapter exposes no engine trace",
      ),
    ],
    ["Benchmark", capability(capabilities, "benchmark")],
  ] as const;
  return (
    <section
      className="panel capability-panel"
      aria-labelledby="capability-heading"
    >
      <header className="panel-heading">
        <div>
          <p className="eyebrow">Current provider and adapter metadata</p>
          <h2 id="capability-heading">Capabilities</h2>
        </div>
        <span className="provenance-badge">Community build</span>
      </header>
      <dl className="capability-grid">
        {rows.map(([term, value]) => (
          <div key={term}>
            <dt>{term}</dt>
            <dd>{value}</dd>
          </div>
        ))}
      </dl>
      <p className="capability-roadmap">
        Next flavour: official PCRE2 10.47 WebAssembly with actual callout
        traces. Python, Go, Rust, .NET and Java remain unavailable until their
        named runtimes pass the same worker, offset, conformance and licensing
        gates.
      </p>
    </section>
  );
}
