import type { OriginTracking } from '../../../execution/core/types'
import type { TimeRange } from '../../../operations/observability/types'
import type { ActionConfig } from './action'

export type { TimeRange }

/**
 * Command queue task with flexible action system
 */
export interface Task extends OriginTracking {
  id: string
  organizationId: string

  // NEW: Flexible action system
  actions: ActionConfig[]
  context: unknown
  selectedAction?: string
  actionPayload?: unknown

  // Task metadata
  description?: string
  priority: number

  /** Optional checkpoint identifier for grouping related human approval tasks */
  humanCheckpoint?: string

  // Status (updated to include 'completed')
  status: QueueTaskStatus

  /**
   * Target resource tracking — mirrors origin columns.
   * Set when task is created; patchable to redirect execution to a different resource.
   */
  targetResourceId?: string
  targetResourceType?: 'agent' | 'workflow'

  /**
   * Execution ID for the action that runs AFTER user approval.
   * NULL until execution starts.
   *
   * Naming distinction:
   * - originExecutionId = Parent execution that CREATED the HITL task
   * - targetExecutionId = Child execution that RUNS AFTER user approval
   */
  targetExecutionId?: string

  createdAt: Date
  completedAt?: Date
  completedBy?: string
  expiresAt?: Date
  idempotencyKey?: string | null
}

/**
 * Task status values
 * - pending: awaiting action
 * - processing: execution in progress after user approval
 * - completed: action was taken and execution succeeded
 * - failed: execution failed, task can be retried
 * - expired: timed out before action
 */
export type QueueTaskStatus = 'pending' | 'processing' | 'completed' | 'failed' | 'expired'

/**
 * Parameters for creating a new HITL task
 */
export interface CreateTaskParams extends OriginTracking {
  organizationId: string
  actions: ActionConfig[]
  context: unknown
  description?: string
  priority?: number
  expiresAt?: Date
  humanCheckpoint?: string // Optional checkpoint identifier for grouping related human approval tasks
  metadata?: Record<string, unknown>
  idempotencyKey?: string
}

/**
 * Parameters for submitting an action on a task
 */
export interface SubmitActionParams {
  actionId: string
  payload?: unknown
  notes?: string
  userId: string
}

/**
 * Parameters for patching mutable metadata on a task
 */
export interface PatchTaskParams {
  humanCheckpoint?: string | null
  description?: string
  priority?: number
  context?: Record<string, unknown>
  actions?: unknown[]
  targetResourceId?: string | null
  targetResourceType?: 'agent' | 'workflow' | null
  targetExecutionId?: string
  status?: 'pending' | 'failed' | 'completed'
}

/**
 * Filters for listing tasks
 */
export interface ListTasksFilters {
  organizationId: string
  status?: QueueTaskStatus
  humanCheckpoint?: string
  timeRange?: TimeRange
  priorityMin?: number
  priorityMax?: number
  limit?: number
  offset?: number
}

export interface CommandQueueListResponse {
  tasks: Task[]
  total: number
}
