import { z } from "zod";
import { ZWorkflowExecutableDefinition } from "./document";
import { ZWorkflowResponseCompletedTriggerPayload, ZWorkflowWebhookTriggerPayload } from "./triggers";

const ZIsoDateTime = z.iso.datetime({ offset: true });

export const ZWorkflowRunStatus = z.enum(["queued", "running", "completed", "failed", "canceled"]);
export type TWorkflowRunStatus = z.infer<typeof ZWorkflowRunStatus>;

export const ZWorkflowRunLogStatus = z.enum(["pending", "running", "succeeded", "failed", "skipped"]);
export type TWorkflowRunLogStatus = z.infer<typeof ZWorkflowRunLogStatus>;

const ZTriggeredAt = { triggeredAt: ZIsoDateTime };

/** `ZWorkflowTriggerPayload` with the time the trigger fired; one member per trigger type. */
export const ZWorkflowTriggerRunPayload = z
  .discriminatedUnion("type", [
    ZWorkflowResponseCompletedTriggerPayload.extend(ZTriggeredAt),
    ZWorkflowWebhookTriggerPayload.extend(ZTriggeredAt),
  ])
  .describe("Trigger payload snapshot stored with a workflow run.");
export type TWorkflowTriggerRunPayload = z.infer<typeof ZWorkflowTriggerRunPayload>;

export const ZWorkflowRunLogInput = z.record(z.string(), z.unknown());
export type TWorkflowRunLogInput = z.infer<typeof ZWorkflowRunLogInput>;

export const ZWorkflowRunLogOutput = z.record(z.string(), z.unknown());
export type TWorkflowRunLogOutput = z.infer<typeof ZWorkflowRunLogOutput>;

export const ZWorkflowStepResult = z.object({
  stepId: z.string().min(1),
  stepType: z.string().min(1),
  status: ZWorkflowRunLogStatus,
  input: ZWorkflowRunLogInput.optional(),
  output: ZWorkflowRunLogOutput.optional(),
  error: z.string().optional(),
  startedAt: ZIsoDateTime.optional(),
  finishedAt: ZIsoDateTime.optional(),
});
export type TWorkflowStepResult = z.infer<typeof ZWorkflowStepResult>;

/** A step on an If/else branch the run did not take, shown as "Not taken" in run detail (ENG-7170). */
export const ZWorkflowRunNotTakenStep = z.object({
  stepId: z.string().min(1),
  stepType: z.string().min(1),
});
export type TWorkflowRunNotTakenStep = z.infer<typeof ZWorkflowRunNotTakenStep>;

export const ZWorkflowRunData = z
  .object({
    trigger: ZWorkflowTriggerRunPayload.optional(),
    steps: z.array(ZWorkflowStepResult).default([]),
    notTaken: z.array(ZWorkflowRunNotTakenStep).optional(),
  })
  .catchall(z.unknown());
export type TWorkflowRunData = z.infer<typeof ZWorkflowRunData>;

export const ZWorkflowVersion = z
  .object({
    id: z.cuid2(),
    workflowId: z.cuid2(),
    workspaceId: z.cuid2(),
    version: z.number().int().positive().describe("Monotonic workflow version number."),
    definition: ZWorkflowExecutableDefinition.describe("Immutable executable definition snapshot."),
    publishedAt: ZIsoDateTime,
    publishedBy: z.cuid2().nullable().optional(),
  })
  .describe("Immutable workflow definition snapshot used by runs.");
export type TWorkflowVersion = z.infer<typeof ZWorkflowVersion>;

export const ZWorkflowRunLog = z
  .object({
    id: z.cuid2(),
    runId: z.cuid2(),
    sequence: z.number().int().nonnegative(),
    stepId: z.string().min(1),
    stepType: z.string().min(1),
    status: ZWorkflowRunLogStatus,
    input: ZWorkflowRunLogInput.optional(),
    output: ZWorkflowRunLogOutput.optional(),
    error: z.string().nullable().optional(),
    startedAt: ZIsoDateTime.nullable().optional(),
    finishedAt: ZIsoDateTime.nullable().optional(),
  })
  .describe("Persisted trace entry for one workflow run step.");
export type TWorkflowRunLog = z.infer<typeof ZWorkflowRunLog>;
