import { antigravityCliUserAgent, antigravityUserAgent } from "../../adapters/client-fingerprint";
import { DestinationDnsResolutionError } from "../../lib/destination-policy";
import { PinnedHttpError, type PinnedHttpErrorCode } from "../../lib/pinned-http";
import { ProviderOutboundPolicyError, providerOutboundPost, providerRedirectError, type ProviderOutboundDependencies } from "../../lib/provider-outbound";
import { readBoundedResponseBytes } from "../../lib/bounded-body";
import { getValidAccessToken } from "../../oauth";
import { getAccountCredential, getAccountSet, getCredential } from "../../oauth/store";
import { asRecord, normalizePercent, normalizeResetAt, QUOTA_RESPONSE_MAX_BYTES, readQuotaJson, REQUEST_TIMEOUT_MS, toFiniteNumber } from "../quota-wire";
import { antigravityEvidence, report, type ProviderQuotaReport } from "./report-cache";
import { captureAntigravityQuotaReceipt, decisionQuotaSinkActive } from "../../lib/decision-quota-sink";
import { antigravityDecisionWindows } from "../quota-decision-publication";
import { antigravityQuotaCredentialGeneration, isAntigravityArgonIdentity } from "../antigravity-quota-identity";
import type { OcxProviderConfig } from "../../types";
import { quotaCredentialIdentity } from "./account-cache";
import type { ProviderQuota, ProviderQuotaWindow, QuotaFailureCode } from "../quota-types";
import { httpQuotaFailure } from "./failure-codes";

export function antigravityQuotaDiagnosticIdentity(accountId: string, credential = getAccountCredential("google-antigravity", accountId)): string | undefined {
  return credential ? quotaCredentialIdentity("google-antigravity", accountId, credential, {
    adapter: "google", baseUrl: ANTIGRAVITY_ACCOUNT_QUOTA_BASE, authMode: "oauth",
  }) : undefined;
}


function quotaInfoEntries(modelInfo: Record<string, unknown>): Record<string, unknown>[] {
  const entries: Record<string, unknown>[] = [];
  const add = (value: unknown, tier?: string) => {
    const rec = asRecord(value);
    if (!rec) return;
    entries.push(tier ? { ...rec, tier } : rec);
  };
  const addArray = (value: unknown) => {
    if (!Array.isArray(value)) return;
    for (const entry of value) add(entry);
  };

  if (Array.isArray(modelInfo.quotaInfo)) addArray(modelInfo.quotaInfo);
  else add(modelInfo.quotaInfo);
  addArray(modelInfo.quotaInfos);

  const byTier = asRecord(modelInfo.quotaInfoByTier);
  if (byTier) {
    for (const [tier, value] of Object.entries(byTier)) {
      if (Array.isArray(value)) {
        for (const entry of value) add(entry, tier);
      } else {
        add(value, tier);
      }
    }
  }
  return entries;
}

const GEMINI_TOKEN = /(?:^|[^a-z0-9])gemini(?:[^a-z0-9]|$)/i;
const CLAUDE_FAMILY_TOKEN = /(?:^|[^a-z0-9])(?:claude|3p|gpt|opus|sonnet)(?:[^a-z0-9]|$)/i;
/** Canonical model of the Argon summary pool; tagging keeps the window out of routing. */
const ARGON_POOL_MODEL_ID = "gemini-4-argon";

type AntigravitySummaryFamily = "argon" | "gem" | "cla";

/** A bucket's own id and name decide its family when they name one; the group decides otherwise. */
function antigravitySummaryBucketFamily(bucket: Record<string, unknown>): AntigravitySummaryFamily | undefined {
  const identity = `${typeof bucket.bucketId === "string" ? bucket.bucketId : ""} ${typeof bucket.displayName === "string" ? bucket.displayName : ""}`;
  if (isAntigravityArgonIdentity(identity)) return "argon";
  if (GEMINI_TOKEN.test(identity)) return "gem";
  if (CLAUDE_FAMILY_TOKEN.test(identity)) return "cla";
  return undefined;
}

