/**
 * @sanity/ailf-core — Sink schemas
 *
 * Zod schemas for runtime validation of config/sinks.yaml — the configuration
 * file for report delivery sinks.
 *
 * Each sink type has its own config schema, and the top-level SinksFileSchema
 * validates the complete YAML structure. The discriminated union on `type`
 * ensures type-safe parsing with clear error messages for unknown sink types.
 *
 * Usage:
 *   import { SinksFileSchema } from "./schema.js"
 *   const parsed = SinksFileSchema.parse(rawYaml)
 *
 * @see docs/design-docs/report-store/sink-architecture.md
 */
import { z } from "zod";
// ---------------------------------------------------------------------------
// Sink type enum
// ---------------------------------------------------------------------------
/** All supported sink types as a Zod union. */
export const SinkTypeSchema = z.enum([
    "bigquery",
    "github-comment",
    "slack",
    "webhook",
]);
// ---------------------------------------------------------------------------
// Routing rules — severity-aware delivery targeting (Phase 5c)
// ---------------------------------------------------------------------------
/**
 * Schema for sink routing rules.
 * Controls which messages are delivered based on severity and type.
 * When omitted, the sink receives all reports (backward compatible).
 */
export const SinkRoutingSchema = z.object({
    /** Deliver on critical threshold violations */
    critical: z.union([z.boolean(), z.string()]).optional(),
    /** Deliver weekly digest messages */
    digest: z.union([z.boolean(), z.string()]).optional(),
    /** Deliver on info-level threshold violations */
    info: z.union([z.boolean(), z.string()]).optional(),
    /** Deliver on regression detection */
    regression: z.union([z.boolean(), z.string()]).optional(),
    /** Deliver on warning-level threshold violations */
    warning: z.union([z.boolean(), z.string()]).optional(),
});
/** Inferred TypeScript type for sink routing rules. */
// ---------------------------------------------------------------------------
// Individual sink config schemas
// ---------------------------------------------------------------------------
/** Config schema for a generic webhook sink. */
export const WebhookSinkConfigSchema = z.object({
    enabled: z.boolean().default(false),
    headers: z.record(z.string(), z.string()).optional(),
    routing: SinkRoutingSchema.optional(),
    type: z.literal("webhook"),
    url: z.string().url(),
});
// ---------------------------------------------------------------------------
// Individual sink config schemas
// ---------------------------------------------------------------------------
/** Config schema for a Slack notification sink. */
export const SlackSinkConfigSchema = z.object({
    channel: z.string().optional(),
    enabled: z.boolean().default(false),
    routing: SinkRoutingSchema.optional(),
    type: z.literal("slack"),
    webhookUrl: z.string().url(),
});
/** Config schema for a GitHub PR comment sink. */
export const GitHubCommentSinkConfigSchema = z.object({
    enabled: z.boolean().default(false),
    routing: SinkRoutingSchema.optional(),
    token: z.string().optional(),
    type: z.literal("github-comment"),
});
/** Config schema for a BigQuery sink. */
export const BigQuerySinkConfigSchema = z.object({
    credentials: z.string().optional(),
    dataset: z.string(),
    enabled: z.boolean().default(false),
    project: z.string(),
    routing: SinkRoutingSchema.optional(),
    type: z.literal("bigquery"),
});
// ---------------------------------------------------------------------------
// Discriminated union across all sink types
// ---------------------------------------------------------------------------
/**
 * A single sink configuration — discriminated on the `type` field.
 *
 * Zod v4 discriminatedUnion provides clear errors when the `type` value
 * doesn't match any known sink.
 */
export const SinkConfigSchema = z.discriminatedUnion("type", [
    BigQuerySinkConfigSchema,
    GitHubCommentSinkConfigSchema,
    SlackSinkConfigSchema,
    WebhookSinkConfigSchema,
]);
// ---------------------------------------------------------------------------
// Top-level file schema (config/sinks.yaml)
// ---------------------------------------------------------------------------
/** Schema for the complete config/sinks.yaml file. */
export const SinksFileSchema = z.object({
    sinks: z.array(SinkConfigSchema).default([]),
});
