/**
 * Built-in Slack `eval` directive — run playground evals from a mention/DM
 * without per-agent custom handlers (parity with CLI / playground runs).
 *
 * Examples (after stripping @mentions / cursorAccount agent name):
 *   eval
 *   eval triage/open-incidents smoke
 *   eval --tag oncall
 *   eval list
 *   eval status
 *   eval help
 */

import type { EvalRunSnapshot, HostEvalsApi } from "../../evals.js";
import {
  EvalRunConflictError,
  EvalRunEmptyError,
} from "../../internal/eval-run-store.js";
import { stripLeadingSlackMentions } from "./stop.js";
import type { SlackBinding } from "./types.js";

const EVAL_POLL_MS = 2_000;
const TYPING_STATUS_MAX = 50;

export type SlackEvalDirective =
  | { kind: "help" }
  | { kind: "list" }
  | { kind: "status" }
  | { kind: "run"; filterIds?: string[]; tags?: string[] };

function escapeRegExp(value: string): string {
  return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

/**
 * Normalize inbound Slack text for directive parsing: strip leading
 * `<@U…>` mentions, leading `@U…` markdown mentions, and an optional
 * cursorAccount agentName prefix (`OncallBuddy eval …`).
 */
export function normalizeSlackEvalDirectiveBody(
  text: string,
  options?: { agentName?: string }
): string {
  let body = stripLeadingSlackMentions(text);
  // markdown form from slackTextToMarkdown / cursorAccount envelopes
  while (/^@[A-Za-z0-9._-]+\s+/.test(body)) {
    body = body.replace(/^@[A-Za-z0-9._-]+\s+/, "").trim();
  }
  const agentName = options?.agentName?.trim();
  if (agentName !== undefined && agentName.length > 0) {
    const prefix = new RegExp(`^${escapeRegExp(agentName)}\\s+`, "i");
    body = body.replace(prefix, "").trim();
  }
  return body;
}

/**
 * Parse a Slack eval directive. Returns null when the message is not an
 * eval command (falls through to the normal agent handler).
 */
export function parseSlackEvalDirective(
  text: string,
  options?: { agentName?: string }
): SlackEvalDirective | null {
  const body = normalizeSlackEvalDirectiveBody(text, options);
  if (body.length === 0) {
    return null;
  }
  const tokens = body.split(/\s+/).filter((t) => t.length > 0);
  if (tokens.length === 0 || !/^eval$/i.test(tokens[0] ?? "")) {
    return null;
  }

  const rest = tokens.slice(1);
  if (rest.length === 0) {
    return { kind: "run" };
  }

  const head = (rest[0] ?? "").toLowerCase();
  if (
    rest.length === 1 &&
    (head === "help" || head === "-h" || head === "--help")
  ) {
    return { kind: "help" };
  }
  if (rest.length === 1 && (head === "list" || head === "ls")) {
    return { kind: "list" };
  }
  if (rest.length === 1 && head === "status") {
    return { kind: "status" };
  }

  const filterIds: string[] = [];
  const tags: string[] = [];
  for (let i = 0; i < rest.length; i++) {
    const tok = rest[i] ?? "";
    if (tok === "--tag" || tok === "-t") {
      const value = rest[i + 1];
      if (value === undefined || value.length === 0) {
        return { kind: "help" };
      }
      tags.push(value);
      i += 1;
      continue;
    }
    if (tok.startsWith("--tag=")) {
      const value = tok.slice("--tag=".length);
      if (value.length === 0) {
        return { kind: "help" };
      }
      tags.push(value);
      continue;
    }
    if (tok.startsWith("-t=") && tok.length > 3) {
      tags.push(tok.slice(3));
      continue;
    }
    if (tok.startsWith("-")) {
      // Unknown flag — surface help rather than treating as an eval id.
      return { kind: "help" };
    }
    filterIds.push(tok);
  }

  const run: SlackEvalDirective = { kind: "run" };
  if (filterIds.length > 0) {
    run.filterIds = filterIds;
  }
  if (tags.length > 0) {
    run.tags = tags;
  }
  return run;
}

/** True when either raw Slack text or markdown form is an eval directive. */
export function isSlackEvalDirective(
  text: string,
  markdown: string,
  options?: { agentName?: string }
): boolean {
  return (
    parseSlackEvalDirective(text, options) !== null ||
    parseSlackEvalDirective(markdown, options) !== null
  );
}

export function formatSlackEvalHelp(): string {
  return [
    "*Eval commands* (same batches as the playground / `agentkit eval --prod`):",
    "• `eval` — run all evals",
    "• `eval <id> [id…]` — run specific eval ids",
    "• `eval --tag <tag>` — run evals with a tag (`-t` also works)",
    "• `eval list` — list available evals",
    "• `eval status` — show the active or latest eval run",
    "• `eval help` — this message",
  ].join("\n");
}

export function formatSlackEvalList(
  evals: Array<{
    id: string;
    description?: string;
    tags?: string[];
  }>
): string {
  if (evals.length === 0) {
    return "No evals found under `evals/`.";
  }
  const lines = [`*${evals.length} eval(s):*`];
  for (const item of evals) {
    const tags =
      item.tags === undefined || item.tags.length === 0
        ? ""
        : ` _[${item.tags.join(", ")}]_`;
    const desc =
      item.description === undefined || item.description === ""
        ? ""
        : ` — ${item.description}`;
    lines.push(`• \`${item.id}\`${tags}${desc}`);
  }
  return lines.join("\n");
}

export function formatSlackEvalRunSummary(run: EvalRunSnapshot): string {
  const { summary } = run;
  const statusLabel =
    run.status === "completed"
      ? "completed"
      : run.status === "failed"
        ? "failed"
        : "running";
  const lines = [
    `*Eval run* \`${run.runId}\` — *${statusLabel}*`,
    `• ${summary.passed} passed, ${summary.failed} failed, ${summary.total} total` +
      (summary.done < summary.total
        ? ` (${summary.done}/${summary.total} done)`
        : ""),
  ];
  if (run.error !== undefined && run.error !== "") {
    lines.push(`• error: ${run.error}`);
  }
  for (const c of run.cases) {
    const mark =
      c.status !== "done"
        ? c.status === "running"
          ? "RUN "
          : "….  "
        : c.ok === true
          ? "PASS"
          : "FAIL";
    const dur =
      c.durationMs === undefined
        ? ""
        : ` (${(c.durationMs / 1000).toFixed(1)}s)`;
    const err =
      c.ok === false && c.error !== undefined && c.error !== ""
        ? ` — ${c.error}`
        : "";
    lines.push(`\`${mark}\` \`${c.id}\`${dur}${err}`);
  }
  return lines.join("\n");
}

function truncateTyping(status: string): string {
  if (status.length <= TYPING_STATUS_MAX) {
    return status;
  }
  return `${status.slice(0, TYPING_STATUS_MAX - 1)}…`;
}

async function sleep(ms: number): Promise<void> {
  await new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

async function safeTyping(
  binding: SlackBinding,
  status: string
): Promise<void> {
  try {
    await binding.thread.startTyping(truncateTyping(status));
  } catch {
    // best-effort status
  }
}

async function safePost(binding: SlackBinding, text: string): Promise<void> {
  try {
    await binding.thread.post(text);
  } catch {
    // best-effort reply
  }
}

/**
 * Handle a parsed Slack eval directive against {@link HostEvalsApi}.
 * Posts results in-thread; does not start an agent turn.
 */
export async function handleSlackEvalDirective(args: {
  directive: SlackEvalDirective;
  evals: HostEvalsApi;
  binding: SlackBinding;
  /** Override poll interval (tests). */
  pollMs?: number;
}): Promise<void> {
  const { directive, evals, binding } = args;
  const pollMs = args.pollMs ?? EVAL_POLL_MS;

  if (directive.kind === "help") {
    await safePost(binding, formatSlackEvalHelp());
    return;
  }

  if (directive.kind === "list") {
    const listed = await evals.list();
    await safePost(binding, formatSlackEvalList(listed.evals));
    return;
  }

  if (directive.kind === "status") {
    const { runs, activeRunId } = await evals.listRunsWithActive();
    const run =
      (activeRunId === undefined ? undefined : await evals.get(activeRunId)) ??
      runs[0];
    if (run === undefined) {
      await safePost(binding, "No eval runs yet. Try `eval list` then `eval`.");
      return;
    }
    await safePost(binding, formatSlackEvalRunSummary(run));
    return;
  }

  await safeTyping(binding, "Starting evals…");
  let run: EvalRunSnapshot;
  try {
    run = await evals.start({
      filterIds: directive.filterIds,
      tags: directive.tags,
    });
  } catch (error) {
    if (error instanceof EvalRunConflictError) {
      const active = await evals.get(error.activeRunId);
      await safePost(
        binding,
        active === undefined
          ? `An eval run is already in progress (\`${error.activeRunId}\`).`
          : `An eval run is already in progress:\n${formatSlackEvalRunSummary(active)}`
      );
      return;
    }
    if (error instanceof EvalRunEmptyError) {
      await safePost(
        binding,
        "No matching evals. Try `eval list` or `eval help`."
      );
      return;
    }
    const message = error instanceof Error ? error.message : String(error);
    await safePost(binding, `Failed to start evals: ${message}`);
    return;
  }

  await safePost(
    binding,
    `Started eval run \`${run.runId}\` (${run.summary.total} case(s)).`
  );

  while (run.status === "running") {
    await safeTyping(
      binding,
      `Evals ${run.summary.done}/${run.summary.total}…`
    );
    await sleep(pollMs);
    const next = await evals.get(run.runId);
    if (next === undefined) {
      await safePost(
        binding,
        `Eval run \`${run.runId}\` disappeared before completion.`
      );
      return;
    }
    run = next;
  }

  await safePost(binding, formatSlackEvalRunSummary(run));
  await safeTyping(binding, "Evals done.");
}
