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 value doesn't crash the parse.
export const TriggerInboxStatusSchema = z
    .union([
    z.enum([
        "initializing",
        "active",
        "paused",
        "deleting",
        "initialization_failure",
    ]),
    z.string(),
])
    .describe("Inbox lifecycle status");
// Open enum: trigger ops can ship new reasons (rate_limited,
// quota_exceeded, etc.) without an SDK release.
export const TriggerInboxPausedReasonSchema = z
    .union([
    z.enum([
        "user",
        "authentication",
        "authentication_access_revoked",
        "partner_revoked",
        "subscribe_failed",
        "migrate_failed",
        "abandoned",
        "unknown",
        "upstream_failures",
    ]),
    z.string(),
])
    .describe("Why the inbox was paused, if applicable");
export const TriggerSubscriptionApiSchema = z.object({
    connection_id: z.union([z.string(), z.number(), z.null()]),
    app_key: z.string(),
    action_key: z.string(),
    inputs: z.record(z.string(), z.unknown()),
});
const TriggerInboxItemSchemaBase = z.object({
    id: z.string(),
    created_at: z.string(),
    name: z.string().nullable(),
    status: TriggerInboxStatusSchema,
    paused_reason: TriggerInboxPausedReasonSchema.nullable(),
    notification_url: z.string().nullable(),
    subscription: TriggerSubscriptionApiSchema,
});
export const TriggerInboxItemSchema = withFormatter(TriggerInboxItemSchemaBase, {
    format: (item) => {
        const details = [
            {
                text: `Status: ${item.status}`,
                style: item.status === "active" ? "accent" : "dim",
            },
            {
                text: `Subscription: ${item.subscription.app_key} / ${item.subscription.action_key}`,
                style: "dim",
            },
        ];
        if (item.paused_reason) {
            details.push({
                text: `Paused reason: ${item.paused_reason}`,
                style: "warning",
            });
        }
        if (item.notification_url) {
            details.push({
                text: `Notify: ${item.notification_url}`,
                style: "dim",
            });
        }
        return {
            title: item.name ?? `Trigger Inbox ${item.id}`,
            id: item.id,
            details,
        };
    },
});
