import { Annotation } from '@langchain/langgraph';
/**
 * State annotation for the feature-agent graph.
 *
 * Uses LangGraph's Annotation API to define state channels.
 * The `messages` channel uses a reducer to accumulate messages
 * from all nodes as the graph executes.
 */
export const FeatureAgentAnnotation = Annotation.Root({
    featureId: (Annotation),
    repositoryPath: (Annotation),
    specDir: (Annotation),
    worktreePath: (Annotation),
    currentNode: (Annotation),
    error: Annotation({
        reducer: (prev, next) => (next !== undefined ? next : prev),
        default: () => null,
    }),
    approvalGates: Annotation({
        reducer: (prev, next) => next ?? prev,
        default: () => undefined,
    }),
    model: Annotation({
        reducer: (_prev, next) => next ?? _prev,
        default: () => undefined,
    }),
    messages: Annotation({
        reducer: (prev, next) => [...prev, ...next],
        default: () => [],
    }),
    // --- Validation state channels (for validate/repair loops) ---
    validationRetries: Annotation({
        reducer: (_prev, next) => next,
        default: () => 0,
    }),
    lastValidationTarget: Annotation({
        reducer: (_prev, next) => next,
        default: () => '',
    }),
    lastValidationErrors: Annotation({
        reducer: (_prev, next) => next,
        default: () => [],
    }),
    // --- Approval action channels (set by Command({update}) on resume) ---
    _approvalAction: Annotation({
        reducer: (_prev, next) => (next !== undefined ? next : _prev),
        default: () => null,
    }),
    _rejectionFeedback: Annotation({
        reducer: (_prev, next) => (next !== undefined ? next : _prev),
        default: () => null,
    }),
    _needsReexecution: Annotation({
        reducer: (_prev, next) => next,
        default: () => false,
    }),
    // --- Merge and workflow state channels ---
    prUrl: Annotation({
        reducer: (_prev, next) => (next !== undefined ? next : _prev),
        default: () => null,
    }),
    prNumber: Annotation({
        reducer: (_prev, next) => (next !== undefined ? next : _prev),
        default: () => null,
    }),
    commitHash: Annotation({
        reducer: (_prev, next) => (next !== undefined ? next : _prev),
        default: () => null,
    }),
    ciStatus: Annotation({
        reducer: (_prev, next) => (next !== undefined ? next : _prev),
        default: () => null,
    }),
    push: Annotation({
        reducer: (_prev, next) => next,
        default: () => false,
    }),
    openPr: Annotation({
        reducer: (_prev, next) => next,
        default: () => false,
    }),
    forkAndPr: Annotation({
        reducer: (_prev, next) => next,
        default: () => false,
    }),
    commitSpecs: Annotation({
        reducer: (_prev, next) => next,
        default: () => true,
    }),
    ciWatchEnabled: Annotation({
        reducer: (_prev, next) => next,
        default: () => true,
    }),
    enableEvidence: Annotation({
        reducer: (_prev, next) => next,
        default: () => false,
    }),
    commitEvidence: Annotation({
        reducer: (_prev, next) => next,
        default: () => false,
    }),
    // --- Evidence state ---
    evidence: Annotation({
        reducer: (prev, next) => [...prev, ...next],
        default: () => [],
    }),
    evidenceRetries: Annotation({
        reducer: (_prev, next) => next,
        default: () => 0,
    }),
    // --- Resume context (set when resuming from error/crash/stop) ---
    resumeReason: Annotation({
        reducer: (_prev, next) => next ?? _prev,
        default: () => undefined,
    }),
    // --- CI watch/fix loop state ---
    ciFixAttempts: Annotation({
        reducer: (_prev, next) => next,
        default: () => 0,
    }),
    ciFixHistory: Annotation({
        reducer: (prev, next) => [...prev, ...next],
        default: () => [],
    }),
    ciFixStatus: Annotation({
        reducer: (_prev, next) => next,
        default: () => 'idle',
    }),
});
