import type { DiffAnalysis, WorkflowDiff } from '@/lib/workflows/diff'
import type { WorkflowState } from '../workflows/workflow/types'

export interface WorkflowDiffState {
  hasActiveDiff: boolean
  isShowingDiff: boolean
  isDiffReady: boolean
  baselineWorkflow: WorkflowState | null
  baselineWorkflowId: string | null
  diffAnalysis: DiffAnalysis | null
  diffMetadata: WorkflowDiff['metadata'] | null
  diffError?: string | null
  pendingExternalUpdates: Record<string, number>
  remoteUpdateVersions: Record<string, number>
  reconcilingWorkflows: Record<string, boolean>
  reconciliationErrors: Record<string, string>
  _triggerMessageId?: string | null
}

interface DiffActionOptions {
  /** Skip recording this operation for undo/redo. Used during undo/redo replay. */
  skipRecording?: boolean
  /** Skip persisting to DB. Use when the server tool already saved (e.g. edit_workflow). */
  skipPersist?: boolean
  /**
   * Explicit baseline snapshot to diff against.
   * Use this when the proposed state is fetched asynchronously and the live
   * workflow store may have already been updated to that same state.
   */
  baselineWorkflow?: WorkflowState
}

export interface WorkflowDiffActions {
  setProposedChanges: (
    workflowState: WorkflowState,
    diffAnalysis?: DiffAnalysis,
    options?: DiffActionOptions
  ) => Promise<void>
  clearDiff: (options?: { restoreBaseline?: boolean }) => void
  toggleDiffView: () => void
  acceptChanges: (options?: DiffActionOptions) => Promise<void>
  rejectChanges: (options?: DiffActionOptions) => Promise<void>
  reapplyDiffMarkers: () => void
  markRemoteUpdateSeen: (workflowId: string) => void
  markExternalUpdatePending: (workflowId: string) => void
  clearExternalUpdatePending: (workflowId: string) => void
  setWorkflowReconciliationInProgress: (workflowId: string, isReconciling: boolean) => void
  setWorkflowReconciliationError: (workflowId: string, error: string | null) => void
  _batchedStateUpdate: (updates: Partial<WorkflowDiffState>) => void
}
