Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

31.1 KB
Raw Blame
/**
 * Pay-as-you-go billing & usage section — the SUBSCRIBED leader/member views.
 *
 * All data comes from the real {@code Wallet} snapshot ({@code GET
 * /api/v1/payg/wallet} via {@link useWallet}); there is no mock fallback. What
 * the wallet doesn't provide, the UI doesn't show:
 *
 *   - spend + cap render in the units/USD the backend actually returns
 *     ({@code spendUnitsThisPeriod}, {@code capUsd}, {@code noCap})
 *   - the per-category breakdown comes from {@code categoryBreakdown}
 *     (wallet_category_summary view)
 *   - money-equivalent display and the units↔money cap preview need
 *     stripe.prices via Sync Engine and are deliberately absent until that
 *     ships
 *   - the activity feed renders {@code wallet.recent}, which is {@code []} in
 *     V1 — so it shows a real empty state, not fabricated rows
 */
import React, { useState } from "react";
import { Alert, Badge, Group, Stack, Text } from "@mantine/core";
import { Button } from "@editor/ui/Button";
import { useRenderCount } from "@editor/hooks/useRenderCount";
import { Icon } from "@editor/ui/Icon";
import { alert as showToast } from "@editor/components/toast";
import {
  PrepaidCapacityMeterPanel,
  prepaidSnapshotFromWallet,
} from "@editor/components/shared/config/configSections/usageMeters";
// Relative (not @editor/*) so the co-located CSS + sibling component resolve directly.
// oxlint-disable-next-line no-restricted-imports
import "./Payg.css";
// oxlint-disable-next-line no-restricted-imports
import SpendCapControl from "./SpendCapControl";
import { useTranslation } from "react-i18next";
import type { Wallet } from "@editor/hooks/useWallet";
import { privateContentClassName } from "@editor/components/shared/privateContentClassName";

// ─── Types ────────────────────────────────────────────────────────────────

type Gate = "OFFSITE_PROCESSING" | "AUTOMATION" | "AI_SUPPORT" | "CLIENT_SIDE";

interface PaygProps {
  role: "LEADER" | "MEMBER";
  /** Real wallet snapshot from {@code useWallet}. Single source of truth. */
  wallet: Wallet;
  /**
   * Persist a cap change. Provided by {@code Plan} → {@code useWallet} for the
   * leader view; absent on the member view (read-only).
   */
  onSaveCap?: (capUsd: number | null) => Promise<void> | void;
  /**
   * Open the Stripe Customer Portal. When omitted the Stripe card is hidden.
   * On error the implementation shows a friendly toast and resolves — callers
   * don't need to wrap in try/catch.
   */
  onOpenPortal?: () => Promise<void>;
}

// ─── Helpers ──────────────────────────────────────────────────────────────

// Stable-ish avatar colour from a string.
const AVATAR_COLORS = [
  "var(--c-avatar-1)",
  "var(--c-avatar-2)",
  "var(--c-avatar-3)",
  "var(--c-avatar-4)",
  "var(--c-avatar-5)",
  "var(--c-avatar-6)",
];
function avatarColor(seed: string): string {
  let h = 0;
  for (let i = 0; i < seed.length; i++) h = (h * 31 + seed.charCodeAt(i)) | 0;
  return AVATAR_COLORS[Math.abs(h) % AVATAR_COLORS.length];
}

function gateLabel(
  g: Gate,
  t: (k: string, fallback: string) => string,
): string {
  switch (g) {
    case "OFFSITE_PROCESSING":
      return t(
        "payg.gates.offsite",
        "Server tools (compress, OCR, convert, watermark…)",
      );
    case "AUTOMATION":
      return t("payg.gates.automation", "Automations & pipelines");
    case "AI_SUPPORT":
      return t("payg.gates.ai", "AI tools (AI Create, suggestions, AI-OCR)");
    case "CLIENT_SIDE":
      return t(
        "payg.gates.client",
        "Browser-only tools (viewer, page editor, file management)",
      );
  }
}

// ─── "What counts as a document?" help ──────────────────────────────────────

