import { z } from "zod";
import { withFormatter } from "../utils/schema-utils";
// Open enum: known values listed for docs / TS autocomplete (when the
// doc-gen / type-trick tooling consumes the union AST), plus a string
// fallback so a new server-side state doesn't crash the parse.
export const TriggerMessageStatusSchema = z
    .union([z.enum(["available", "leased", "acked", "quarantined"]), z.string()])
    .describe("Message lifecycle status");
export const TriggerMessageAttributesSchema = z.object({
    lease_count: z.number().int(),
    error_message: z.string().nullable(),
    possible_duplicate_data: z.boolean(),
});
const TriggerMessageItemSchemaBase = z.object({
    id: z.string(),
    created_at: z.string(),
    status: TriggerMessageStatusSchema,
    message_attributes: TriggerMessageAttributesSchema,
});
export const TriggerMessageItemSchema = withFormatter(TriggerMessageItemSchemaBase, {
    format: (item) => {
        const details = [
            { text: `Status: ${item.status}`, style: "dim" },
            {
                text: `Lease count: ${item.message_attributes.lease_count}`,
                style: "dim",
            },
        ];
        if (item.message_attributes.error_message) {
            details.push({
                text: `Error: ${item.message_attributes.error_message}`,
                style: "warning",
            });
        }
        if (item.message_attributes.possible_duplicate_data) {
            details.push({
                text: "Possible duplicate data",
                style: "warning",
            });
        }
        return {
            title: `Message ${item.id}`,
            id: item.id,
            details,
        };
    },
});
const LeasedTriggerMessageItemSchemaBase = TriggerMessageItemSchemaBase.extend({
    payload: z.record(z.string(), z.unknown()),
});
export const LeasedTriggerMessageItemSchema = withFormatter(LeasedTriggerMessageItemSchemaBase, {
    format: (item) => {
        const details = [
            { text: `Status: ${item.status}`, style: "dim" },
            {
                text: `Lease count: ${item.message_attributes.lease_count}`,
                style: "dim",
            },
        ];
        if (item.message_attributes.error_message) {
            details.push({
                text: `Error: ${item.message_attributes.error_message}`,
                style: "warning",
            });
        }
        if (item.message_attributes.possible_duplicate_data) {
            details.push({
                text: "Possible duplicate data",
                style: "warning",
            });
        }
        return {
            title: `Message ${item.id}`,
            id: item.id,
            data: item.payload,
            details,
        };
    },
});
