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

import * as z from "zod/v3";
import { remap as remap$ } from "../lib/primitives.js";
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 type StartRollingReleaseRequestBody = {
  /**
   * The ID of the canary deployment to start the rolling release for
   */
  canaryDeploymentId: string;
};

export type StartRollingReleaseRequest = {
  /**
   * Project ID or project name (URL-encoded)
   */
  idOrName: string;
  /**
   * The Team identifier to perform the request on behalf of.
   */
  teamId?: string | undefined;
  /**
   * The Team slug to perform the request on behalf of.
   */
  slug?: string | undefined;
  requestBody?: StartRollingReleaseRequestBody | undefined;
};

/**
 * The current state of the rolling release
 */
export const StartRollingReleaseState = {
  Aborted: "ABORTED",
  Active: "ACTIVE",
  Complete: "COMPLETE",
} as const;
/**
 * The current state of the rolling release
 */
export type StartRollingReleaseState = ClosedEnum<
  typeof StartRollingReleaseState
>;

/**
 * When set to `PAUSED`, the rollout is frozen at the current percentage until continued.
 */
export const StartRollingReleaseSubstate = {
  Paused: "PAUSED",
} as const;
/**
 * When set to `PAUSED`, the rollout is frozen at the current percentage until continued.
 */
export type StartRollingReleaseSubstate = ClosedEnum<
  typeof StartRollingReleaseSubstate
>;

/**
 * The state of the deployment depending on the process of deploying, or if it is ready or in an error state
 */
export const StartRollingReleaseReadyState = {
  Blocked: "BLOCKED",
  Building: "BUILDING",
  Canceled: "CANCELED",
  Error: "ERROR",
  Initializing: "INITIALIZING",
  Queued: "QUEUED",
  Ready: "READY",
} as const;
/**
 * The state of the deployment depending on the process of deploying, or if it is ready or in an error state
 */
export type StartRollingReleaseReadyState = ClosedEnum<
  typeof StartRollingReleaseReadyState
>;

/**
 * If defined, either `staging` if a staging alias in the format `<project>.<team>.now.sh` was assigned upon creation, or `production` if the aliases from `alias` were assigned. `null` value indicates the "preview" deployment.
 */
export const StartRollingReleaseTarget = {
  Production: "production",
  Staging: "staging",
} as const;
/**
 * If defined, either `staging` if a staging alias in the format `<project>.<team>.now.sh` was assigned upon creation, or `production` if the aliases from `alias` were assigned. `null` value indicates the "preview" deployment.
 */
export type StartRollingReleaseTarget = ClosedEnum<
  typeof StartRollingReleaseTarget
>;

/**
 * Where was the deployment created from. Best-effort guess for metrics only — not authoritative; do not gate behavior on it.
 */
export const StartRollingReleaseSource = {
  ApiTriggerGitDeploy: "api-trigger-git-deploy",
  Cli: "cli",
  CloneRepo: "clone/repo",
  Drop: "drop",
  Git: "git",
  GitDeployHook: "git-deploy-hook",
  Import: "import",
  ImportRepo: "import/repo",
  Redeploy: "redeploy",
  V0Web: "v0-web",
} as const;
/**
 * Where was the deployment created from. Best-effort guess for metrics only — not authoritative; do not gate behavior on it.
 */
export type StartRollingReleaseSource = ClosedEnum<
  typeof StartRollingReleaseSource
>;

/**
 * The current deployment receiving production traffic
 */
export type StartRollingReleaseCurrentDeployment = {
  /**
   * The name of the project associated with the deployment at the time that the deployment was created
   */
  name: string;
  /**
   * A number containing the date when the deployment was created in milliseconds
   */
  createdAt: number;
  /**
   * The state of the deployment depending on the process of deploying, or if it is ready or in an error state
   */
  readyState: StartRollingReleaseReadyState;
  /**
   * A string holding the unique ID of the deployment
   */
  id: string;
  /**
   * If defined, either `staging` if a staging alias in the format `<project>.<team>.now.sh` was assigned upon creation, or `production` if the aliases from `alias` were assigned. `null` value indicates the "preview" deployment.
   */
  target?: StartRollingReleaseTarget | null | undefined;
  readyStateAt?: number | undefined;
  /**
   * Where was the deployment created from. Best-effort guess for metrics only — not authoritative; do not gate behavior on it.
   */
  source?: StartRollingReleaseSource | undefined;
  /**
   * A string with the unique URL of the deployment
   */
  url: string;
};

