"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import Link from "next/link";
import { useParams, useRouter } from "next/navigation";
import { useQuery } from "@tanstack/react-query";
import { apiFetch } from "@open-mercato/ui/backend/utils/api";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Page, PageBody } from "@open-mercato/ui/backend/Page";
import { Button } from "@open-mercato/ui/primitives/button";
import { Spinner } from "@open-mercato/ui/primitives/spinner";
import { JsonDisplay } from "@open-mercato/ui/backend/JsonDisplay";
import { RecordNotFoundState, ErrorMessage } from "@open-mercato/ui/backend/detail";
function ExecutionLogDetailPage() {
  const router = useRouter();
  const params = useParams();
  let logId;
  if (params?.slug && Array.isArray(params.slug)) {
    logId = params.slug[1];
  } else if (params?.id) {
    logId = Array.isArray(params.id) ? params.id[0] : params.id;
  }
  const t = useT();
  const { data: log, isLoading, error } = useQuery({
    queryKey: ["business-rules", "logs", logId],
    queryFn: async () => {
      const response = await apiFetch(`/api/business_rules/logs/${logId}`);
      if (!response.ok) {
        const httpErr = new Error(
          response.status === 404 ? t("business_rules.logs.errors.notFound", "Execution log not found.") : t("business_rules.logs.errors.fetchFailed")
        );
        httpErr.status = response.status;
        throw httpErr;
      }
      const result = await response.json();
      return result;
    },
    enabled: !!logId
  });
  if (isLoading) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsxs("div", { className: "flex h-[50vh] flex-col items-center justify-center gap-2 text-muted-foreground", children: [
      /* @__PURE__ */ jsx(Spinner, { className: "h-6 w-6" }),
      /* @__PURE__ */ jsx("span", { children: t("business_rules.logs.detail.loading") })
    ] }) }) });
  }
  const isNotFound = !isLoading && error?.status === 404;
  if (isNotFound) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(
      RecordNotFoundState,
      {
        label: t("business_rules.logs.errors.notFound", "Execution log not found."),
        backHref: "/backend/logs",
        backLabel: t("business_rules.logs.backToList", "Back to logs")
      }
    ) }) });
  }
  if (error || !log) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(
      ErrorMessage,
      {
        label: error?.message ?? t("business_rules.logs.errors.loadFailed"),
        action: /* @__PURE__ */ jsx(Button, { asChild: true, variant: "outline", size: "sm", children: /* @__PURE__ */ jsx(Link, { href: "/backend/logs", children: t("business_rules.logs.backToList", "Back to logs") }) })
      }
    ) }) });
  }
  const getResultBadgeClass = (result) => {
    switch (result) {
      case "SUCCESS":
        return "bg-green-100 text-green-800";
      case "FAILURE":
        return "bg-yellow-100 text-yellow-800";
      case "ERROR":
        return "bg-red-100 text-red-800";
      default:
        return "bg-muted text-foreground";
    }
  };
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsxs("div", { className: "space-y-6", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("h1", { className: "text-2xl font-bold text-foreground", children: t("business_rules.logs.detail.title") }),
        /* @__PURE__ */ jsxs("p", { className: "text-sm text-muted-foreground mt-1", children: [
          t("business_rules.logs.detail.logId"),
          ": ",
          log.id
        ] })
      ] }),
      /* @__PURE__ */ jsx(Button, { onClick: () => router.push("/backend/logs"), variant: "outline", children: t("business_rules.logs.backToList") })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-card p-6", children: [
      /* @__PURE__ */ jsx("h2", { className: "text-lg font-semibold mb-4", children: t("business_rules.logs.detail.summary") }),
      /* @__PURE__ */ jsxs("dl", { className: "grid grid-cols-1 md:grid-cols-2 gap-4", children: [
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.executedAt") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1 text-sm text-foreground", children: new Date(log.executedAt).toLocaleString() })
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.result") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1", children: /* @__PURE__ */ jsx(
            "span",
            {
              className: `inline-flex items-center px-2 py-1 rounded text-xs font-medium ${getResultBadgeClass(
                log.executionResult
              )}`,
              children: t(`business_rules.logs.result.${log.executionResult.toLowerCase()}`)
            }
          ) })
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.executionTime") }),
          /* @__PURE__ */ jsxs("dd", { className: "mt-1 text-sm text-foreground", children: [
            log.executionTimeMs,
            "ms"
          ] })
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.executedBy") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1 text-sm text-foreground", children: log.executedBy || t("common.unknown") })
        ] })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-card p-6", children: [
      /* @__PURE__ */ jsx("h2", { className: "text-lg font-semibold mb-4", children: t("business_rules.logs.detail.ruleInfo") }),
      log.rule ? /* @__PURE__ */ jsxs("dl", { className: "grid grid-cols-1 md:grid-cols-2 gap-4", children: [
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.ruleName") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1", children: /* @__PURE__ */ jsx(
            Link,
            {
              href: `/backend/rules/${log.rule.id}`,
              className: "text-sm text-primary hover:underline",
              children: log.rule.ruleName
            }
          ) })
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.ruleType") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1 text-sm text-foreground", children: log.rule.ruleType })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "md:col-span-2", children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.ruleId") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1 text-sm text-foreground font-mono", children: log.rule.ruleId })
        ] })
      ] }) : /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t("business_rules.logs.ruleDeleted") })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-card p-6", children: [
      /* @__PURE__ */ jsx("h2", { className: "text-lg font-semibold mb-4", children: t("business_rules.logs.detail.entityInfo") }),
      /* @__PURE__ */ jsxs("dl", { className: "grid grid-cols-1 md:grid-cols-2 gap-4", children: [
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.entityType") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1 text-sm text-foreground", children: log.entityType })
        ] }),
        /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.eventType") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1 text-sm text-foreground", children: log.eventType || t("common.none") })
        ] }),
        log.entityId && /* @__PURE__ */ jsxs("div", { className: "md:col-span-2", children: [
          /* @__PURE__ */ jsx("dt", { className: "text-sm font-medium text-muted-foreground", children: t("business_rules.logs.fields.entityId") }),
          /* @__PURE__ */ jsx("dd", { className: "mt-1 text-sm text-foreground font-mono break-all", children: log.entityId })
        ] })
      ] })
    ] }),
    log.errorMessage && /* @__PURE__ */ jsxs("div", { className: "rounded-lg border border-destructive bg-destructive/5 p-6", children: [
      /* @__PURE__ */ jsx("h2", { className: "text-lg font-semibold mb-4 text-destructive", children: t("business_rules.logs.detail.errorMessage") }),
      /* @__PURE__ */ jsx("pre", { className: "text-sm text-destructive whitespace-pre-wrap font-mono", children: log.errorMessage })
    ] }),
    log.inputContext && /* @__PURE__ */ jsx(
      JsonDisplay,
      {
        data: log.inputContext,
        title: t("business_rules.logs.detail.inputContext")
      }
    ),
    log.outputContext && /* @__PURE__ */ jsx(
      JsonDisplay,
      {
        data: log.outputContext,
        title: t("business_rules.logs.detail.outputContext")
      }
    ),
    log.resultValue && /* @__PURE__ */ jsx(
      JsonDisplay,
      {
        data: log.resultValue,
        title: t("business_rules.logs.detail.resultValue")
      }
    )
  ] }) }) });
}
export {
  ExecutionLogDetailPage as default
};
//# sourceMappingURL=page.js.map
