/** Minimal OAuth types, ported from jawcode packages/ai/src/utils/oauth/types.ts. */
export type OAuthCredentialSource = "oauth" | "local-cli" | "credential-file" | "environment" | "manual";

/**
 * How the account authenticated. Mirrors `KiroAuthType` in `./kiro-credentials`, restated here so
 * the credential-store types do not depend on the SQLite import module.
 *
 * `aws_sso_oidc` covers AWS Builder ID, which never issues an account-scoped CodeWhisperer
 * profile ARN; the adapter needs that distinction to tell a Builder ID account apart from a
 * `kiro_desktop` account whose profile import merely failed.
 */
export type KiroCredentialAuthType = "kiro_desktop" | "aws_sso_oidc";

/** Account-scoped Kiro data required for refresh and request routing. */
export interface KiroOAuthMetadata {
  profileArn?: string;
  ssoRegion?: string;
  apiRegion?: string;
  clientId?: string;
  clientSecret?: string;
  /**
   * Non-secret routing signal. Derived from the presence of a device-registration client pair, so
   * it stays accurate even though `clientId`/`clientSecret` never leave the credential store.
   */
  authType?: KiroCredentialAuthType;
}

/**
 * Account-scoped Muse Code data that is NOT the request bearer.
 *
 * The Model API is authenticated by the `LLM|` key in `access`; this token authenticates
 * the Meta ACCOUNT and exists only to mint that key and to read subscription usage
 * (devlog/_fin/260912_muse_device_oauth/002 A). Keeping it out of `access` is what lets
 * every request path stay unchanged.
 *
 * It must never be added to `OAuthAccountSummary` (src/oauth/index.ts:1803) or to
 * `OAuthAccessSnapshot` (src/oauth/index.ts:85-100). Both are hand-built allowlists, and
 * that construction — not a redactor — is what keeps a secret out of a response.
 */
export interface MuseOAuthMetadata {
  /** Meta account access token from the device grant. Never sent to api.meta.ai/v1. */
  oauthAccessToken: string;
  /**
   * The stable Meta account id. Kept HERE rather than in `accountId` on purpose (wp2
   * audit fold W2): the store keys a slot on `accountId ?? email`, and this provider
   * import path has always supplied email only. Promoting `user_id` to `accountId` would
   * make a device login fail to match the row an imported login already created, giving
   * one human two accounts.
   */
  userId?: string;
  /** Epoch ms of the mint that produced the stored key. */
  mintedAt?: number;
  /** Subscription tier label as Meta reported it. Display only. */
  tierName?: string;
}

export interface WorkBuddyOAuthMetadata {
  uid: string;
  domain: string;
  enterpriseId?: string;
  refreshExpiresAt?: number;
}

/** Private web-session authority for the account-specific M365 chat model picker. */
export interface M365CopilotWebSession { cookie: string; accountId: string }

export type OAuthCredentials = {
  /** Kept only in the protected credential store and private catalog auth captures. */
  m365?: M365CopilotWebSession;
  /** Account-scoped WorkBuddy identity, kept inside the protected credential store. */
  workbuddy?: WorkBuddyOAuthMetadata;
  refresh: string;
  access: string;
  /** Epoch ms after any small provider-specific early-refresh margin; the shared gate adds 1 minute. */
  expires: number;
  email?: string;
  accountId?: string;
  source?: OAuthCredentialSource;
  /** Validated upstream subscription label; null is an observed unknown, absent is not read. */
  plan?: string | null;
  /**
   * Display-only name of the organization the grant serves (Anthropic), read with the plan from
   * the same bearer's profile; null is an observed unknown, absent is not read. Never identity.
   */
  organizationName?: string | null;
  /**
   * Private authenticated account proof; never projected into account summaries. The optional
   * organization is the grant's scope, observed from the same bearer; it never proves identity.
   */
  anthropicIdentity?: { v: 1; accountUuid: string; bearerSha256: string; organizationUuid?: string };
  /** Private exchange-attested scope for a bearer whose account UUID could not be proven. */
  anthropicOrganizationScope?: { v: 1; organizationUuid: string; bearerSha256: string };
  /** Google Antigravity (Cloud Code Assist) discovered project id; injected into the CCA envelope. */
  projectId?: string;
  /**
   * GitHub Copilot allowlisted API origin from token `endpoints.api` (HTTPS `*.githubcopilot.com` only).
   * Never reuse for Antigravity projectId; validated on write and again at request time.
   */
  apiBaseUrl?: string;
  /** Never returned by management APIs; persisted only inside the protected auth-store boundary. */
  kiro?: KiroOAuthMetadata;
  /** Never returned by management APIs; persisted only inside the protected auth-store boundary. */
  muse?: MuseOAuthMetadata;
};

