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

import * as z from "zod/v3";
import { safeParse } from "../lib/schemas.js";
import { Result as SafeParseResult } from "../types/fp.js";
import * as types from "../types/primitives.js";
import { APIKeyQuota, APIKeyQuota$inboundSchema } from "./apikeyquota.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

/**
 * Generic metadata attached to the API key.
 *
 * @remarks
 *
 * The accepted shape depends on the key's `purpose` and is validated when the key is created. For `ai-gateway` keys this carries `environment` and `spendAttribution`.
 */
export type APIKeyMetadata = {};

/**
 * Information about the newly created API key.
 */
export type APIKey = {
  /**
   * The unique identifier of the API key.
   */
  id: string;
  /**
   * The human-readable name of the API key.
   */
  name: string;
  /**
   * The last few characters of the API key string, for helping identify the API key.
   */
  partialKey: string;
  /**
   * The ID of the team that the API key grants access to.
   */
  teamId: string;
  /**
   * The API key's purpose, i.e. what resources it can be used with.
   */
  purpose: string;
  /**
   * The ID of the project that this API key grants access to.
   *
   * @remarks
   *
   * When this is unset, the API key grants access to all projects in the team.
   */
  projectId: string | null;
  /**
   * Timestamp (in milliseconds) of when the API key expires.
   */
  expiresAt: number | null;
  /**
   * Timestamp (in milliseconds) of when the API key was most recently used.
   */
  activeAt: number;
  /**
   * Timestamp (in milliseconds) of when the API key was created.
   */
  createdAt: number;
  /**
   * The ID of the user who created the API key.
   */
  createdBy: string;
  /**
   * Timestamp (in milliseconds) of when the API key was marked as leaked.
   */
  leakedAt: number | null;
  /**
   * URL where the API key was discovered as leaked.
   */
  leakedUrl: string | null;
  /**
   * The ID of the app that created the API key, if any
   */
  createdByAppId: string | null;
  /**
   * AI Gateway quota associated with an API key.
   */
  quota?: APIKeyQuota | undefined;
  /**
   * Generic metadata attached to the API key.
   *
   * @remarks
   *
   * The accepted shape depends on the key's `purpose` and is validated when the key is created. For `ai-gateway` keys this carries `environment` and `spendAttribution`.
   */
  metadata?: APIKeyMetadata | undefined;
};

/** @internal */
export const APIKeyMetadata$inboundSchema: z.ZodType<
  APIKeyMetadata,
  z.ZodTypeDef,
  unknown
> = z.object({});

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

/** @internal */
export const APIKey$inboundSchema: z.ZodType<APIKey, z.ZodTypeDef, unknown> = z
  .object({
    id: types.string(),
    name: types.string(),
    partialKey: types.string(),
    teamId: types.string(),
    purpose: types.string(),
    projectId: types.nullable(types.string()),
    expiresAt: types.nullable(types.number()),
    activeAt: types.number(),
    createdAt: types.number(),
    createdBy: types.string(),
    leakedAt: types.nullable(types.number()),
    leakedUrl: types.nullable(types.string()),
    createdByAppId: types.nullable(types.string()),
    quota: types.optional(APIKeyQuota$inboundSchema),
    metadata: types.optional(z.lazy(() => APIKeyMetadata$inboundSchema)),
  });

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