function classifyAntigravityFamily(modelId: string, modelInfo: Record<string, unknown>, quotaInfo: Record<string, unknown>): "Gem" | "Cla" | null {
  const displayName = typeof modelInfo.displayName === "string" ? modelInfo.displayName : "";
  // Argon models keep their exact model rows but never seed the shared Gem family bar.
  if (isAntigravityArgonIdentity(`${modelId} ${displayName}`)) return null;
  const tier = typeof quotaInfo.tier === "string" ? quotaInfo.tier : "";
  const haystack = `${modelId} ${displayName} ${tier}`.toLowerCase();
  if (haystack.includes("gemini")) return "Gem";
  if (haystack.includes("claude") || haystack.includes("opus") || haystack.includes("sonnet") || haystack.includes("gpt-oss") || haystack.includes("gpt_oss")) return "Cla";
  return null;
}

function antigravityUsedPercent(quotaInfo: Record<string, unknown>): number | undefined {
  const target = asRecord(quotaInfo.remaining) ?? quotaInfo;
  const remaining = normalizePercent(toFiniteNumber(target.remainingFraction) !== undefined
    ? toFiniteNumber(target.remainingFraction)! * 100
    : toFiniteNumber(target.remainingPercentage) !== undefined
      ? toFiniteNumber(target.remainingPercentage)! * 100
      : undefined);
  if (remaining === undefined) return undefined;
  return normalizePercent(100 - remaining);
}

/** Gem/Cla windows from a `fetchAvailableModels` body; shared by the provider and account probes. */
function antigravityWindowsFromModels(body: Record<string, unknown> | null): ProviderQuotaWindow[] {
  const models = asRecord(body?.models);
  if (!models) return [];

  const windows = new Map<string, ProviderQuotaWindow>();
  const modelWindows: ProviderQuotaWindow[] = [];
  const labels = new Set(["Gem", "Cla"]);
  for (const [modelId, rawModelInfo] of Object.entries(models)) {
    const modelInfo = asRecord(rawModelInfo);
    if (!modelInfo) continue;
    for (const quotaInfo of quotaInfoEntries(modelInfo)) {
      const label = classifyAntigravityFamily(modelId, modelInfo, quotaInfo);
      const percent = antigravityUsedPercent(quotaInfo);
      if (percent === undefined) continue;
      const resetAt = normalizeResetAt(quotaInfo.resetTime);
      if (label && !windows.has(label)) {
        windows.set(label, { label, percent, ...(resetAt !== undefined ? { resetAt } : {}) });
      }
      // Keep model measurements as display/diagnostic evidence. They are deliberately
      // separate from the family summaries above; routing must not infer eligibility
      // from one model's counter or from a missing row.
      if (modelId.trim() && modelId.length <= 128 && modelWindows.length < 256) {
        const tier = typeof quotaInfo.tier === "string" && quotaInfo.tier.length <= 64
          ? quotaInfo.tier.trim().slice(0, Math.max(0, 125 - modelId.length))
          : "";
        const baseLabel = `${modelId}${tier ? ` · ${tier}` : ""}`;
        let modelLabel = baseLabel;
        for (let ordinal = 2; labels.has(modelLabel); ordinal += 1) {
          const suffix = ` #${ordinal}`;
          modelLabel = `${baseLabel.slice(0, 128 - suffix.length)}${suffix}`;
        }
        labels.add(modelLabel);
        modelWindows.push({
          label: modelLabel,
          modelId,
          percent,
          ...(resetAt !== undefined ? { resetAt } : {}),
        });
      }
    }
  }

  const customWindows = ["Gem", "Cla"].flatMap(label => {
    const window = windows.get(label);
    return window ? [window] : [];
  });
  return [...customWindows, ...modelWindows];
}

/**
 * Parse Google Antigravity quota from `v1internal:retrieveUserQuotaSummary`.
 * Groups contain Gemini models and Claude/3P models, each with 5h and weekly limit buckets.
 */