/**
 * The state of the deployment depending on the process of deploying, or if it is ready or in an error state
 */
export const StartRollingReleaseRollingReleaseReadyState = {
  Blocked: "BLOCKED",
  Building: "BUILDING",
  Canceled: "CANCELED",
  Error: "ERROR",
  Initializing: "INITIALIZING",
  Queued: "QUEUED",
  Ready: "READY",
} as const;
/**
 * The state of the deployment depending on the process of deploying, or if it is ready or in an error state
 */
export type StartRollingReleaseRollingReleaseReadyState = ClosedEnum<
  typeof StartRollingReleaseRollingReleaseReadyState
>;

/**
 * If defined, either `staging` if a staging alias in the format `<project>.<team>.now.sh` was assigned upon creation, or `production` if the aliases from `alias` were assigned. `null` value indicates the "preview" deployment.
 */
export const StartRollingReleaseRollingReleaseTarget = {
  Production: "production",
  Staging: "staging",
} as const;
/**
 * If defined, either `staging` if a staging alias in the format `<project>.<team>.now.sh` was assigned upon creation, or `production` if the aliases from `alias` were assigned. `null` value indicates the "preview" deployment.
 */
export type StartRollingReleaseRollingReleaseTarget = ClosedEnum<
  typeof StartRollingReleaseRollingReleaseTarget
>;

/**
 * Where was the deployment created from. Best-effort guess for metrics only — not authoritative; do not gate behavior on it.
 */
export const StartRollingReleaseRollingReleaseSource = {
  ApiTriggerGitDeploy: "api-trigger-git-deploy",
  Cli: "cli",
  CloneRepo: "clone/repo",
  Drop: "drop",
  Git: "git",
  GitDeployHook: "git-deploy-hook",
  Import: "import",
  ImportRepo: "import/repo",
  Redeploy: "redeploy",
  V0Web: "v0-web",
} as const;
/**
 * Where was the deployment created from. Best-effort guess for metrics only — not authoritative; do not gate behavior on it.
 */
export type StartRollingReleaseRollingReleaseSource = ClosedEnum<
  typeof StartRollingReleaseRollingReleaseSource
>;

/**
 * The canary deployment being rolled out
 */
export type StartRollingReleaseCanaryDeployment = {
  /**
   * The name of the project associated with the deployment at the time that the deployment was created
   */
  name: string;
  /**
   * A number containing the date when the deployment was created in milliseconds
   */
  createdAt: number;
  /**
   * The state of the deployment depending on the process of deploying, or if it is ready or in an error state
   */
  readyState: StartRollingReleaseRollingReleaseReadyState;
  /**
   * A string holding the unique ID of the deployment
   */
  id: string;
  /**
   * If defined, either `staging` if a staging alias in the format `<project>.<team>.now.sh` was assigned upon creation, or `production` if the aliases from `alias` were assigned. `null` value indicates the "preview" deployment.
   */
  target?: StartRollingReleaseRollingReleaseTarget | null | undefined;
  readyStateAt?: number | undefined;
  /**
   * Where was the deployment created from. Best-effort guess for metrics only — not authoritative; do not gate behavior on it.
   */
  source?: StartRollingReleaseRollingReleaseSource | undefined;
  /**
   * A string with the unique URL of the deployment
   */
  url: string;
};

/**
 * The advancement type of the rolling release
 */
export const StartRollingReleaseAdvancementType = {
  Automatic: "automatic",
  ManualApproval: "manual-approval",
} as const;
/**
 * The advancement type of the rolling release
 */
export type StartRollingReleaseAdvancementType = ClosedEnum<
  typeof StartRollingReleaseAdvancementType
>;

/**
 * All stages configured for this rolling release
 */
export type StartRollingReleaseStages = {
  /**
   * The zero-based index of the stage
   */
  index: number;
  /**
   * Whether or not this stage is the final stage (targetPercentage === 100)
   */
  isFinalStage: boolean;
  /**
   * The percentage of traffic to serve to the canary deployment (0-100)
   */
  targetPercentage: number;
  /**
   * Whether or not this stage requires manual approval to proceed
   */
  requireApproval: boolean;
  /**
   * Duration in seconds for automatic advancement, null for manual stages or the final stage
   */
  duration: number | null;
  /**
   * Whether to linearly shift traffic over the duration of this stage
   */
  linearShift?: boolean | undefined;
};

/**
 * The currently active stage, null if the rollout is aborted
 */
