/*
 * 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 type GetCustomEnvironmentRequest = {
  /**
   * The unique project identifier or the project name
   */
  idOrName: string;
  /**
   * The unique custom environment identifier within the project
   */
  environmentSlugOrId: 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;
};

/**
 * The type of environment (production, preview, or development)
 */
export const GetCustomEnvironmentType = {
  Development: "development",
  Preview: "preview",
  Production: "production",
} as const;
/**
 * The type of environment (production, preview, or development)
 */
export type GetCustomEnvironmentType = ClosedEnum<
  typeof GetCustomEnvironmentType
>;

/**
 * The type of matching to perform
 */
export const GetCustomEnvironmentEnvironmentType = {
  EndsWith: "endsWith",
  Equals: "equals",
  StartsWith: "startsWith",
} as const;
/**
 * The type of matching to perform
 */
export type GetCustomEnvironmentEnvironmentType = ClosedEnum<
  typeof GetCustomEnvironmentEnvironmentType
>;

/**
 * Configuration for matching git branches to this environment
 */
export type GetCustomEnvironmentBranchMatcher = {
  /**
   * The type of matching to perform
   */
  type: GetCustomEnvironmentEnvironmentType;
  /**
   * The pattern to match against branch names
   */
  pattern: string;
};

/**
 * A list of verification challenges, one of which must be completed to verify the domain for use on the project. After the challenge is complete `POST /projects/:idOrName/domains/:domain/verify` to verify the domain. Possible challenges: - If `verification.type = TXT` the `verification.domain` will be checked for a TXT record matching `verification.value`.
 */
export type GetCustomEnvironmentVerification = {
  type: string;
  domain: string;
  value: string;
  reason: string;
};

/**
 * List of domains associated with this environment
 */
export type GetCustomEnvironmentDomains = {
  name: string;
  apexName: string;
  projectId: string;
  redirect?: string | null | undefined;
  redirectStatusCode?: number | null | undefined;
  gitBranch?: string | null | undefined;
  customEnvironmentId?: string | null | undefined;
  updatedAt?: number | undefined;
  createdAt?: number | undefined;
  /**
   * `true` if the domain is verified for use with the project. If `false` it will not be used as an alias on this project until the challenge in `verification` is completed.
   */
  verified: boolean;
  /**
   * A list of verification challenges, one of which must be completed to verify the domain for use on the project. After the challenge is complete `POST /projects/:idOrName/domains/:domain/verify` to verify the domain. Possible challenges: - If `verification.type = TXT` the `verification.domain` will be checked for a TXT record matching `verification.value`.
   */
  verification?: Array<GetCustomEnvironmentVerification> | undefined;
};

/**
 * Internal representation of a custom environment with all required properties
 */
export type GetCustomEnvironmentResponseBody = {
  /**
   * Unique identifier for the custom environment (format: env_*)
   */
  id: string;
  /**
   * URL-friendly name of the environment
   */
  slug: string;
  /**
   * The type of environment (production, preview, or development)
   */
  type: GetCustomEnvironmentType;
  /**
   * Optional description of the environment's purpose
   */
  description?: string | undefined;
  /**
   * Configuration for matching git branches to this environment
   */
  branchMatcher?: GetCustomEnvironmentBranchMatcher | undefined;
  /**
   * List of domains associated with this environment
   */
  domains?: Array<GetCustomEnvironmentDomains> | undefined;
  /**
   * List of aliases for the current deployment
   */
  currentDeploymentAliases?: Array<string> | undefined;
  /**
   * Timestamp when the environment was created
   */
  createdAt: number;
  /**
   * Timestamp when the environment was last updated
   */
  updatedAt: number;
};

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

/** @internal */
export const GetCustomEnvironmentRequest$outboundSchema: z.ZodType<
  GetCustomEnvironmentRequest$Outbound,
  z.ZodTypeDef,
  GetCustomEnvironmentRequest
> = z.object({
  idOrName: z.string(),
  environmentSlugOrId: z.string(),
  teamId: z.string().optional(),
  slug: z.string().optional(),
});

export function getCustomEnvironmentRequestToJSON(
  getCustomEnvironmentRequest: GetCustomEnvironmentRequest,
): string {
  return JSON.stringify(
    GetCustomEnvironmentRequest$outboundSchema.parse(
      getCustomEnvironmentRequest,
    ),
  );
}

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

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

/** @internal */
export const GetCustomEnvironmentBranchMatcher$inboundSchema: z.ZodType<
  GetCustomEnvironmentBranchMatcher,
  z.ZodTypeDef,
  unknown
> = z.object({
  type: GetCustomEnvironmentEnvironmentType$inboundSchema,
  pattern: types.string(),
});

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

/** @internal */
export const GetCustomEnvironmentVerification$inboundSchema: z.ZodType<
  GetCustomEnvironmentVerification,
  z.ZodTypeDef,
  unknown
> = z.object({
  type: types.string(),
  domain: types.string(),
  value: types.string(),
  reason: types.string(),
});

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

/** @internal */
export const GetCustomEnvironmentDomains$inboundSchema: z.ZodType<
  GetCustomEnvironmentDomains,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: types.string(),
  apexName: types.string(),
  projectId: types.string(),
  redirect: z.nullable(types.string()).optional(),
  redirectStatusCode: z.nullable(types.number()).optional(),
  gitBranch: z.nullable(types.string()).optional(),
  customEnvironmentId: z.nullable(types.string()).optional(),
  updatedAt: types.optional(types.number()),
  createdAt: types.optional(types.number()),
  verified: types.boolean(),
  verification: types.optional(
    z.array(z.lazy(() => GetCustomEnvironmentVerification$inboundSchema)),
  ),
});

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

/** @internal */
export const GetCustomEnvironmentResponseBody$inboundSchema: z.ZodType<
  GetCustomEnvironmentResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  slug: types.string(),
  type: GetCustomEnvironmentType$inboundSchema,
  description: types.optional(types.string()),
  branchMatcher: types.optional(
    z.lazy(() => GetCustomEnvironmentBranchMatcher$inboundSchema),
  ),
  domains: types.optional(
    z.array(z.lazy(() => GetCustomEnvironmentDomains$inboundSchema)),
  ),
  currentDeploymentAliases: types.optional(z.array(types.string())),
  createdAt: types.number(),
  updatedAt: types.number(),
});

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