function parseAntigravityQuotaSummary(body: Record<string, unknown> | null, knownFamiliesOnly = false): ProviderQuota | null {
  const groups = Array.isArray(body?.groups) ? (body.groups as unknown[]) : [];
  if (groups.length === 0) return null;

  const customWindowsMap = new Map<string, ProviderQuotaWindow>();

  for (const rawGroup of groups) {
    const group = asRecord(rawGroup);
    if (!group) continue;
    const groupDisplayName = typeof group.displayName === "string" ? group.displayName : "";
    const groupName = `${groupDisplayName} ${typeof group.description === "string" ? group.description : ""}`.toLowerCase();
    // A description that merely mentions Argon must not move a whole Gemini group.
    const groupFamily: AntigravitySummaryFamily | undefined = isAntigravityArgonIdentity(groupDisplayName) ? "argon"
      : groupName.includes("gemini") ? "gem"
        : groupName.includes("claude") || groupName.includes("3p") || groupName.includes("gpt") ? "cla"
          : undefined;

    const buckets = Array.isArray(group.buckets) ? (group.buckets as unknown[]) : [];
    for (const rawBucket of buckets) {
      const bucket = asRecord(rawBucket);
      if (!bucket) continue;
      const windowStr = `${typeof bucket.window === "string" ? bucket.window : ""} ${typeof bucket.bucketId === "string" ? bucket.bucketId : ""} ${typeof bucket.displayName === "string" ? bucket.displayName : ""}`.toLowerCase();
      const percent = antigravityUsedPercent(bucket);
      if (percent === undefined) continue;
      const resetAt = normalizeResetAt(bucket.resetTime);

      const isWeekly = windowStr.includes("week");
      const is5h = windowStr.includes("5h") || windowStr.includes("five");
      const family = antigravitySummaryBucketFamily(bucket) ?? groupFamily;

      if (family === "argon") {
        const label = is5h ? "Argon" : isWeekly ? "Argon (Weekly)" : "";
        if (label && !customWindowsMap.has(label)) {
          customWindowsMap.set(label, { label, modelId: ARGON_POOL_MODEL_ID, percent, ...(resetAt !== undefined ? { resetAt } : {}) });
        }
      } else if (family === "gem") {
        const label = is5h ? "Gem" : isWeekly ? "Gem (Weekly)" : "";
        if (label && !customWindowsMap.has(label)) {
          customWindowsMap.set(label, { label, percent, ...(resetAt !== undefined ? { resetAt } : {}) });
        }
      } else if (family === "cla") {
        const label = is5h ? "Cla" : isWeekly ? "Cla (Weekly)" : "";
        if (label && !customWindowsMap.has(label)) {
          customWindowsMap.set(label, { label, percent, ...(resetAt !== undefined ? { resetAt } : {}) });
        }
      } else if (!knownFamiliesOnly) {
        const baseLabel = typeof group.displayName === "string" ? group.displayName : "Other";
        const label = isWeekly ? `${baseLabel} (Weekly)` : baseLabel;
        if (!customWindowsMap.has(label)) {
          customWindowsMap.set(label, { label, percent, ...(resetAt !== undefined ? { resetAt } : {}) });
        }
      }
    }
  }

  const PREFERRED_ORDER = ["Gem", "Gem (Weekly)", "Cla", "Cla (Weekly)", "Argon", "Argon (Weekly)"];
  const customWindows = Array.from(customWindowsMap.values()).sort((a, b) => {
    const ia = PREFERRED_ORDER.indexOf(a.label);
    const ib = PREFERRED_ORDER.indexOf(b.label);
    if (ia !== -1 && ib !== -1) return ia - ib;
    if (ia !== -1) return -1;
    if (ib !== -1) return 1;
    return a.label.localeCompare(b.label);
  });

  if (customWindows.length === 0) {
    return null;
  }

  return {
    customWindows,
    updatedAt: Date.now(),
  };
}

const ANTIGRAVITY_ACCOUNT_QUOTA_BASE = "https://daily-cloudcode-pa.googleapis.com";
const ANTIGRAVITY_QUOTA_SUMMARY_URL = `${ANTIGRAVITY_ACCOUNT_QUOTA_BASE}/v1internal:retrieveUserQuotaSummary`;
const ANTIGRAVITY_QUOTA_MODELS_URL = `${ANTIGRAVITY_ACCOUNT_QUOTA_BASE}/v1internal:fetchAvailableModels`;