/** One logged-in account inside a provider's account set (multiauth). */
export interface ProviderAccount {
  /** Stable short id, generated once at append time; never re-derived after rotation. */
  id: string;
  /** Rotated on each explicit login and retained across token refreshes. */
  loginId?: string;
  /** Anthropic credential replacement sequence and unique revision identity. */
  credentialRevision?: { sequence: number; id: string };
  /** Native Kiro device accounts cannot use the kiro-cli reauth path. */
  loginOrigin?: "kiro-device";
  /** User-owned display label; never participates in auth identity or routing. */
  alias?: string;
  credential: OAuthCredentials;
  /** Terminal refresh failure (invalid_grant / reused / revoked) — re-login required. */
  needsReauth?: boolean;
  /** Kiro SSO refresh input-error evidence; status-only attention after this generation expires. */
  refreshAttentionGeneration?: string;
  /** Operator exclusion from generic OAuth account selection until explicitly resumed. */
  paused?: boolean;
  pauseDetails?: import("../types/account-auto-pause").AccountPauseDetails;
  /** What convicted an automatic pause; a sibling of `pauseDetails`, never present without it. */
  pauseEvidence?: import("../types/account-auto-pause").AccountPauseEvidence;
  pauseRevision?: string;
  /** Anthropic-only usage-switch override; absent inherits its pool default, zero disables it. */
  autoSwitchThresholdOverride?: number;
  /**
   * Why the account needs reauthentication. `verify_account` means the grant is
   * alive but the provider blocks the account until the human verifies it
   * (Antigravity 403 PERMISSION_DENIED) — a plain re-login without that
   * verification will not help. Absent means an ordinary credential failure.
   */
  needsReauthReason?: "verify_account";
  /**
   * Why a `verify_account` hold was placed, when it is narrower than "verify". `tos_violation`:
   * Google suspended the account. Kept beside the hold, not instead of it, so a build that does
   * not know the cause still keeps the account out of the pool.
   */
  needsReauthCause?: "tos_violation";
  addedAt?: number;
}

/** auth.json value per provider: N accounts + which one requests use. */
export interface ProviderAccountSet {
  activeAccountId: string;
  /** Opaque selection generation; absent in legacy stores, independent of token refresh. */
  selectionRevision?: string;
  accounts: ProviderAccount[];
}

/** Non-secret snapshot used to condition a selection on the choice that started a request. */
export interface OAuthAccountSelection {
  accountId: string;
  revision?: string;
}

export interface OAuthController {
  onAuth?(info: { url: string; instructions?: string; deviceCode?: string }): void;
  onProgress?(message: string): void;
  onManualCodeInput?(expectedState?: string): Promise<string>;
  signal?: AbortSignal;
}

/**
 * How a login flow may use a locally detected CLI token.
 * "off" goes straight to the real OAuth flow, "fallback" imports a local token when present
 * and falls back to OAuth otherwise, "only" imports without any OAuth fallback.
 */
export type LocalTokenImportMode = "off" | "fallback" | "only";
