"use client";
import { cn } from "../../../lib/utils";
import { useTranslations } from "next-intl";
import { useMemo } from "react";
import { RoundPageContainer } from "../../../components";
import {
Card,
CardAction,
CardContent,
CardHeader,
CardTitle,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "../../../shadcnui";
import { useTokenUsageAdmin } from "../contexts/TokenUsageAdminContext";
import type { StackBy } from "../data/tokenusage-admin.types";
import { TokenUsageAdminTiles } from "./TokenUsageAdminTiles";
import { TokenUsageBreakdownTable } from "./TokenUsageBreakdownTable";
import { TokenUsageRankedBar } from "./TokenUsageRankedBar";
import { TokenUsageTimelineChart } from "./TokenUsageTimelineChart";
const STACK_BY_VALUES: StackBy[] = ["scope", "type", "company"];
/**
* Page body for the administrative token-usage dashboard.
*
* Stateless by design — every value it renders comes from
* `useTokenUsageAdmin()`. The filter bar is deliberately NOT here: it belongs to
* the page title bar, which `RoundPageContainer` fills from `SharedContext`, so
* the provider publishes it (see TokenUsageAdminContext).
*/
export function TokenUsageAdminContainer() {
const t = useTranslations();
const {
summary,
timeline,
byCompany,
byUser,
byOperation,
byCustomerOperation,
filters,
setFilters,
singleCustomerMode,
isLoading,
error,
} = useTokenUsageAdmin();
// narr8-shaped deployments record essentially no platform spend (usage with no
// owning company), which would leave the platform tile and the
// platform-by-operation panel as permanent zeros. Treat "nothing to show" the
// same way a company filter is treated — the customer half then takes full
// width. a360ai is unaffected: it has real platform spend.
const platformIsEmpty = useMemo(
() =>
summary
.filter((row) => row.scope === "platform")
.every((row) => row.cost === 0 && row.credits === 0 && row.tokensIn === 0 && row.tokensOut === 0),
[summary],
);
const hidePlatform = singleCustomerMode || platformIsEmpty;
const stackByItems = useMemo(
() => ({
scope: t("token_usage.admin.stack.scope"),
type: t("token_usage.admin.stack.type"),
company: t("token_usage.admin.stack.company"),
}),
[t],
);
if (error) {
return (
{error}
);
}
// Loading renders nothing in the body: the title bar (with the filter bar) is
// already mounted, so a spinner would only make the controls jump on arrival.
if (isLoading) return ;
const emptyLabel = t("token_usage.admin.no_data");
return (
{/* The two cost centres, broken down the same way so they can be read
against each other. Operation types are vocabulary, not data — unlike
the company and user panels above, these labels get translated.
Platform spend has no owning company, so a company filter makes that
half meaningless: the provider skips the request and the card is
hidden rather than rendered empty, leaving the customer half full
width. The same holds when the window records no platform spend at
all — see `hidePlatform` above. */}