/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 * @generated-id: 928b2a3573d6
 */

import * as z from "zod/v4";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import * as openEnums from "../../types/enums.js";
import { OpenEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { smartUnion } from "../../types/smartUnion.js";
import * as components from "../components/index.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

export type StreamWorkflowExecutionLogsRequest = {
  executionId: string;
  /**
   * Filter logs by workflow run ID
   */
  runId?: string | null | undefined;
  /**
   * Filter logs by activity ID
   */
  activityId?: string | null | undefined;
  /**
   * Start a fresh stream at this timestamp (ignored when resuming via last_event_id)
   */
  after?: Date | null | undefined;
  /**
   * Resume from this cursor (a prior response's SSE id)
   */
  lastEventId?: string | null | undefined;
};

export const Event = {
  Log: "log",
  Error: "error",
} as const;
export type Event = OpenEnum<typeof Event>;

export type StreamWorkflowExecutionLogsData =
  | components.ExecutionLogRecord
  | components.StreamError;

/**
 * Stream of Server-Sent Events (SSE): `log` events carry an ExecutionLogRecord; `error` events carry a StreamError payload.
 */
export type StreamWorkflowExecutionLogsResponseBody = {
  event?: Event | undefined;
  id?: string | undefined;
  data?: components.ExecutionLogRecord | components.StreamError | undefined;
};

/** @internal */
export type StreamWorkflowExecutionLogsRequest$Outbound = {
  execution_id: string;
  run_id?: string | null | undefined;
  activity_id?: string | null | undefined;
  after?: string | null | undefined;
  last_event_id?: string | null | undefined;
};

/** @internal */
export const StreamWorkflowExecutionLogsRequest$outboundSchema: z.ZodType<
  StreamWorkflowExecutionLogsRequest$Outbound,
  StreamWorkflowExecutionLogsRequest
> = z.object({
  executionId: z.string(),
  runId: z.nullable(z.string()).optional(),
  activityId: z.nullable(z.string()).optional(),
  after: z.nullable(z.date().transform(v => v.toISOString())).optional(),
  lastEventId: z.nullable(z.string()).optional(),
}).transform((v) => {
  return remap$(v, {
    executionId: "execution_id",
    runId: "run_id",
    activityId: "activity_id",
    lastEventId: "last_event_id",
  });
});

export function streamWorkflowExecutionLogsRequestToJSON(
  streamWorkflowExecutionLogsRequest: StreamWorkflowExecutionLogsRequest,
): string {
  return JSON.stringify(
    StreamWorkflowExecutionLogsRequest$outboundSchema.parse(
      streamWorkflowExecutionLogsRequest,
    ),
  );
}

/** @internal */
export const Event$inboundSchema: z.ZodType<Event, unknown> = openEnums
  .inboundSchema(Event);

/** @internal */
export const StreamWorkflowExecutionLogsData$inboundSchema: z.ZodType<
  StreamWorkflowExecutionLogsData,
  unknown
> = smartUnion([
  components.ExecutionLogRecord$inboundSchema,
  components.StreamError$inboundSchema,
]);

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

/** @internal */
export const StreamWorkflowExecutionLogsResponseBody$inboundSchema: z.ZodType<
  StreamWorkflowExecutionLogsResponseBody,
  unknown
> = z.object({
  event: Event$inboundSchema.optional(),
  id: z.string().optional(),
  data: z.string().optional().transform((v, ctx) => {
    if (v === undefined) return undefined;
    try {
      return JSON.parse(v);
    } catch (err) {
      ctx.addIssue({
        input: v,
        code: "custom",
        message: `malformed json: ${err}`,
      });
      return z.NEVER;
    }
  }).pipe(
    smartUnion([
      components.ExecutionLogRecord$inboundSchema,
      components.StreamError$inboundSchema,
    ]).optional(),
  ),
});

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