/** Only these fixed accounting destinations may use transparent Fake-IP DNS. */
export function isCanonicalAntigravityQuotaUrl(name: string, url: string): boolean {
  return name === "google-antigravity"
    && (url === ANTIGRAVITY_QUOTA_SUMMARY_URL || url === ANTIGRAVITY_QUOTA_MODELS_URL);
}

let antigravityOutboundDependencies: ProviderOutboundDependencies = {
  isCanonicalUrl: isCanonicalAntigravityQuotaUrl,
};

/** Test seam: inject resolver/pinned transport for provider and per-account probes. */
export function setAntigravityAccountQuotaTransportForTests(dependencies: ProviderOutboundDependencies | null): void {
  antigravityOutboundDependencies = { ...dependencies, isCanonicalUrl: isCanonicalAntigravityQuotaUrl };
}

/**
 * Per-account Antigravity quota (#1082). Always probes Google's own Cloud Code Assist host
 * through the pinned provider-outbound transport: a configured `baseUrl` is a routing choice
 * for requests, not a second source of Google's accounting for a stored credential, and fixing
 * the destination keeps the `provider\0accountId` cache identity exact across config changes.
 * A redirect or non-2xx yields null (unavailable), never a partial row.
 */
type AntigravityQuotaProbeResult =
  | { kind: "available"; quota: ProviderQuota; source: "google-antigravity:retrieveUserQuotaSummary" | "google-antigravity:fetchAvailableModels" }
  | { kind: "unavailable"; failure: QuotaFailureCode; legacy: { kind: "null" } | { kind: "throw"; error: unknown } };

/**
 * Every pinned-transport failure code, classified once.
 *
 * A conditional that named one code and sent the rest to `timeout` was correct only for as long
 * as the union held exactly the codes it was written against. When the transport learned to
 * report a coding it cannot undo, that answer was reported as a timeout, which is a different
 * operational story entirely. A total map makes a new code a compile error here rather than a
 * quiet misdiagnosis.
 */
const PINNED_QUOTA_FAILURES = {
  connect_timeout: "timeout",
  first_byte_timeout: "timeout",
  inactivity_timeout: "timeout",
  // The response arrived and cannot be used: too large, coded in a format this transport cannot
  // undo, or coded bytes that did not decode. None of these is a timing failure.
  output_byte_limit: "response_unusable",
  unsupported_content_encoding: "response_unusable",
  content_decode_failed: "response_unusable",
} satisfies Record<PinnedHttpErrorCode, QuotaFailureCode>;

function quotaTransportFailure(error: unknown): QuotaFailureCode {
  if (error instanceof ProviderOutboundPolicyError) return "destination_blocked";
  if (error instanceof DestinationDnsResolutionError) return "dns_failed";
  if (error instanceof PinnedHttpError) return PINNED_QUOTA_FAILURES[error.code];
  if (error instanceof DOMException && error.name === "TimeoutError") return "timeout";
  return "transport_error";
}

function unavailableAntigravityQuota(failure: QuotaFailureCode): AntigravityQuotaProbeResult {
  return { kind: "unavailable", failure, legacy: { kind: "null" } };
}

/**
 * Prefer a summary network-policy diagnosis over a vaguer fallback. A blocked
 * destination is an actionable local-network fact, while "upstream_error" tells
 * the operator to go look at Google. A successful models probe still clears
 * the first failure completely.
 */
function antigravityUnavailableFailure(
  summaryFailure: QuotaFailureCode | undefined,
  fallbackFailure: QuotaFailureCode,
): QuotaFailureCode {
  if (
    (summaryFailure === "destination_blocked" || summaryFailure === "dns_failed")
    && fallbackFailure !== "destination_blocked"
    && fallbackFailure !== "dns_failed"
  ) {
    return summaryFailure;
  }
  return fallbackFailure;
}