/**
 * Expandable explainer for the billing unit. Shared by the subscribed hero
 * here and the free-tier hero in {@code PaygFree.tsx}. The bullets state the
 * real charge mechanics (DefaultDocumentClassifier + JobChargeService)
 * without hardcoding the policy-tunable thresholds: each non-empty file is
 * at least one document; page count / file size can make it more; chained
 * steps on the same file join the open process instead of re-charging; a
 * first-step failure writes a compensating refund.
 */
export function DocHelp() {
  const { t } = useTranslation();
  const [open, setOpen] = useState(false);
  return (
    <div className="payg-help">
      <Button
        variant="tertiary"
        className="payg-help__toggle"
        aria-expanded={open}
        onClick={() => setOpen((o) => !o)}
        leftSection={<Icon name="circle-question-mark" size={15} />}
      >
        {t("payg.docHelp.toggle", "What counts as a PDF?")}
        <Icon name="chevron-down" size={16} className="payg-help__chevron" />
      </Button>
      {open && (
        <div className="payg-help__panel">
          <ul>
            <li>
              {t(
                "payg.docHelp.billable",
                "Only automation runs, AI tools, and API calls count. Manual tools in the editor are always free.",
              )}
            </li>
            <li>
              {t(
                "payg.docHelp.perFile",
                "Each file you process counts as one PDF. Very long or very large files can count as more than one.",
              )}
            </li>
            <li>
              {t(
                "payg.docHelp.chains",
                "Running the same file through several steps of one automation counts it once, not once per step.",
              )}
            </li>
            <li>
              {t(
                "payg.docHelp.refunds",
                "If a job fails on its first step, the PDF is credited back automatically.",
              )}
            </li>
          </ul>
        </div>
      )}
    </div>
  );
}

// ─── Hero usage panel ───────────────────────────────────────────────────────

/**
 * Format minor units of an ISO currency for display ("$2.24", "£0.40"). Only
 * called when the backend resolved the rate — currency is always present
 * alongside a non-null money amount.
 */
function formatMinor(minor: number, currency: string | null): string {
  const code = (currency ?? "usd").toUpperCase();
  try {
    return new Intl.NumberFormat(undefined, {
      style: "currency",
      currency: code,
    }).format(minor / 100);
  } catch {
    return `${(minor / 100).toFixed(2)} ${code}`;
  }
}

/** Currency symbol for compact inline use; falls back to the ISO code. */
function currencySymbol(currency: string | null): string {
  switch ((currency ?? "").toLowerCase()) {
    case "usd":
      return "$";
    case "eur":
      return "€";
    case "gbp":
      return "£";
    default:
      return currency ? currency.toUpperCase() + " " : "$";
  }
}

/**
 * Usage hero — gradient panel with the allowance bar. Every number is real:
 * {@code billableUsed} is the ledger's period sum over the team's actual
 * billing window (the Stripe subscription period); {@code billableLimit} is
 * the backend-derived document ceiling (free allowance + what the money cap
 * buys at the subscription Price's per-document rate, null when uncapped);
 * {@code estimatedBillMinor} is spend beyond the allowance at that rate.
 * Fields the backend couldn't resolve are null and simply not rendered.
 */
