/**
 * Shared task-domain result shapes for the operations layer.
 *
 * Defines types that are:
 *  - produced by core functions in `packages/core/src/tasks/`
 *  - consumed by the dispatch layer
 *  - exposed as `Result` types in `operations/tasks.ts`
 *
 * These types are the single source of truth for the wire-format shapes.
 * Core functions import them here (via `@cleocode/contracts`) rather than
 * defining inline interfaces.
 *
 * @module tasks
 * @task T1703 — Fill 20 Result=unknown stubs in operations/tasks.ts
 * @epic T1688
 */
import { z } from 'zod';
import { TASK_STATUSES } from './status-registry.js';
const taskMutationWarningSeveritySchema = z.enum(['info', 'warning']);
/** Zod schema for structured task mutation warnings. @task T10608 */
export const taskMutationWarningSchema = z.object({
    code: z.string().min(1),
    message: z.string().min(1),
    severity: taskMutationWarningSeveritySchema.optional(),
    taskId: z.string().min(1).optional(),
    field: z.string().min(1).optional(),
    index: z.number().int().nonnegative().optional(),
});
/** Zod schema for the standardized task mutation dry-run/preflight summary. @task T10608 */
export const taskMutationDryRunSummarySchema = z.object({
    dryRun: z.literal(true),
    wouldCreate: z.number().int().nonnegative(),
    wouldUpdate: z.number().int().nonnegative(),
    wouldDelete: z.number().int().nonnegative(),
    wouldAffect: z.number().int().nonnegative(),
    validatedCount: z.number().int().nonnegative(),
    insertedCount: z.literal(0),
    updatedCount: z.literal(0),
    deletedCount: z.literal(0),
    warnings: z.array(taskMutationWarningSchema),
});
const taskMutationTaskRecordSchema = z.object({ id: z.string().min(1) }).passthrough();
/** Zod schema for the standardized task mutation envelope. @task T10608 */
export const taskMutationEnvelopeSchema = z.object({
    dryRun: z.boolean().optional(),
    created: z.array(taskMutationTaskRecordSchema),
    updated: z.array(taskMutationTaskRecordSchema),
    deleted: z.array(taskMutationTaskRecordSchema),
    affectedCount: z.number().int().nonnegative(),
    mutationWarnings: z.array(taskMutationWarningSchema),
    dryRunSummary: taskMutationDryRunSummarySchema.optional(),
});
const completionTaskStatusSchema = z.enum(TASK_STATUSES);
const completionCriterionKindSchema = z.enum(['text', 'evidence_bound', 'child_task']);
const completionCriterionStatusSchema = z.enum(['satisfied', 'unsatisfied', 'waived', 'replaced']);
const completionBlockerReasonSchema = z.enum([
    'missing_evidence_binding',
    'child_not_done',
    'child_cancelled_requires_waiver',
    'child_replacement_not_done',
    'child_missing',
    'done_parent_stale',
]);
const completionStaleReasonSchema = z.enum(['done_parent_has_unsatisfied_criteria']);
const completionProjectionRepairErrorCodeSchema = z.enum([
    'projection_not_stale',
    'criteria_missing',
    'binding_target_missing',
    'repair_conflict',
]);
const completionCriterionWaiverSchema = z.object({
    criterionAcId: z.string().min(1),
    childTaskId: z.string().min(1),
    reason: z.string().min(1),
    actor: z.string().min(1),
    waivedAt: z.string().min(1),
});
const completionCriterionReplacementSchema = z.object({
    criterionAcId: z.string().min(1),
    originalChildTaskId: z.string().min(1),
    replacementChildTaskId: z.string().min(1),
    reason: z.string().min(1),
    actor: z.string().min(1),
    replacedAt: z.string().min(1),
});
/** Zod schema for one typed AC completion-result row. @task T10607 */
export const completionCriterionEvaluationSchema = z.object({
    acId: z.string().min(1),
    alias: z.string().min(1),
    text: z.string(),
    kind: completionCriterionKindSchema,
    status: completionCriterionStatusSchema,
    reason: completionBlockerReasonSchema.optional(),
    targetTaskId: z.string().min(1).optional(),
    targetTaskStatus: completionTaskStatusSchema.optional(),
    waiver: completionCriterionWaiverSchema.optional(),
    replacement: completionCriterionReplacementSchema.optional(),
    replacementTaskStatus: completionTaskStatusSchema.optional(),
    evidenceBindings: z.number().int().nonnegative(),
});
/** Zod schema for unsatisfied completion criteria. @task T10607 */
export const unsatisfiedCompletionCriterionSchema = completionCriterionEvaluationSchema.extend({
    status: z.literal('unsatisfied'),
    reason: completionBlockerReasonSchema,
});
const completionTotalsSchema = z.object({
    criteria: z.number().int().nonnegative(),
    satisfied: z.number().int().nonnegative(),
    unsatisfied: z.number().int().nonnegative(),
    waived: z.number().int().nonnegative(),
    replaced: z.number().int().nonnegative(),
});
const completionContextPackSchema = z.object({
    taskId: z.string().min(1),
    generatedAt: z.string().min(1),
    source: z.literal('audit_log'),
    window: z.object({
        limit: z.number().int().positive(),
        since: z.string().min(1).optional(),
        relationDepth: z.number().int().nonnegative(),
        relatedTaskIds: z.array(z.string().min(1)),
    }),
    events: z.array(z.object({
        id: z.string().min(1),
        timestamp: z.string().min(1),
        action: z.enum([
            'task_completed',
            'task_reopened',
            'task_cancelled',
            'task_uncancelled',
            'task_reparented',
            'ac_projection_rebuilt',
        ]),
        taskId: z.string().min(1),
        relation: z.enum(['self', 'parent', 'child', 'sibling', 'related']),
        actor: z.string().min(1),
        details: z.record(z.string(), z.unknown()).optional(),
        before: z.record(z.string(), z.unknown()).optional(),
        after: z.record(z.string(), z.unknown()).optional(),
    })),
    summary: z.object({
        totalEvents: z.number().int().nonnegative(),
        byAction: z.record(z.string(), z.number().int().nonnegative()),
        byRelation: z.record(z.string(), z.number().int().nonnegative()),
        latestEventAt: z.string().nullable(),
    }),
});
/** Zod result contract for `tasks.completion.evaluate`. @task T10607 */
export const completionEvaluationSchema = z.object({
    taskId: z.string().min(1),
    taskStatus: completionTaskStatusSchema,
    ready: z.boolean(),
    stale: z.boolean(),
    staleReasons: z.array(completionStaleReasonSchema),
    contextPack: completionContextPackSchema.optional(),
    satisfied: z.array(completionCriterionEvaluationSchema),
    unsatisfied: z.array(unsatisfiedCompletionCriterionSchema),
    waived: z.array(completionCriterionEvaluationSchema),
    replaced: z.array(completionCriterionEvaluationSchema),
    totals: completionTotalsSchema,
});
/** Zod result contract for `tasks.completion.explain`. @task T10607 */
export const completionExplanationSchema = z.object({
    taskId: z.string().min(1),
    ready: z.boolean(),
    stale: z.boolean(),
    summary: z.string(),
    contextPack: completionContextPackSchema.optional(),
    blockers: z.array(completionCriterionEvaluationSchema),
});
/** Zod params for `tasks.completion.list`. @task T10607 */
export const completionListParamsSchema = z.object({
    taskId: z.string().min(1),
    status: completionCriterionStatusSchema.optional(),
    kind: completionCriterionKindSchema.optional(),
});
/** Zod result contract for `tasks.completion.list`. @task T10607 */
export const completionListResultSchema = z.object({
    taskId: z.string().min(1),
    criteria: z.array(completionCriterionEvaluationSchema),
    totals: completionTotalsSchema,
});
/** Zod params for `tasks.completion.evaluate` and `tasks.completion.explain`. @task T10607 */
export const completionEvaluateParamsSchema = z.object({
    taskId: z.string().min(1),
    includeContext: z.boolean().optional(),
    limit: z.number().int().positive().optional(),
    since: z.string().min(1).optional(),
    relationDepth: z.number().int().nonnegative().optional(),
});
/** Zod params for `tasks.completion.explain`. @task T10607 */
export const completionExplainParamsSchema = completionEvaluateParamsSchema;
/** Zod schema for typed stale-projection repair errors. @task T10607 */
export const completionProjectionRepairErrorSchema = z.object({
    code: completionProjectionRepairErrorCodeSchema,
    message: z.string().min(1),
    taskId: z.string().min(1),
    acId: z.string().min(1).optional(),
    evidenceAtomId: z.string().min(1).optional(),
});
/** Zod params for `tasks.projection.repair`. @task T10607 */
export const completionProjectionRepairParamsSchema = z.object({
    taskId: z.string().min(1),
    dryRun: z.boolean().optional(),
});
/** Zod result contract for `tasks.projection.repair`. @task T10607 */
export const completionProjectionRepairResultSchema = z.object({
    taskId: z.string().min(1),
    repaired: z.boolean(),
    dryRun: z.boolean(),
    staleBefore: z.boolean(),
    staleAfter: z.boolean(),
    errors: z.array(completionProjectionRepairErrorSchema),
});
//# sourceMappingURL=tasks.js.map