/**
 * Provider quota shapes, split out of `quota.ts` so a provider-specific quota module can
 * describe its result without importing the aggregator that will consume it.
 *
 * `quota.ts` imports the Kiro usage module for its fetcher; if that module reached back
 * into `quota.ts` for these types the two would depend on each other. Types have no
 * runtime edge, but a cycle that exists only in the type graph is still a cycle, and it
 * blocks any later attempt to load one side without the other.
 */

export const PROVIDER_QUOTA_MAX_AGE_MS = 30 * 60_000;

/** Management-only eligibility evidence; private credential binding never leaves the server. */
export type ProviderRoutingQuota =
  | { state: "unknown" }
  | { state: "available" | "exhausted"; updatedAt: number; validUntil: number };

export interface ProviderQuotaWindow {
  label: string;
  percent: number;
  resetAt?: number;
  /**
   * Set only when the PRODUCER proved this window covers one model family, structurally rather
   * than by reading its label: an Anthropic `seven_day_<family>` body key, or a limit with
   * `kind: "weekly_scoped"` and a recognized `scope.model.display_name`.
   *
   * Absent means provider-wide, which gates every model. That is the fail-closed direction and
   * the behaviour every other producer keeps. Routing must key on THIS, never on the label text:
   * `quota/antigravity.ts` passes an upstream `group.displayName` straight through, so a
   * provider-wide group named "Opus" there would otherwise be mistaken for a per-model window.
   */
  scope?: "model";
}

export interface ProviderQuotaCreditsUsd {
  used: number;
  limit: number;
  remaining: number;
  percent: number;
  expiresAt?: number;
  unlimited?: boolean;
}

export interface ProviderQuota {
  fiveHourPercent?: number;
  fiveHourResetAt?: number;
  weeklyPercent?: number;
  weeklyResetAt?: number;
  monthlyPercent?: number;
  monthlyResetAt?: number;
  /** Observed Kiro plan credits, independent of token and currency estimates. */
  kiroCreditsUsed?: number;
  kiroCreditsLimit?: number;
  customWindows?: ProviderQuotaWindow[];
  creditsUsd?: ProviderQuotaCreditsUsd;
  updatedAt: number;
}

export type AccountQuotaMode = "probe" | "passive" | "unsupported";

/** Additive management-row fields; cheap lists emit only quotaMode. */
export interface AccountQuotaFields {
  quotaMode?: AccountQuotaMode;
  quota?: ProviderQuota | null;
  quotaUnavailable?: boolean;
  quotaFailure?: QuotaFailureCode;
}


/** Closed account-probe diagnoses; never upstream text, URLs, credentials or routing policy. */
export const QUOTA_FAILURE_CODES = [
  "account_unavailable", "access_denied", "rate_limited", "upstream_error", "redirect_blocked",
  "destination_blocked", "dns_failed", "timeout", "transport_error", "response_unusable",
] as const;
export type QuotaFailureCode = typeof QUOTA_FAILURE_CODES[number];
export function parseQuotaFailureCode(value: unknown): QuotaFailureCode | undefined {
  return QUOTA_FAILURE_CODES.find(code => code === value);
}