function UsageHero({ wallet }: { wallet: Wallet }) {
  const { t } = useTranslation();

  const periodEnd = new Date(wallet.billingPeriodEnd);
  const daysLeft = Math.max(
    0,
    Math.ceil((periodEnd.getTime() - Date.now()) / 86_400_000),
  );
  const hasCap = !wallet.noCap && wallet.capUsd != null;
  const breakdown = wallet.categoryBreakdown;

  const limit = wallet.billableLimit;
  const hasLimit = limit != null && limit > 0;
  const pct = hasLimit ? Math.min(100, (wallet.billableUsed / limit) * 100) : 0;
  const state = hasLimit
    ? pct >= 100
      ? "DEGRADED"
      : pct >= 80
        ? "WARNED"
        : "FULL"
    : "FULL";
  const stateLabel = {
    FULL: t("payg.state.full", "Healthy"),
    WARNED: t("payg.state.warned", "Approaching cap"),
    DEGRADED: t("payg.state.degraded", "Cap reached"),
  }[state];

  return (
    <div className="payg-hero" data-state={state}>
      <div className="payg-hero__inner">
        <Group justify="space-between" align="flex-start" wrap="nowrap">
          <div>
            <div className="payg-hero__eyebrow">
              {t("payg.usage.thisPeriod", "This billing period")}
            </div>
            <div className="payg-hero__figure">
              <span className="payg-hero__spend">
                {wallet.billableUsed.toLocaleString()}
              </span>
              <span className="payg-hero__cap">
                {hasLimit
                  ? t(
                      "payg.usage.ofLimitProcessed",
                      "/ {{limit}} PDFs processed",
                      { limit: limit.toLocaleString() },
                    )
                  : t("payg.usage.processed", "PDFs processed")}
              </span>
            </div>
          </div>
          <div className="payg-status" data-state={state}>
            <span className="payg-status__dot" />
            {stateLabel}
          </div>
        </Group>

        {hasLimit && (
          <div className="payg-bar">
            <div
              className="payg-bar__fill"
              data-state={state}
              style={{ width: `${pct}%` }}
            />
          </div>
        )}

        <div className="payg-hero__meta">
          <span>
            {t("payg.usage.firstFree", "First {{free}} free", {
              free: wallet.freeAllowance.toLocaleString(),
            })}
          </span>
          {wallet.estimatedBillMinor != null && (
            <>
              <span className="payg-hero__meta-dot">•</span>
              <span>
                {t("payg.usage.estBill", "≈ {{amount}} so far this period", {
                  amount: formatMinor(
                    wallet.estimatedBillMinor,
                    wallet.currency,
                  ),
                })}
              </span>
            </>
          )}
          <span className="payg-hero__meta-dot">•</span>
          <span>
            {hasCap
              ? t("payg.usage.capLine", "{{cap}}/mo cap", {
                  cap: `${currencySymbol(wallet.currency)}${wallet.capUsd}`,
                })
              : t("payg.usage.noCap", "No monthly cap")}
          </span>
          <span className="payg-hero__meta-dot">•</span>
          <span>
            {daysLeft === 1
              ? t("payg.usage.resetsTomorrow", "Resets tomorrow")
              : t("payg.usage.resetsIn", "Resets in {{days}} days", {
                  days: daysLeft,
                })}
          </span>
        </div>

        <div className="payg-hero__meta" style={{ marginTop: 6 }}>
          <span>
            {t(
              "payg.usage.breakdown",
              "AI {{ai}} • Automation {{automation}} • API {{api}}",
              {
                ai: breakdown.ai.toLocaleString(),
                automation: breakdown.automation.toLocaleString(),
                api: breakdown.api.toLocaleString(),
              },
            )}
          </span>
        </div>

        <DocHelp />
      </div>
    </div>
  );
}

// ─── Cap editor ─────────────────────────────────────────────────────────────

interface CapEditorProps {
  /** Current cap in major currency units; null = no cap set. */
  capUsd: number | null;
  /** True when the leader explicitly disabled the cap. */
  noCap: boolean;
  /** Per-document rate in minor units; null when unknown — preview hides. */
  pricePerDocMinor: number | null;
  /** Currency of the rate; pairs with {@link CapEditorProps#pricePerDocMinor}. */
  currency: string | null;
  /**
   * Persist the cap change. Receives whole major units (matches the backend's
   * {@code PATCH /api/v1/payg/cap} body) or null for no-cap.
   */
  onSaveCap?: (capUsd: number | null) => Promise<void> | void;
}

/**
 * Single-row cap editor: the shared {@link SpendCapControl} (preset chips +
 * inline custom-entry pill + no-cap + inline Save) over a live "≈ N paid
 * PDFs/month" estimate, wrapped in the plan-page card chrome + the cap-reached
 * disclosure. Save-only — the working value is local, so abandoning the card
 * abandons the edit. The very same control drives the upgrade checkout flow.
 */
