import { _ as __nuxt_component_0 } from './nuxt-link-Cq8xZgFP.mjs';
import { defineComponent, ref, withAsyncContext, watch, computed, mergeProps, withCtx, createTextVNode, toDisplayString, unref, useSSRContext } from 'vue';
import { ssrRenderAttrs, ssrRenderComponent, ssrInterpolate, ssrRenderClass, ssrRenderList } from 'vue/server-renderer';
import { u as useFetch } from './fetch-B5NxMLIO.mjs';
import { u as useWebSocket } from './useWebSocket-UbMCn0vY.mjs';
import { _ as _export_sfc, u as useI18n, a as useHead } from './server.mjs';
import '../nitro/nitro.mjs';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'stream';
import 'events';
import 'http';
import 'crypto';
import 'buffer';
import 'zlib';
import 'https';
import 'net';
import 'tls';
import 'url';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'vue-router';
import '@prisma/client';
import '@prisma/adapter-better-sqlite3';
import 'fs';
import 'path';
import 'fs/promises';
import 'child_process';
import 'node:child_process';
import 'node:url';
import '@vue/shared';
import '../routes/renderer.mjs';
import 'vue-bundle-renderer/runtime';
import 'unhead/server';
import 'devalue';
import 'unhead/utils';

const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "stats",
  __ssrInlineRender: true,
  async setup(__props) {
    let __temp, __restore;
    const { locale } = useI18n();
    const isRefreshing = ref(false);
    const { data: stats2, status, refresh } = ([__temp, __restore] = withAsyncContext(() => useFetch("/api/stats", "$ygpy_aVKAm")), __temp = await __temp, __restore(), __temp);
    const { lastMessage } = useWebSocket();
    watch(lastMessage, (message) => {
      if (message?.type === "claude_end" || message?.type === "stats-update" || message?.type === "refresh") {
        isRefreshing.value = true;
        refresh().finally(() => {
          isRefreshing.value = false;
        });
      }
    });
    const sessionTotalTokens = computed(() => {
      return (stats2.value?.session?.tokensInput || 0) + (stats2.value?.session?.tokensOutput || 0);
    });
    const totalTokens = computed(() => {
      return (stats2.value?.totals?.tokensInput || 0) + (stats2.value?.totals?.tokensOutput || 0);
    });
    const formatNumber = (num) => {
      if (num >= 1e6) {
        return (num / 1e6).toFixed(1) + "M";
      }
      if (num >= 1e3) {
        return (num / 1e3).toFixed(1) + "K";
      }
      return num.toString();
    };
    const formatCost = (cost) => {
      return cost.toFixed(4);
    };
    const formatDuration = (ms) => {
      if (!ms) return "-";
      if (ms < 1e3) {
        return ms + "ms";
      }
      return (ms / 1e3).toFixed(1) + "s";
    };
    const formatDate = (date) => {
      if (!date) return "-";
      return new Date(date).toLocaleString(locale.value === "fr" ? "fr-FR" : "en-US");
    };
    useHead({
      title: "Statistics - AutoCode"
    });
    return (_ctx, _push, _parent, _attrs) => {
      const _component_NuxtLink = __nuxt_component_0;
      _push(`<div${ssrRenderAttrs(mergeProps({ class: "stats-page" }, _attrs))} data-v-3e093864><header class="page-header" data-v-3e093864>`);
      _push(ssrRenderComponent(_component_NuxtLink, {
        to: "/kanban",
        class: "back-link"
      }, {
        default: withCtx((_, _push2, _parent2, _scopeId) => {
          if (_push2) {
            _push2(` ← ${ssrInterpolate(_ctx.$t("stats.backToKanban"))}`);
          } else {
            return [
              createTextVNode(" ← " + toDisplayString(_ctx.$t("stats.backToKanban")), 1)
            ];
          }
        }),
        _: 1
      }, _parent));
      _push(`<h1 data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.title"))}</h1><div class="header-badges" data-v-3e093864>`);
      if (unref(stats2)?.profile) {
        _push(`<span class="badge badge-profile" data-v-3e093864>${ssrInterpolate(unref(stats2).profile)}</span>`);
      } else {
        _push(`<!---->`);
      }
      if (unref(stats2)?.model) {
        _push(`<span class="badge badge-model" data-v-3e093864>${ssrInterpolate(unref(stats2).model)}</span>`);
      } else {
        _push(`<!---->`);
      }
      _push(`<span class="${ssrRenderClass([{ active: unref(isRefreshing) }, "refresh-indicator"])}" data-v-3e093864></span></div></header>`);
      if (unref(status) === "pending") {
        _push(`<div class="loading" data-v-3e093864>${ssrInterpolate(_ctx.$t("kanban.loading"))}</div>`);
      } else {
        _push(`<!--[-->`);
        if (unref(stats2)?.processingIssues?.length) {
          _push(`<div class="processing-alert card" data-v-3e093864><h3 data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.processing"))}</h3><div class="processing-issues" data-v-3e093864><!--[-->`);
          ssrRenderList(unref(stats2).processingIssues, (issue) => {
            _push(`<span class="processing-badge" data-v-3e093864><span class="dot-pulse" data-v-3e093864></span> ${ssrInterpolate(issue)}</span>`);
          });
          _push(`<!--]--></div></div>`);
        } else {
          _push(`<!---->`);
        }
        _push(`<div class="stats-grid" data-v-3e093864><div class="stat-card card" data-v-3e093864><h2 data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.session"))}</h2><div class="stat-value" data-v-3e093864>${ssrInterpolate(formatNumber(unref(sessionTotalTokens)))}</div><div class="stat-breakdown" data-v-3e093864><div class="breakdown-item" data-v-3e093864><span class="label" data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.tokensInput"))}</span><span class="value" data-v-3e093864>${ssrInterpolate(formatNumber(unref(stats2)?.session?.tokensInput || 0))}</span></div><div class="breakdown-item" data-v-3e093864><span class="label" data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.tokensOutput"))}</span><span class="value" data-v-3e093864>${ssrInterpolate(formatNumber(unref(stats2)?.session?.tokensOutput || 0))}</span></div></div><div class="stat-footer" data-v-3e093864><span class="calls-count" data-v-3e093864>${ssrInterpolate(unref(stats2)?.session?.callCount || 0)} ${ssrInterpolate(_ctx.$t("stats.calls"))}</span><span class="cost-value" data-v-3e093864>$${ssrInterpolate(formatCost(unref(stats2)?.session?.costUsd || 0))}</span></div>`);
        if (unref(stats2)?.session?.startedAt) {
          _push(`<div class="started-at" data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.startedAt"))}: ${ssrInterpolate(formatDate(unref(stats2).session.startedAt))}</div>`);
        } else {
          _push(`<!---->`);
        }
        _push(`</div><div class="stat-card card" data-v-3e093864><h2 data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.totals"))}</h2><div class="stat-value" data-v-3e093864>${ssrInterpolate(formatNumber(unref(totalTokens)))}</div><div class="stat-breakdown" data-v-3e093864><div class="breakdown-item" data-v-3e093864><span class="label" data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.tokensInput"))}</span><span class="value" data-v-3e093864>${ssrInterpolate(formatNumber(unref(stats2)?.totals?.tokensInput || 0))}</span></div><div class="breakdown-item" data-v-3e093864><span class="label" data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.tokensOutput"))}</span><span class="value" data-v-3e093864>${ssrInterpolate(formatNumber(unref(stats2)?.totals?.tokensOutput || 0))}</span></div></div><div class="stat-footer" data-v-3e093864><span class="calls-count" data-v-3e093864>${ssrInterpolate(unref(stats2)?.totals?.callCount || 0)} ${ssrInterpolate(_ctx.$t("stats.calls"))}</span><span class="cost-value" data-v-3e093864>$${ssrInterpolate(formatCost(unref(stats2)?.totals?.costUsd || 0))}</span></div></div><div class="stat-card card info-card" data-v-3e093864><h2 data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.cost"))}</h2><p class="cost-explanation" data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.costExplanation"))}</p></div></div><div class="card history-card" data-v-3e093864><h2 data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.recentActivity"))}</h2>`);
        if (!unref(stats2)?.history?.length) {
          _push(`<div class="empty" data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.noActivity"))}</div>`);
        } else {
          _push(`<div class="table-container" data-v-3e093864><table class="history-table" data-v-3e093864><thead data-v-3e093864><tr data-v-3e093864><th data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.issue"))}</th><th data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.column"))}</th><th data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.tokensInput"))}</th><th data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.tokensOutput"))}</th><th data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.cost"))}</th><th data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.duration"))}</th><th data-v-3e093864>${ssrInterpolate(_ctx.$t("stats.date"))}</th></tr></thead><tbody data-v-3e093864><!--[-->`);
          ssrRenderList(unref(stats2).history, (call, index) => {
            _push(`<tr data-v-3e093864><td data-v-3e093864>`);
            if (call.issue) {
              _push(ssrRenderComponent(_component_NuxtLink, {
                to: `/issue/${call.issue}`,
                class: "issue-link"
              }, {
                default: withCtx((_, _push2, _parent2, _scopeId) => {
                  if (_push2) {
                    _push2(`${ssrInterpolate(call.issue)}`);
                  } else {
                    return [
                      createTextVNode(toDisplayString(call.issue), 1)
                    ];
                  }
                }),
                _: 2
              }, _parent));
            } else {
              _push(`<span class="text-muted" data-v-3e093864>-</span>`);
            }
            _push(`</td><td data-v-3e093864>${ssrInterpolate(call.column || "-")}</td><td data-v-3e093864>${ssrInterpolate(formatNumber(call.tokensInput))}</td><td data-v-3e093864>${ssrInterpolate(formatNumber(call.tokensOutput))}</td><td class="cost-cell" data-v-3e093864>$${ssrInterpolate(formatCost(call.costUsd || 0))}</td><td data-v-3e093864>${ssrInterpolate(formatDuration(call.duration))}</td><td data-v-3e093864>${ssrInterpolate(formatDate(call.timestamp))}</td></tr>`);
          });
          _push(`<!--]--></tbody></table></div>`);
        }
        _push(`</div><!--]-->`);
      }
      _push(`</div>`);
    };
  }
});
const _sfc_setup = _sfc_main.setup;
_sfc_main.setup = (props, ctx) => {
  const ssrContext = useSSRContext();
  (ssrContext.modules || (ssrContext.modules = /* @__PURE__ */ new Set())).add("pages/stats.vue");
  return _sfc_setup ? _sfc_setup(props, ctx) : void 0;
};
const stats = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-3e093864"]]);

export { stats as default };
//# sourceMappingURL=stats-Cxo5MAfa.mjs.map