export async function probeAntigravityUsageQuota(accessToken: string, projectId: string, signal?: AbortSignal): Promise<AntigravityQuotaProbeResult> {
  // An optional caller signal (account import) cancels every request of this probe, including the
  // 403 retry and the models fallback, alongside the per-request deadline.
  const requestSignal = () => signal ? AbortSignal.any([signal, AbortSignal.timeout(REQUEST_TIMEOUT_MS)]) : AbortSignal.timeout(REQUEST_TIMEOUT_MS);
  const fetchQuota = (url: string, userAgent: string) => providerOutboundPost("google-antigravity", { baseUrl: ANTIGRAVITY_ACCOUNT_QUOTA_BASE }, url, {
    headers: {
      Accept: "application/json", "Content-Type": "application/json",
      "User-Agent": userAgent, Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify({ project: projectId }), signal: requestSignal(),
  }, antigravityOutboundDependencies);
  let summaryFailure: QuotaFailureCode | undefined;
  try {
    // The summary includes the Gemini 4 Argon group only for the CLI client family; under the
    // IDE UA the same account reports just the Gemini and Claude groups, so Argon usage is lost.
    let response = await fetchQuota(ANTIGRAVITY_QUOTA_SUMMARY_URL, antigravityCliUserAgent());
    if (response.status === 403) {
      // Some valid accounts reject the IDE fingerprint only for quota accounting.
      try { await response.body?.cancel(); } catch { /* Best-effort release before retry. */ }
      response = await fetchQuota(ANTIGRAVITY_QUOTA_SUMMARY_URL, "antigravity/1.0");
    }
    if (await providerRedirectError(response, ANTIGRAVITY_QUOTA_SUMMARY_URL)) return unavailableAntigravityQuota("redirect_blocked");
    if (response.status === 401 || response.status === 403) return unavailableAntigravityQuota("access_denied");
    if (response.ok) {
      const quota = parseAntigravityQuotaSummary(asRecord(await readQuotaJson(response)));
      if (quota) return { kind: "available", quota, source: "google-antigravity:retrieveUserQuotaSummary" };
    }
  } catch (error) {
    // Existing behavior: summary transport/parse failure may recover through the models probe.
    summaryFailure = quotaTransportFailure(error);
  }
  if (signal?.aborted) return unavailableAntigravityQuota(summaryFailure ?? "transport_error");
  try {
    const response = await fetchQuota(ANTIGRAVITY_QUOTA_MODELS_URL, antigravityUserAgent());
    if (await providerRedirectError(response, ANTIGRAVITY_QUOTA_MODELS_URL)) {
      return unavailableAntigravityQuota(antigravityUnavailableFailure(summaryFailure, "redirect_blocked"));
    }
    if (!response.ok) {
      return unavailableAntigravityQuota(antigravityUnavailableFailure(summaryFailure, httpQuotaFailure(response.status)));
    }
    const customWindows = antigravityWindowsFromModels(asRecord(await readQuotaJson(response)));
    if (!customWindows.length) {
      return unavailableAntigravityQuota(antigravityUnavailableFailure(summaryFailure, "response_unusable"));
    }
    return { kind: "available", quota: { customWindows, updatedAt: Date.now() }, source: "google-antigravity:fetchAvailableModels" };
  } catch (error) {
    // The public compatibility wrapper still rejects this exact fallback error; it never enters a DTO.
    return {
      kind: "unavailable",
      failure: antigravityUnavailableFailure(summaryFailure, quotaTransportFailure(error)),
      legacy: { kind: "throw", error },
    };
  }
}

/** Raw outcome of one summary request: the HTTP evidence, not a quota-poller verdict. */
export type AntigravitySummaryOnceResult =
  | { kind: "response"; status: number; bodyText: string; quota: ProviderQuota | null }
  | { kind: "redirect" }
  | { kind: "transport"; failure: QuotaFailureCode };

const SUMMARY_ERROR_BODY_MAX_BYTES = 8 * 1024;

/**
 * Exactly one read-only `retrieveUserQuotaSummary` request under the CLI fingerprint, for the
 * per-account probe. Unlike {@link probeAntigravityUsageQuota} there is no 403 retry under the IDE
 * UA and no models fallback: the operator asked what Google answers this account, and the first
 * answer is that evidence. The caller owns the token; nothing here refreshes or stores it.
 */
export async function fetchAntigravityQuotaSummaryOnce(
  accessToken: string,
  projectId: string,
  parentSignal: AbortSignal | undefined,
  beforeSend: () => boolean,
): Promise<AntigravitySummaryOnceResult> {
  const deadline = AbortSignal.timeout(REQUEST_TIMEOUT_MS);
  const signal = parentSignal ? AbortSignal.any([parentSignal, deadline]) : deadline;
  const interrupted = Promise.withResolvers<never>();
  const onAbort = () => interrupted.reject(signal.reason);
  signal.addEventListener("abort", onAbort, { once: true });
  try {
    signal.throwIfAborted();
    const request = providerOutboundPost("google-antigravity", { baseUrl: ANTIGRAVITY_ACCOUNT_QUOTA_BASE }, ANTIGRAVITY_QUOTA_SUMMARY_URL, {
      headers: {
        Accept: "application/json", "Content-Type": "application/json",
        "User-Agent": antigravityCliUserAgent(), Authorization: `Bearer ${accessToken}`,
      },
      body: JSON.stringify({ project: projectId }),
      signal,
    }, {
      ...antigravityOutboundDependencies,
      maxPinnedErrorBytes: SUMMARY_ERROR_BODY_MAX_BYTES,
      beforeSend: () => {
        // System DNS can finish after the deadline. Fence the eventual dispatch too.
        signal.throwIfAborted();
        return beforeSend();
      },
    });
    const response = await Promise.race([request, interrupted.promise]);
    if (response.status >= 300 && response.status < 400) {
      void response.body?.cancel().catch(() => undefined);
      return { kind: "redirect" };
    }
    const maxBytes = response.ok ? QUOTA_RESPONSE_MAX_BYTES : SUMMARY_ERROR_BODY_MAX_BYTES;
    const body = await readBoundedResponseBytes(response, { maxBytes, signal });
    if (body.oversized) return { kind: "transport", failure: "response_unusable" };
    const bodyText = new TextDecoder().decode(body.bytes);
    if (response.ok) {
      let decoded: unknown;
      try { decoded = JSON.parse(bodyText); }
      catch { return { kind: "transport", failure: "response_unusable" }; }
      const quota = parseAntigravityQuotaSummary(asRecord(decoded), true);
      return { kind: "response", status: response.status, bodyText: "", quota };
    }
    return { kind: "response", status: response.status, bodyText, quota: null };
  } catch (error) {
    return { kind: "transport", failure: quotaTransportFailure(error) };
  } finally {
    signal.removeEventListener("abort", onAbort);
  }
}

export async function fetchAntigravityUsageQuota(accessToken: string, projectId: string): Promise<ProviderQuota | null> {
  const result = await probeAntigravityUsageQuota(accessToken, projectId);
  if (result.kind === "available") return result.quota;
  if (result.legacy.kind === "throw") throw result.legacy.error;
  return null;
}

/** Every store read uses `provider`, the same key inference resolves, so a row never probes another row's account. */
export async function fetchAntigravityQuota(provider: string, config: OcxProviderConfig): Promise<ProviderQuotaReport | null> {
  const credential = getCredential(provider);
  if (!credential?.projectId) return null;
  let accessToken: string;
  try { accessToken = await getValidAccessToken(provider); } catch { return null; }
  const set = decisionQuotaSinkActive() ? getAccountSet(provider) : undefined;
  const account = set?.accounts.find(row => row.id === set.activeAccountId);
  const probed = account?.credential.access === accessToken && account.credential.projectId === credential.projectId ? account : undefined;
  const receipt = probed ? captureAntigravityQuotaReceipt(provider, config, probed.id, antigravityQuotaCredentialGeneration(probed.credential)) : undefined;
  const result = await probeAntigravityUsageQuota(accessToken, credential.projectId);
  if (result.kind === "available") {
    const value = report(provider, result.source, result.quota);
    // Only the summary endpoint reports real family buckets. The models fallback labels one model's
    // counter Gem/Cla, so it stays display-only: no windows, and retained summary clocks survive.
    const windows = result.source === "google-antigravity:retrieveUserQuotaSummary" ? antigravityDecisionWindows(result.quota) : [];
    if (value && receipt) antigravityEvidence.set(value, { receipt, windows: Object.freeze(windows.map(row => Object.freeze(row))) });
    return value;
  }
  if (result.legacy.kind === "throw") throw result.legacy.error;
  return null;
}