function CapEditor({
  capUsd,
  noCap,
  pricePerDocMinor,
  currency,
  onSaveCap,
}: CapEditorProps) {
  const { t } = useTranslation();
  const savedCap = noCap || capUsd == null ? null : capUsd;
  const [working, setWorking] = useState<number | null>(savedCap);

  return (
    <div className="payg-card">
      <Stack gap="sm">
        <div>
          <div className="payg-card__title">
            {t("payg.cap.title", "Monthly spending cap")}
          </div>
          <div className="payg-card__subtitle">
            {t(
              "payg.cap.subtitle",
              "The most your team can spend per month. Billable processing pauses at the cap and resumes next period.",
            )}
          </div>
        </div>

        <SpendCapControl
          capUsd={working}
          onChange={setWorking}
          pricePerDocMinor={pricePerDocMinor}
          currency={currency}
          savedCapUsd={savedCap}
          onSave={onSaveCap}
        />

        <CapReachedHelp />
      </Stack>
    </div>
  );
}

function CapReadOnly({
  capUsd,
  noCap,
}: {
  capUsd: number | null;
  noCap: boolean;
}) {
  const { t } = useTranslation();
  const hasCap = !noCap && capUsd != null;
  return (
    <div className="payg-card">
      <Stack gap="sm">
        <div className="payg-card__title">
          {t("payg.cap.title", "Monthly spending cap")}
        </div>
        <Group gap="xs" align="baseline">
          <Text fz={28} fw={750} lh={1}>
            {hasCap ? `$${capUsd}` : t("payg.cap.noneShort", "No cap")}
          </Text>
          {hasCap && (
            <Text c="dimmed">{t("payg.cap.perMonth", "/ month")}</Text>
          )}
        </Group>
        <div className="payg-preview" style={{ marginTop: 6 }}>
          <div>
            <div className="payg-preview__main">
              {t(
                "payg.member.askLeader",
                "Only your team owner can change the cap.",
              )}
            </div>
          </div>
        </div>

        <CapReachedHelp />
      </Stack>
    </div>
  );
}

// ─── Gates ────────────────────────────────────────────────────────────────

// What still works once the cap is hit. Everyday tools keep running; only AI
// and automation/pipelines pause until the cap resets or is raised.
const GATE_CAP_BEHAVIOR: { gate: Gate; staysAtCap: boolean }[] = [
  { gate: "CLIENT_SIDE", staysAtCap: true },
  { gate: "OFFSITE_PROCESSING", staysAtCap: true },
  { gate: "AUTOMATION", staysAtCap: false },
  { gate: "AI_SUPPORT", staysAtCap: false },
];

/**
 * Collapsed "what happens at the cap" disclosure rendered inside the cap
 * card(s) to save vertical space. Mirrors the {@link DocHelp} toggle pattern;
 * default-collapsed so it costs no height until the user opens it.
 */
function CapReachedHelp() {
  const { t } = useTranslation();
  const [open, setOpen] = useState(false);
  return (
    <div className="payg-help">
      <Button
        variant="tertiary"
        className="payg-help__toggle"
        aria-expanded={open}
        onClick={() => setOpen((o) => !o)}
        leftSection={<Icon name="circle-question-mark" size={15} />}
      >
        {t("payg.gates.title", "What happens when the cap is reached")}
        <Icon name="chevron-down" size={16} className="payg-help__chevron" />
      </Button>
      {open && (
        <div className="payg-help__panel">
          <div className="payg-gates">
            {GATE_CAP_BEHAVIOR.map(({ gate, staysAtCap }) => (
              <div className="payg-gate" data-enabled={staysAtCap} key={gate}>
                <span className="payg-gate__chip">
                  {staysAtCap ? (
                    <Icon name="check" size={18} />
                  ) : (
                    <Icon name="lock" size={16} />
                  )}
                </span>
                <span className="payg-gate__label">{gateLabel(gate, t)}</span>
                {!staysAtCap && (
                  <span className="payg-gate__tag" data-variant="pause">
                    {t("payg.gates.pauses", "pauses at cap")}
                  </span>
                )}
              </div>
            ))}
          </div>
        </div>
      )}
    </div>
  );
}

// ─── Per-member usage ────────────────────────────────────────────────────────

/**
 * Leader-only roster of each teammate's billable usage this period. Display-only — per-member
 * sub-cap enforcement isn't shipped, so there's no cap control here.
 */
