"use server";

import { getActiveOrgId } from "@/lib/active-org";
import { getOrgQuotaLimits } from "./quotas";
import { countOrgKeywords } from "./usage";

/** Keyword quota for the active org (used vs limit) — feeds the sidebar bar.
 *  Pure read (no revalidation) so it's safe to call on mount. limit -1 =
 *  unlimited. Returns null when there's no active org. */
export async function getKeywordQuotaAction(): Promise<{
  used: number;
  limit: number;
} | null> {
  let orgId: string;
  try {
    orgId = await getActiveOrgId();
  } catch {
    return null;
  }
  const [used, limits] = await Promise.all([
    countOrgKeywords(orgId),
    getOrgQuotaLimits(orgId),
  ]);
  return { used, limit: limits.keywords };
}
