/**
 * @sanity/ailf-core — Schedule schemas
 *
 * Zod schemas for runtime validation of config/schedules.yaml — the
 * configuration file for scheduled evaluation runs and digests.
 *
 * Each schedule defines a recurring pipeline run with source, mode,
 * area/task filters, and publishing options. The digest section controls
 * periodic summary notifications.
 *
 * @see docs/design-docs/report-store/implementation.md — Phase 5
 */
import { z } from "zod";
import { RAW_EVAL_MODES } from "../../ailf-shared/index.js";
// ---------------------------------------------------------------------------
// Cron expression validation
// ---------------------------------------------------------------------------
/**
 * Validates a basic cron expression (5 fields: min hour dom month dow).
 * Accepts standard GitHub Actions cron syntax.
 */
const CronSchema = z
    .string()
    .regex(/^[\d*,/-]+\s+[\d*,/-]+\s+[\d*,/-]+\s+[\d*,/-]+\s+[\d*,/-]+$/, "Invalid cron expression — expected 5 space-separated fields");
// ---------------------------------------------------------------------------
// Schedule entry
// ---------------------------------------------------------------------------
/** Schema for a single scheduled evaluation run. */
export const ScheduleEntrySchema = z.object({
    /** Feature areas to evaluate (blank = all areas) */
    areas: z.array(z.string()).optional(),
    /** Whether to auto-compare against the most recent baseline */
    compare: z.boolean().default(false),
    /** Cron expression (UTC) for when this schedule runs */
    cron: CronSchema,
    /** Whether this schedule is active */
    enabled: z.boolean().default(true),
    /**
     * Evaluation mode — accepts both canonical and legacy names.
     * Legacy names must pass through normalizeMode() before entering typed pipeline code.
     */
    mode: z.enum(RAW_EVAL_MODES).default("baseline"),
    /** Human-readable schedule name (used as report tag) */
    name: z
        .string()
        .min(1)
        .regex(/^[a-z][a-z0-9-]*$/, "Schedule name must be lowercase kebab-case"),
    /** Whether to publish the report to the Sanity store + sinks */
    publish: z.boolean().default(true),
    /** Documentation source to evaluate */
    source: z.string().default("production"),
    /** Specific task IDs to evaluate (blank = all tasks) */
    tasks: z.array(z.string()).optional(),
});
// ---------------------------------------------------------------------------
// Digest configuration
// ---------------------------------------------------------------------------
/** Schema for the weekly digest section. */
export const DigestConfigSchema = z.object({
    /** Cron expression for when the digest is sent */
    cron: CronSchema.optional(),
    /** Whether the digest is enabled */
    enabled: z.boolean().default(false),
    /** How many days of reports to include in the digest */
    lookbackDays: z.number().int().min(1).max(90).default(7),
    /** Slack webhook URL (supports env var interpolation) */
    slackWebhookUrl: z.string().optional(),
});
// ---------------------------------------------------------------------------
// Top-level file schema (config/schedules.yaml)
// ---------------------------------------------------------------------------
/** Schema for the complete config/schedules.yaml file. */
export const SchedulesFileSchema = z.object({
    /** Digest aggregation settings */
    digest: DigestConfigSchema.optional(),
    /** Scheduled evaluation runs */
    schedules: z.array(ScheduleEntrySchema).default([]),
});
