/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import { safeParse } from "../lib/schemas.js";
import { ClosedEnum } from "../types/enums.js";
import { Result as SafeParseResult } from "../types/fp.js";
import * as types from "../types/primitives.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

export const AuthTokenScopesOrigin = {
  App: "app",
  Apple: "apple",
  Bitbucket: "bitbucket",
  Chatgpt: "chatgpt",
  Email: "email",
  Emu: "emu",
  Github: "github",
  GithubWebhook: "github-webhook",
  Gitlab: "gitlab",
  Google: "google",
  Invite: "invite",
  Manual: "manual",
  Otp: "otp",
  Passkey: "passkey",
  Saml: "saml",
  Sms: "sms",
  TokenExchangeOidc: "token-exchange-oidc",
} as const;
export type AuthTokenScopesOrigin = ClosedEnum<typeof AuthTokenScopesOrigin>;

/**
 * The access scopes granted to the token.
 */
export type Scopes2 = {
  type: "team";
  teamId: string;
  origin?: AuthTokenScopesOrigin | undefined;
  createdAt: number;
  expiresAt?: number | undefined;
};

/**
 * Possible step-up auth origins
 */
export const AuthTokenScopes1Origin = {
  EmailOtp: "email-otp",
  Otp: "otp",
  RecoveryCode: "recovery-code",
  Totp: "totp",
  Webauthn: "webauthn",
} as const;
/**
 * Possible step-up auth origins
 */
export type AuthTokenScopes1Origin = ClosedEnum<typeof AuthTokenScopes1Origin>;

export type Sudo = {
  /**
   * Possible step-up auth origins
   */
  origin: AuthTokenScopes1Origin;
  verifiedAt?: number | undefined;
  expiresAt: number;
};

export const ScopesOrigin = {
  App: "app",
  Apple: "apple",
  Bitbucket: "bitbucket",
  Chatgpt: "chatgpt",
  Email: "email",
  Emu: "emu",
  Github: "github",
  GithubWebhook: "github-webhook",
  Gitlab: "gitlab",
  Google: "google",
  Invite: "invite",
  Manual: "manual",
  Otp: "otp",
  Passkey: "passkey",
  Saml: "saml",
  Sms: "sms",
  TokenExchangeOidc: "token-exchange-oidc",
} as const;
export type ScopesOrigin = ClosedEnum<typeof ScopesOrigin>;

/**
 * The access scopes granted to the token.
 */
export type Scopes1 = {
  type: "user";
  sudo?: Sudo | undefined;
  origin?: ScopesOrigin | undefined;
  createdAt: number;
  expiresAt?: number | undefined;
};

export type Scopes = Scopes1 | Scopes2;

/**
 * Authentication token metadata.
 */
export type AuthToken = {
  /**
   * The unique identifier of the token.
   */
  id: string;
  /**
   * The human-readable name of the token.
   */
  name: string;
  /**
   * The type of the token.
   */
  type: string;
  /**
   * The token's prefix, for identification purposes.
   */
  prefix?: string | undefined;
  /**
   * The last few characters of the token, for identification purposes.
   */
  suffix?: string | undefined;
  /**
   * The origin of how the token was created.
   */
  origin?: string | undefined;
  /**
   * The access scopes granted to the token.
   */
  scopes?: Array<Scopes1 | Scopes2> | undefined;
  /**
   * Timestamp (in milliseconds) of when the token was created.
   */
  createdAt: number;
  /**
   * Timestamp (in milliseconds) of when the token was most recently used.
   */
  activeAt: number;
  /**
   * Timestamp (in milliseconds) of when the token expires.
   */
  expiresAt?: number | undefined;
  /**
   * Timestamp (in milliseconds) of when the token was revoked.
   */
  revokedAt?: number | undefined;
  /**
   * Timestamp (in milliseconds) of when the token was marked as leaked.
   */
  leakedAt?: number | undefined;
  /**
   * URL where the token was discovered as leaked.
   */
  leakedUrl?: string | undefined;
};

/** @internal */
export const AuthTokenScopesOrigin$inboundSchema: z.ZodNativeEnum<
  typeof AuthTokenScopesOrigin
> = z.nativeEnum(AuthTokenScopesOrigin);

/** @internal */
export const Scopes2$inboundSchema: z.ZodType<Scopes2, z.ZodTypeDef, unknown> =
  z.object({
    type: types.literal("team"),
    teamId: types.string(),
    origin: types.optional(AuthTokenScopesOrigin$inboundSchema),
    createdAt: types.number(),
    expiresAt: types.optional(types.number()),
  });

export function scopes2FromJSON(
  jsonString: string,
): SafeParseResult<Scopes2, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Scopes2$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Scopes2' from JSON`,
  );
}

/** @internal */
export const AuthTokenScopes1Origin$inboundSchema: z.ZodNativeEnum<
  typeof AuthTokenScopes1Origin
> = z.nativeEnum(AuthTokenScopes1Origin);

/** @internal */
export const Sudo$inboundSchema: z.ZodType<Sudo, z.ZodTypeDef, unknown> = z
  .object({
    origin: AuthTokenScopes1Origin$inboundSchema,
    verifiedAt: types.optional(types.number()),
    expiresAt: types.number(),
  });

export function sudoFromJSON(
  jsonString: string,
): SafeParseResult<Sudo, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Sudo$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Sudo' from JSON`,
  );
}

/** @internal */
export const ScopesOrigin$inboundSchema: z.ZodNativeEnum<typeof ScopesOrigin> =
  z.nativeEnum(ScopesOrigin);

/** @internal */
export const Scopes1$inboundSchema: z.ZodType<Scopes1, z.ZodTypeDef, unknown> =
  z.object({
    type: types.literal("user"),
    sudo: types.optional(z.lazy(() => Sudo$inboundSchema)),
    origin: types.optional(ScopesOrigin$inboundSchema),
    createdAt: types.number(),
    expiresAt: types.optional(types.number()),
  });

export function scopes1FromJSON(
  jsonString: string,
): SafeParseResult<Scopes1, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Scopes1$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Scopes1' from JSON`,
  );
}

/** @internal */
export const Scopes$inboundSchema: z.ZodType<Scopes, z.ZodTypeDef, unknown> = z
  .union([
    z.lazy(() => Scopes1$inboundSchema),
    z.lazy(() => Scopes2$inboundSchema),
  ]);

export function scopesFromJSON(
  jsonString: string,
): SafeParseResult<Scopes, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Scopes$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Scopes' from JSON`,
  );
}

/** @internal */
export const AuthToken$inboundSchema: z.ZodType<
  AuthToken,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  name: types.string(),
  type: types.string(),
  prefix: types.optional(types.string()),
  suffix: types.optional(types.string()),
  origin: types.optional(types.string()),
  scopes: types.optional(
    z.array(z.union([
      z.lazy(() => Scopes1$inboundSchema),
      z.lazy(() =>
        Scopes2$inboundSchema
      ),
    ])),
  ),
  createdAt: types.number(),
  activeAt: types.number(),
  expiresAt: types.optional(types.number()),
  revokedAt: types.optional(types.number()),
  leakedAt: types.optional(types.number()),
  leakedUrl: types.optional(types.string()),
});

export function authTokenFromJSON(
  jsonString: string,
): SafeParseResult<AuthToken, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => AuthToken$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'AuthToken' from JSON`,
  );
}
