/**
 * Convex validators for workflows domain
 *
 * This file re-exports validators from sub-domain folders:
 * - definitions/validators.ts
 * - steps/validators.ts
 * - executions/validators.ts
 * - processing_records/validators.ts
 */

import { v } from 'convex/values';

import {
  jsonRecordValidator,
  jsonValueValidator,
} from '../lib/validators/json';
// From workflow_engine
import { stepConfigValidator } from '../workflow_engine/types/nodes';

// =============================================================================
// WORKFLOW DEFINITIONS VALIDATORS
// =============================================================================

export const workflowStatusValidator = v.union(
  v.literal('draft'),
  v.literal('active'),
  v.literal('archived'),
);

export const workflowTypeValidator = v.literal('predefined');

export const retryPolicyValidator = v.object({
  maxRetries: v.number(),
  backoffMs: v.number(),
});

export const secretConfigValidator = v.object({
  kind: v.literal('inlineEncrypted'),
  cipherText: v.string(),
  keyId: v.optional(v.string()),
});

export const workflowConfigValidator = v.optional(
  v.object({
    timeout: v.optional(v.number()),
    retryPolicy: v.optional(retryPolicyValidator),
    variables: v.optional(v.record(v.string(), jsonValueValidator)),
    secrets: v.optional(v.record(v.string(), secretConfigValidator)),
  }),
);

export const workflowUpdateValidator = v.object({
  name: v.optional(v.string()),
  description: v.optional(v.string()),
  category: v.optional(v.string()),
  config: v.optional(workflowConfigValidator),
  metadata: v.optional(jsonRecordValidator),
});

// =============================================================================
// WORKFLOW EXECUTIONS VALIDATORS (re-export from executions/validators.ts)
// =============================================================================

export {
  executionSortOrderValidator,
  updateExecutionStatusArgsValidator,
  completeExecutionArgsValidator,
  failExecutionArgsValidator,
  patchExecutionArgsValidator,
  resumeExecutionArgsValidator,
  setComponentWorkflowArgsValidator,
  updateExecutionMetadataArgsValidator,
  updateExecutionVariablesArgsValidator,
  listExecutionsArgsValidator,
  listExecutionsCursorArgsValidator,
} from './executions/validators';

// =============================================================================
// WORKFLOW STEP DEFINITIONS VALIDATORS
// =============================================================================

export const stepTypeValidator = v.union(
  v.literal('start'),
  v.literal('trigger'),
  v.literal('llm'),
  v.literal('condition'),
  v.literal('action'),
  v.literal('loop'),
  v.literal('output'),
);

export const wfStepDefDocValidator = v.object({
  _id: v.id('wfStepDefs'),
  _creationTime: v.number(),
  organizationId: v.string(),
  wfDefinitionId: v.id('wfDefinitions'),
  stepSlug: v.string(),
  name: v.string(),
  description: v.optional(v.string()),
  stepType: stepTypeValidator,
  order: v.number(),
  nextSteps: v.record(v.string(), v.string()),
  config: stepConfigValidator,
  // @deprecated — unused legacy fields, kept for schema compatibility
  inputMapping: v.optional(v.record(v.string(), jsonValueValidator)),
  outputMapping: v.optional(v.record(v.string(), jsonValueValidator)),
  metadata: v.optional(jsonRecordValidator),
});

export const getOrderedStepsArgsValidator = {
  wfDefinitionId: v.id('wfDefinitions'),
};

export const listWorkflowStepsArgsValidator = {
  wfDefinitionId: v.id('wfDefinitions'),
};

export const createStepArgsValidator = {
  wfDefinitionId: v.id('wfDefinitions'),
  stepSlug: v.string(),
  name: v.string(),
  stepType: stepTypeValidator,
  config: stepConfigValidator,
  nextSteps: v.record(v.string(), v.string()),
  organizationId: v.string(),
};

export const deleteStepArgsValidator = {
  stepRecordId: v.id('wfStepDefs'),
};

export const updateStepArgsValidator = {
  stepRecordId: v.id('wfStepDefs'),
  updates: v.object({
    name: v.optional(v.string()),
    description: v.optional(v.string()),
    config: v.optional(stepConfigValidator),
    nextSteps: v.optional(v.record(v.string(), v.string())),
    metadata: v.optional(jsonRecordValidator),
  }),
};