function MemberUsage({ members }: { members: Wallet["members"] }) {
  const { t } = useTranslation();
  return (
    <div className="payg-card">
      <Stack gap="sm">
        <div>
          <div className="payg-card__title">
            {t("payg.members.title", "Team member usage")}
          </div>
          <div className="payg-card__subtitle">
            {t(
              "payg.members.subtitle",
              "Billable PDFs each teammate has processed this period.",
            )}
          </div>
        </div>
        <div>
          {members.map((m) => (
            <div className="payg-member" key={m.userId}>
              <span
                className={`payg-member__avatar ${privateContentClassName}`}
                style={{ background: avatarColor(m.userId) }}
              >
                {m.name.charAt(0).toUpperCase()}
              </span>
              <div
                className={privateContentClassName}
                style={{ flex: 1, minWidth: 0 }}
              >
                <div className="payg-member__name">{m.name}</div>
                <div className="payg-member__email">{m.email}</div>
              </div>
              <div className="payg-member__usage">
                <div className="payg-member__usage-num">
                  {m.spendUnits.toLocaleString()}{" "}
                  <Text span size="xs" c="dimmed">
                    {t("payg.members.docs", "PDFs")}
                  </Text>
                </div>
              </div>
            </div>
          ))}
        </div>
      </Stack>
    </div>
  );
}

// ─── Activity feed ──────────────────────────────────────────────────────────

/**
 * Feature flag — the activity feed is hidden until the meter-event surface is
 * built and polished (Wave 2). The backend returns {@code []} today, so an
 * unpolished "No billable activity yet" card adds nothing. Flip to {@code true}
 * once {@code wallet.recent} carries real rows. Kept as a flag (not deleted) so
 * the renderer below stays wired and ready.
 */
const SHOW_ACTIVITY_FEED = false;

/**
 * Renders {@code wallet.recent} — the backend returns {@code []} in V1 (the
 * meter-event surface ships in Wave 2), so today this shows a real empty
 * state. The row renderer is ready for when the rows arrive; fields are read
 * defensively because the activity-row shape isn't finalised yet (the Wallet
 * type carries {@code Record<string, unknown>} for the same reason).
 */
function ActivityFeed({ recent }: { recent: Wallet["recent"] }) {
  const { t } = useTranslation();
  return (
    <div className="payg-card">
      <Stack gap="sm">
        <div>
          <div className="payg-card__title">
            {t("payg.activity.title", "Recent billable activity")}
          </div>
          <div className="payg-card__subtitle">
            {t(
              "payg.activity.subtitle",
              "Only AI and automation draw from your budget. Everyday tools are free and aren't listed here.",
            )}
          </div>
        </div>
        {recent.length === 0 ? (
          <Text size="sm" c="dimmed">
            {t("payg.activity.empty", "No billable activity yet this period.")}
          </Text>
        ) : (
          <div>
            {recent.map((r, i) => (
              <div className="payg-activity-row" key={String(r.id ?? i)}>
                <span
                  className="payg-activity__dot"
                  data-kind={String(r.kind ?? "")}
                />
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div className="payg-activity__label">
                    {String(r.label ?? "")}
                  </div>
                  <div className="payg-activity__ts">{String(r.ts ?? "")}</div>
                </div>
                <span className="payg-activity__kind">
                  {String(r.kind ?? "")}
                </span>
                <span className="payg-activity__units">
                  {String(r.docUnits ?? 0)} {t("payg.activity.docs", "docs")}
                </span>
              </div>
            ))}
          </div>
        )}
      </Stack>
    </div>
  );
}

// ─── Stripe CTA ──────────────────────────────────────────────────────────────

