/*
 * 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 {
  SandboxNetworkPolicy,
  SandboxNetworkPolicy$inboundSchema,
} from "./sandboxnetworkpolicy.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

/**
 * The status of the sandbox.
 */
export const SessionStatus = {
  Aborted: "aborted",
  Failed: "failed",
  Pending: "pending",
  Running: "running",
  Snapshotting: "snapshotting",
  Stopped: "stopped",
  Stopping: "stopping",
} as const;
/**
 * The status of the sandbox.
 */
export type SessionStatus = ClosedEnum<typeof SessionStatus>;

/**
 * The quantity of data transfered to and from the sandbox, in bytes. This value is only available once the sandbox is stopped, and only if it stopped successfully.
 */
export type NetworkTransfer = {
  ingress: number;
  egress: number;
};

/**
 * This object contains information related to a Vercel Sandbox Session. v2 endpoints return "session" instead of "sandbox" as the response wrapper key.
 */
export type Session = {
  /**
   * The name of the source sandbox.
   */
  sourceSandboxName: string;
  /**
   * The unique identifier of the project associated with this session.
   */
  projectId: string;
  /**
   * The unique identifier of the sandbox.
   */
  id: string;
  /**
   * Memory allocated to this sandbox in MB.
   */
  memory: number;
  /**
   * Number of vCPUs allocated to this sandbox.
   */
  vcpus: number;
  /**
   * The region where the sandbox is hosted.
   */
  region: string;
  /**
   * The runtime of the sandbox.
   */
  runtime: string;
  /**
   * The maximum amount of time the sandbox will run for in milliseconds.
   */
  timeout: number;
  /**
   * The status of the sandbox.
   */
  status: SessionStatus;
  /**
   * The time when the sandbox was requested, in milliseconds since the epoch.
   */
  requestedAt: number;
  /**
   * The time when the sandbox was started, in milliseconds since the epoch.
   */
  startedAt?: number | undefined;
  /**
   * The working directory of the sandbox.
   */
  cwd: string;
  /**
   * The time when the sandbox was requested to stop, in milliseconds since the epoch.
   */
  requestedStopAt?: number | undefined;
  /**
   * The time when the sandbox was stopped, in milliseconds since the epoch.
   */
  stoppedAt?: number | undefined;
  /**
   * The time when the sandbox was aborted, in milliseconds since the epoch.
   */
  abortedAt?: number | undefined;
  /**
   * The duration of the sandbox in milliseconds.
   */
  duration?: number | undefined;
  /**
   * The unique identifier of the snapshot associated with this sandbox, if any.
   */
  sourceSnapshotId?: string | undefined;
  /**
   * The time when a snapshot was requested, in milliseconds since the epoch.
   */
  snapshottedAt?: number | undefined;
  /**
   * The time when the sandbox was created, in milliseconds since the epoch.
   */
  createdAt: number;
  /**
   * The last time the sandbox was updated, in milliseconds since the epoch.
   */
  updatedAt: number;
  /**
   * The network policy applied to this sandbox, if any.
   */
  networkPolicy?: SandboxNetworkPolicy | undefined;
  /**
   * The amount of CPU time the sandbox consumed, if available, in milliseconds. This value is only available once the sandbox is stopped, and only if it stopped successfully.
   */
  activeCpuDurationMs?: number | undefined;
  /**
   * The quantity of data transfered to and from the sandbox, in bytes. This value is only available once the sandbox is stopped, and only if it stopped successfully.
   */
  networkTransfer?: NetworkTransfer | undefined;
};

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

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

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

/** @internal */
export const Session$inboundSchema: z.ZodType<Session, z.ZodTypeDef, unknown> =
  z.object({
    sourceSandboxName: types.string(),
    projectId: types.string(),
    id: types.string(),
    memory: types.number(),
    vcpus: types.number(),
    region: types.string(),
    runtime: types.string(),
    timeout: types.number(),
    status: SessionStatus$inboundSchema,
    requestedAt: types.number(),
    startedAt: types.optional(types.number()),
    cwd: types.string(),
    requestedStopAt: types.optional(types.number()),
    stoppedAt: types.optional(types.number()),
    abortedAt: types.optional(types.number()),
    duration: types.optional(types.number()),
    sourceSnapshotId: types.optional(types.string()),
    snapshottedAt: types.optional(types.number()),
    createdAt: types.number(),
    updatedAt: types.number(),
    networkPolicy: types.optional(SandboxNetworkPolicy$inboundSchema),
    activeCpuDurationMs: types.optional(types.number()),
    networkTransfer: types.optional(
      z.lazy(() => NetworkTransfer$inboundSchema),
    ),
  });

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