/*
 * 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";

/**
 * The status of the current sandbox.
 */
export const NamedSandboxStatus = {
  Running: "running",
  Stopped: "stopped",
  Stopping: "stopping",
} as const;
/**
 * The status of the current sandbox.
 */
export type NamedSandboxStatus = ClosedEnum<typeof NamedSandboxStatus>;

/**
 * The regions the sandbox fails over to. Empty when it does not fail over.
 */
export const FailoverRegions = {
  Arn1: "arn1",
  Bom1: "bom1",
  Cdg1: "cdg1",
  Cle1: "cle1",
  Cpt1: "cpt1",
  Dub1: "dub1",
  Fra1: "fra1",
  Gru1: "gru1",
  Hkg1: "hkg1",
  Hnd1: "hnd1",
  Iad1: "iad1",
  Icn1: "icn1",
  Kix1: "kix1",
  Lhr1: "lhr1",
  Pdx1: "pdx1",
  Sfo1: "sfo1",
  Sin1: "sin1",
  Syd1: "syd1",
  Yul1: "yul1",
} as const;
/**
 * The regions the sandbox fails over to. Empty when it does not fail over.
 */
export type FailoverRegions = ClosedEnum<typeof FailoverRegions>;

/**
 * Keep-last snapshot configuration.
 */
export type KeepLastSnapshots = {
  /**
   * Number of most recent snapshots to keep.
   */
  count: number;
  /**
   * Expiration time in milliseconds for kept snapshots.
   */
  expiration?: number | undefined;
  /**
   * Whether to immediately delete evicted snapshots.
   */
  deleteEvicted: boolean;
};

export const NamedSandboxNetworkPolicyMode = {
  AllowAll: "allow-all",
  Custom: "custom",
  DefaultAllow: "default-allow",
  DefaultDeny: "default-deny",
  DenyAll: "deny-all",
} as const;
export type NamedSandboxNetworkPolicyMode = ClosedEnum<
  typeof NamedSandboxNetworkPolicyMode
>;

/**
 * Network policy configuration.
 */
export type NetworkPolicy = {
  mode: NamedSandboxNetworkPolicyMode;
  allowedDomains?: Array<string> | undefined;
  allowedCIDRs?: Array<string> | undefined;
  deniedCIDRs?: Array<string> | undefined;
  s3Key?: string | undefined;
};

export const NamedSandboxMode = {
  ReadOnly: "read-only",
  ReadWrite: "read-write",
  Snapshot: "snapshot",
} as const;
export type NamedSandboxMode = ClosedEnum<typeof NamedSandboxMode>;

/**
 * Key-value pairs of mount path and drive.
 */
export type Mounts = {
  drive: string;
  mode?: NamedSandboxMode | undefined;
};

/**
 * This object contains information related to a Vercel NamedSandbox.
 */