function StripePortalLink({
  onOpenPortal,
}: {
  onOpenPortal: () => Promise<void>;
}) {
  const { t } = useTranslation();
  const [loading, setLoading] = useState(false);

  const handleClick = async () => {
    setLoading(true);
    try {
      await onOpenPortal();
    } catch (e: unknown) {
      // 503 = Supabase edge fn isn't configured (local dev without
      // PORTAL_NOT_CONFIGURED env). 404 = no Stripe customer yet (e.g. the
      // team was force-subscribed via dev hooks). Both are user-actionable
      // in roughly the same way ("try again later or contact support") so
      // we don't bother branching the copy.
      console.warn("[Payg] portal session failed", e);
      showToast({
        alertType: "warning",
        title: t(
          "payg.stripe.toast.unavailable.title",
          "Billing portal unavailable",
        ),
        body: t(
          "payg.stripe.toast.unavailable.body",
          "Billing portal isn't available right now. Try again in a moment.",
        ),
        location: "bottom-right",
      });
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="payg-stripe">
      <div>
        <div className="payg-stripe__title">
          {t("payg.stripe.title", "Manage billing in Stripe")}
        </div>
        <div className="payg-stripe__subtitle">
          {t(
            "payg.stripe.subtitle",
            "Receipts, invoices, payment method, billing currency.",
          )}
        </div>
      </div>
      <Button
        onClick={handleClick}
        loading={loading}
        rightSection={<Icon name="external-link" size={16} />}
        variant="secondary"
      >
        {t("payg.stripe.open", "Open billing portal")}
      </Button>
    </div>
  );
}

// ─── Prepaid bundle ───────────────────────────────────────────────────────

function daysUntil(iso: string | null): number | null {
  if (!iso) return null;
  const end = new Date(iso).getTime();
  if (Number.isNaN(end)) return null;
  return Math.ceil((end - Date.now()) / 86_400_000);
}

/**
 * Contextual nudge when prepaid capacity is running low, exhausted, or its term
 * is nearly up (the demo's "top up to keep processing" strip). Leader-only CTA —
 * members can't buy. Returns null when nothing needs saying.
 */
function PrepaidBanner({
  snap,
}: {
  snap: NonNullable<ReturnType<typeof prepaidSnapshotFromWallet>>;
}) {
  const { t } = useTranslation();
  const pctLeft = snap.total > 0 ? (snap.remaining / snap.total) * 100 : 0;
  const days = daysUntil(snap.expiresAt);
  const exhausted = snap.remaining <= 0;
  const low = !exhausted && pctLeft <= 20;
  const expiringSoon = days != null && days >= 0 && days <= 30;
  if (!exhausted && !low && !expiringSoon) return null;

  const message = exhausted
    ? t(
        "payg.prepaid.banner.exhausted",
        "Your prepaid capacity is used up — metered billing has resumed. Top up to keep the discount.",
      )
    : low
      ? t(
          "payg.prepaid.banner.low",
          "You're down to {{pct}}% of your prepaid capacity. Top up any time to keep processing uninterrupted.",
          { pct: Math.max(1, Math.round(pctLeft)) },
        )
      : t(
          "payg.prepaid.banner.expiring",
          "Your prepaid year ends in {{count}} days. Unused capacity expires — top up to start a fresh year.",
          // This arm only renders when expiringSoon (days is a valid number); ?? 0 satisfies the
          // count option's number type.
          { count: days ?? 0 },
        );

  return (
    <Alert color={exhausted ? "red" : "yellow"} variant="light">
      <span>{message}</span>
    </Alert>
  );
}

// ─── Main component ───────────────────────────────────────────────────────

const Payg: React.FC<PaygProps> = ({
  role,
  wallet,
  onSaveCap,
  onOpenPortal,
}) => {
  useRenderCount(role === "LEADER" ? "PaygLeader" : "PaygMember");
  const { t } = useTranslation();
  const isLeader = role === "LEADER";
  const prepaid = prepaidSnapshotFromWallet(wallet);
  // Read-only prepaid display. The buy/top-up flow lives on the portal Processor page (the
  // create-payg-bundle-checkout edge fn is driven from there); this editor Plan page only shows an
  // existing prepaid balance and is slated for removal on a separate branch.

  const fmt = (iso: string) =>
    new Date(iso).toLocaleDateString(undefined, {
      day: "numeric",
      month: "short",
    });

  return (
    <div className="payg">
      <Stack gap="md">
        {prepaid && <PrepaidBanner snap={prepaid} />}

        {/* The modal chrome already renders the section title ("Billing &
            usage"), so we lead with the descriptive subtitle + role pill. */}
        <div className="payg-planhead">
          <div className="payg-planhead__top">
            <span className="payg-planhead__eyebrow">
              {t(
                "payg.header.eyebrow",
                "Processor plan · {{start}} – {{end}}",
                {
                  start: fmt(wallet.billingPeriodStart),
                  end: fmt(wallet.billingPeriodEnd),
                },
              )}
            </span>
            <Group gap="xs" wrap="nowrap">
              {wallet.billingMode === "prepaid" && (
                <Badge color="blue" variant="light" radius="sm">
                  {t("payg.prepaid.chip", "Prepaid year")}
                </Badge>
              )}
              <span className="payg-role-pill" data-leader={isLeader}>
                {isLeader
                  ? t("payg.role.leader", "Team owner")
                  : t("payg.role.member", "Member")}
              </span>
            </Group>
          </div>

          <div className="payg-planhead__split">
            <div className="payg-planhead__col">
              <div className="payg-planhead__lbl payg-planhead__lbl--free">
                <Icon
                  name="infinity"
                  size={16}
                  className="payg-planhead__lbl-icon"
                />
                {t("payg.header.freeLabel", "Always free")}
              </div>
              <p className="payg-planhead__title">
                {t("payg.header.freeTitle", "Unlimited PDF editing")}
              </p>
              <p className="payg-planhead__body">
                {t(
                  "payg.header.freeBody",
                  "View, edit, merge, split, sign, watermark, compress, convert and manual OCR, as much as you want, no matter where you trigger it.",
                )}
              </p>
            </div>

            <div className="payg-planhead__col payg-planhead__col--meter">
              <div className="payg-planhead__lbl payg-planhead__lbl--meter">
                <Icon
                  name="zap"
                  size={16}
                  className="payg-planhead__lbl-icon"
                />
                {t("payg.header.meterLabel", "Metered")}
              </div>
              <p className="payg-planhead__title">
                {t("payg.header.meterTitle", "Automation · AI · API")}
              </p>
              <p className="payg-planhead__body">
                {t(
                  "payg.header.meterBody",
                  "{{limit}} free PDFs to start, then billed per PDF up to your cap.",
                  { limit: wallet.freeAllowance.toLocaleString() },
                )}
              </p>
            </div>
          </div>
        </div>

        <UsageHero wallet={wallet} />

        {prepaid && (
          <div className="payg-card">
            <Stack gap="sm">
              <div>
                <div className="payg-card__title">
                  {t("payg.prepaid.card.title", "Prepaid capacity")}
                </div>
                <div className="payg-card__subtitle">
                  {t(
                    "payg.prepaid.card.subtitle",
                    "Used before metered billing and outside your spend cap.",
                  )}
                </div>
              </div>
              <PrepaidCapacityMeterPanel snap={prepaid} />
            </Stack>
          </div>
        )}

        {isLeader ? (
          <CapEditor
            capUsd={wallet.capUsd}
            noCap={wallet.noCap}
            pricePerDocMinor={wallet.pricePerDocMinor}
            currency={wallet.currency}
            onSaveCap={onSaveCap}
          />
        ) : (
          <CapReadOnly capUsd={wallet.capUsd} noCap={wallet.noCap} />
        )}

        {isLeader && wallet.members.length > 0 && (
          <MemberUsage members={wallet.members} />
        )}

        {SHOW_ACTIVITY_FEED && <ActivityFeed recent={wallet.recent} />}

        {isLeader && onOpenPortal && (
          <StripePortalLink onOpenPortal={onOpenPortal} />
        )}
      </Stack>
    </div>
  );
};

export default Payg;

// Convenience exports for the config nav to render either variant directly.
export interface PaygLeaderProps {
  /** See {@link PaygProps#wallet}. */
  wallet: Wallet;
  /** See {@link PaygProps#onSaveCap}. */
  onSaveCap?: (capUsd: number | null) => Promise<void> | void;
  /** See {@link PaygProps#onOpenPortal}. */
  onOpenPortal?: () => Promise<void>;
}
export const PaygLeader: React.FC<PaygLeaderProps> = ({
  wallet,
  onSaveCap,
  onOpenPortal,
}) => (
  <Payg
    role="LEADER"
    wallet={wallet}
    onSaveCap={onSaveCap}
    onOpenPortal={onOpenPortal}
  />
);
export const PaygMember: React.FC<{ wallet: Wallet }> = ({ wallet }) => (
  <Payg role="MEMBER" wallet={wallet} />
);