export type StartRollingReleaseActiveStage = {
  /**
   * The zero-based index of the stage
   */
  index: number;
  /**
   * Whether or not this stage is the final stage (targetPercentage === 100)
   */
  isFinalStage: boolean;
  /**
   * The percentage of traffic to serve to the canary deployment (0-100)
   */
  targetPercentage: number;
  /**
   * Whether or not this stage requires manual approval to proceed
   */
  requireApproval: boolean;
  /**
   * Duration in seconds for automatic advancement, null for manual stages or the final stage
   */
  duration: number | null;
  /**
   * Whether to linearly shift traffic over the duration of this stage
   */
  linearShift?: boolean | undefined;
};

/**
 * The next stage to be activated, null if not in ACTIVE state
 */
export type StartRollingReleaseNextStage = {
  /**
   * The zero-based index of the stage
   */
  index: number;
  /**
   * Whether or not this stage is the final stage (targetPercentage === 100)
   */
  isFinalStage: boolean;
  /**
   * The percentage of traffic to serve to the canary deployment (0-100)
   */
  targetPercentage: number;
  /**
   * Whether or not this stage requires manual approval to proceed
   */
  requireApproval: boolean;
  /**
   * Duration in seconds for automatic advancement, null for manual stages or the final stage
   */
  duration: number | null;
  /**
   * Whether to linearly shift traffic over the duration of this stage
   */
  linearShift?: boolean | undefined;
};

/**
 * Rolling release information including configuration and document details, or null if no rolling release exists
 */
export type StartRollingReleaseRollingRelease = {
  /**
   * The current state of the rolling release
   */
  state: StartRollingReleaseState;
  /**
   * When set to `PAUSED`, the rollout is frozen at the current percentage until continued.
   */
  substate: StartRollingReleaseSubstate | null;
  /**
   * The current deployment receiving production traffic
   */
  currentDeployment: StartRollingReleaseCurrentDeployment | null;
  /**
   * The canary deployment being rolled out
   */
  canaryDeployment: StartRollingReleaseCanaryDeployment | null;
  /**
   * The ID of a deployment queued for the next rolling release
   */
  queuedDeploymentId: string | null;
  /**
   * The advancement type of the rolling release
   */
  advancementType: StartRollingReleaseAdvancementType;
  /**
   * All stages configured for this rolling release
   */
  stages: Array<StartRollingReleaseStages>;
  /**
   * The currently active stage, null if the rollout is aborted
   */
  activeStage: StartRollingReleaseActiveStage | null;
  /**
   * The next stage to be activated, null if not in ACTIVE state
   */
  nextStage: StartRollingReleaseNextStage | null;
  /**
   * Unix timestamp in milliseconds when the rolling release started
   */
  startedAt: number;
  /**
   * Unix timestamp in milliseconds when the rolling release was last updated
   */
  updatedAt: number;
  /**
   * When set (for example while {@link substate} is `PAUSED`), the canary traffic percentage persisted on the rollout document — use for dashboard display when linear shift is active.
   */
  currentCanaryPercentage?: number | undefined;
};

/**
 * The response format for rolling release endpoints that return rolling release information
 */
export type StartRollingReleaseResponseBody = {
  /**
   * Rolling release information including configuration and document details, or null if no rolling release exists
   */
  rollingRelease: StartRollingReleaseRollingRelease | null;
};

/** @internal */
export type StartRollingReleaseRequestBody$Outbound = {
  canaryDeploymentId: string;
};

/** @internal */
export const StartRollingReleaseRequestBody$outboundSchema: z.ZodType<
  StartRollingReleaseRequestBody$Outbound,
  z.ZodTypeDef,
  StartRollingReleaseRequestBody
> = z.object({
  canaryDeploymentId: z.string(),
});

export function startRollingReleaseRequestBodyToJSON(
  startRollingReleaseRequestBody: StartRollingReleaseRequestBody,
): string {
  return JSON.stringify(
    StartRollingReleaseRequestBody$outboundSchema.parse(
      startRollingReleaseRequestBody,
    ),
  );
}

/** @internal */
export type StartRollingReleaseRequest$Outbound = {
  idOrName: string;
  teamId?: string | undefined;
  slug?: string | undefined;
  RequestBody?: StartRollingReleaseRequestBody$Outbound | undefined;
};

/** @internal */
export const StartRollingReleaseRequest$outboundSchema: z.ZodType<
  StartRollingReleaseRequest$Outbound,
  z.ZodTypeDef,
  StartRollingReleaseRequest