export type NamedSandbox = {
  /**
   * The unique identifier of the sandbox.
   */
  name: string;
  /**
   * Current snapshot ID that the named sandbox is pointing to.
   */
  currentSnapshotId?: string | undefined;
  /**
   * Current session ID the sandbox is pointing to.
   */
  currentSessionId: string;
  /**
   * The status of the current sandbox.
   */
  status: NamedSandboxStatus;
  /**
   * The time when the sandbox status was last updated, in milliseconds since the epoch.
   */
  statusUpdatedAt: number;
  /**
   * Whether the sandbox persists its state across restarts via automatic snapshots.
   */
  persistent: boolean;
  /**
   * The region the sandbox is pinned to: the region stored on the sandbox, otherwise the platform default. Where a running session actually landed is reported by `session.region`.
   */
  region?: string | undefined;
  /**
   * The regions the sandbox fails over to. Empty when it does not fail over.
   */
  failoverRegions?: Array<FailoverRegions> | undefined;
  /**
   * Number of virtual CPUs allocated.
   */
  vcpus?: number | undefined;
  /**
   * Memory allocated in MB.
   */
  memory?: number | undefined;
  /**
   * Runtime identifier.
   */
  runtime?: string | undefined;
  /**
   * Digest-pinned reference of the container image the sandbox was created from, when it was created from an image ("{repository}@{manifestDigest}").
   */
  image?: string | undefined;
  /**
   * Timeout in milliseconds.
   */
  timeout?: number | undefined;
  /**
   * Default snapshot expiration time in milliseconds. 0 means no expiration.
   */
  snapshotExpiration?: number | undefined;
  /**
   * Keep-last snapshot configuration.
   */
  keepLastSnapshots?: KeepLastSnapshots | undefined;
  /**
   * Network policy configuration.
   */
  networkPolicy?: NetworkPolicy | undefined;
  /**
   * The Connect network id for the target Secure Compute private network.
   */
  networkId?: string | undefined;
  /**
   * Cumulative egress bytes across all sandbox runs.
   */
  totalEgressBytes?: number | undefined;
  /**
   * Cumulative ingress bytes across all sandbox runs.
   */
  totalIngressBytes?: number | undefined;
  /**
   * Cumulative active CPU duration in milliseconds across all sandbox runs.
   */
  totalActiveCpuDurationMs?: number | undefined;
  /**
   * Cumulative wall-clock duration in milliseconds across all sandbox runs.
   */
  totalDurationMs?: number | undefined;
  /**
   * The working directory of the sandbox.
   */
  cwd?: string | undefined;
  /**
   * Key-value tags attached to the named sandbox.
   */
  tags?: { [k: string]: string } | undefined;
  /**
   * Key-value pairs of mount path and drive.
   */
  mounts?: { [k: string]: Mounts } | undefined;
  /**
   * The time when the named sandbox was created, in milliseconds since the epoch.
   */
  createdAt: number;
  /**
   * The time when the named sandbox was last updated, in milliseconds since the epoch.
   */
  updatedAt: number;
  /**
   * The time at which the currently running sandbox will time out, in milliseconds since the epoch. Only present while a session is running.
   */
  expiresAt?: number | undefined;
};

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

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

/** @internal */
export const KeepLastSnapshots$inboundSchema: z.ZodType<
  KeepLastSnapshots,
  z.ZodTypeDef,
  unknown
> = z.object({
  count: types.number(),
  expiration: types.optional(types.number()),
  deleteEvicted: types.boolean(),
});

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

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

/** @internal */
export const NetworkPolicy$inboundSchema: z.ZodType<
  NetworkPolicy,
  z.ZodTypeDef,
  unknown
> = z.object({
  mode: NamedSandboxNetworkPolicyMode$inboundSchema,
  allowedDomains: types.optional(z.array(types.string())),
  allowedCIDRs: types.optional(z.array(types.string())),
  deniedCIDRs: types.optional(z.array(types.string())),
  s3Key: types.optional(types.string()),
});

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

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

/** @internal */
export const Mounts$inboundSchema: z.ZodType<Mounts, z.ZodTypeDef, unknown> = z
  .object({
    drive: types.string(),
    mode: types.optional(NamedSandboxMode$inboundSchema),
  });

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

/** @internal */
export const NamedSandbox$inboundSchema: z.ZodType<
  NamedSandbox,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: types.string(),
  currentSnapshotId: types.optional(types.string()),
  currentSessionId: types.string(),
  status: NamedSandboxStatus$inboundSchema,
  statusUpdatedAt: types.number(),
  persistent: types.boolean(),
  region: types.optional(types.string()),
  failoverRegions: types.optional(z.array(FailoverRegions$inboundSchema)),
  vcpus: types.optional(types.number()),
  memory: types.optional(types.number()),
  runtime: types.optional(types.string()),
  image: types.optional(types.string()),
  timeout: types.optional(types.number()),
  snapshotExpiration: types.optional(types.number()),
  keepLastSnapshots: types.optional(
    z.lazy(() => KeepLastSnapshots$inboundSchema),
  ),
  networkPolicy: types.optional(z.lazy(() => NetworkPolicy$inboundSchema)),
  networkId: types.optional(types.string()),
  totalEgressBytes: types.optional(types.number()),
  totalIngressBytes: types.optional(types.number()),
  totalActiveCpuDurationMs: types.optional(types.number()),
  totalDurationMs: types.optional(types.number()),
  cwd: types.optional(types.string()),
  tags: types.optional(z.record(types.string())),
  mounts: types.optional(z.record(z.lazy(() => Mounts$inboundSchema))),
  createdAt: types.number(),
  updatedAt: types.number(),
  expiresAt: types.optional(types.number()),
});

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