> = z.object({
  idOrName: z.string(),
  teamId: z.string().optional(),
  slug: z.string().optional(),
  requestBody: z.lazy(() => StartRollingReleaseRequestBody$outboundSchema)
    .optional(),
}).transform((v) => {
  return remap$(v, {
    requestBody: "RequestBody",
  });
});

export function startRollingReleaseRequestToJSON(
  startRollingReleaseRequest: StartRollingReleaseRequest,
): string {
  return JSON.stringify(
    StartRollingReleaseRequest$outboundSchema.parse(startRollingReleaseRequest),
  );
}

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

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

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

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

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

/** @internal */
export const StartRollingReleaseCurrentDeployment$inboundSchema: z.ZodType<
  StartRollingReleaseCurrentDeployment,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: types.string(),
  createdAt: types.number(),
  readyState: StartRollingReleaseReadyState$inboundSchema,
  id: types.string(),
  target: z.nullable(StartRollingReleaseTarget$inboundSchema).optional(),
  readyStateAt: types.optional(types.number()),
  source: types.optional(StartRollingReleaseSource$inboundSchema),
  url: types.string(),
});

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

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

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

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

/** @internal */
export const StartRollingReleaseCanaryDeployment$inboundSchema: z.ZodType<
  StartRollingReleaseCanaryDeployment,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: types.string(),
  createdAt: types.number(),
  readyState: StartRollingReleaseRollingReleaseReadyState$inboundSchema,
  id: types.string(),
  target: z.nullable(StartRollingReleaseRollingReleaseTarget$inboundSchema)
    .optional(),
  readyStateAt: types.optional(types.number()),
  source: types.optional(StartRollingReleaseRollingReleaseSource$inboundSchema),
  url: types.string(),
});

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

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

/** @internal */
export const StartRollingReleaseStages$inboundSchema: z.ZodType<
  StartRollingReleaseStages,
  z.ZodTypeDef,
  unknown
> = z.object({
  index: types.number(),
  isFinalStage: types.boolean(),
  targetPercentage: types.number(),
  requireApproval: types.boolean(),
  duration: types.nullable(types.number()),
  linearShift: types.optional(types.boolean()),
});

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

/** @internal */
export const StartRollingReleaseActiveStage$inboundSchema: z.ZodType<
  StartRollingReleaseActiveStage,
  z.ZodTypeDef,
  unknown
> = z.object({
  index: types.number(),
  isFinalStage: types.boolean(),
  targetPercentage: types.number(),
  requireApproval: types.boolean(),
  duration: types.nullable(types.number()),
  linearShift: types.optional(types.boolean()),
});

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

/** @internal */
export const StartRollingReleaseNextStage$inboundSchema: z.ZodType<
  StartRollingReleaseNextStage,
  z.ZodTypeDef,
  unknown
> = z.object({
  index: types.number(),
  isFinalStage: types.boolean(),
  targetPercentage: types.number(),
  requireApproval: types.boolean(),
  duration: types.nullable(types.number()),
  linearShift: types.optional(types.boolean()),
});

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

/** @internal */
export const StartRollingReleaseRollingRelease$inboundSchema: z.ZodType<
  StartRollingReleaseRollingRelease,
  z.ZodTypeDef,
  unknown
> = z.object({
  state: StartRollingReleaseState$inboundSchema,
  substate: types.nullable(StartRollingReleaseSubstate$inboundSchema),
  currentDeployment: types.nullable(
    z.lazy(() => StartRollingReleaseCurrentDeployment$inboundSchema),
  ),
  canaryDeployment: types.nullable(
    z.lazy(() => StartRollingReleaseCanaryDeployment$inboundSchema),
  ),
  queuedDeploymentId: types.nullable(types.string()),
  advancementType: StartRollingReleaseAdvancementType$inboundSchema,
  stages: z.array(z.lazy(() => StartRollingReleaseStages$inboundSchema)),
  activeStage: types.nullable(
    z.lazy(() => StartRollingReleaseActiveStage$inboundSchema),
  ),
  nextStage: types.nullable(
    z.lazy(() => StartRollingReleaseNextStage$inboundSchema),
  ),
  startedAt: types.number(),
  updatedAt: types.number(),
  currentCanaryPercentage: types.optional(types.number()),
});

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

/** @internal */
export const StartRollingReleaseResponseBody$inboundSchema: z.ZodType<
  StartRollingReleaseResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  rollingRelease: types.nullable(
    z.lazy(() => StartRollingReleaseRollingRelease$inboundSchema),
  ),
});

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