export interface FormFieldOption {
  label: string
  value: string | number
}

export interface FormField {
  name: string
  label: string
  type?: 'text' | 'number' | 'date' | 'select' | 'textarea'
  required?: boolean
  multiple?: boolean
  // 兼容旧数据：早期 options 为字符串数组，新数据为 { label, value } 数组
  options?: FormFieldOption[] | string[]
}

export interface FlowDefinition {
  id: number
  code: string
  name: string
  description?: string
  category?: string
  version: number
  status: number // 0-草稿 1-已发布 2-停用
  formSchema?: string
  formFields?: FormField[]
  nodeFieldName?: string
  ownerFieldName?: string
  bizFieldName?: string
  flowJson?: string
  createTime?: string
  updateTime?: string
}

export interface FlowInstance {
  id: number
  definitionId: number
  definitionName?: string
  title?: string
  bizValue?: string
  instanceNo?: string
  businessKey?: string
  status: number // 0-待接收, 1-运行中, 2-已通过, 3-已拒绝, 4-已回退, 5-已完成, 6-已撤回, 7-已终止
  startTime?: string
  endTime?: string
  currentNode?: string
  currentNodeName?: string
  attachmentUrls?: string
  formData?: string
  createBy?: string
}

export interface FlowTask {
  id: number
  instanceId: number
  definitionName?: string
  instanceTitle?: string
  instanceNo?: string
  nodeName: string
  nodeType?: string
  assigneeId?: number
  assigneeType?: string
  assigneeName?: string
  status: number // 0-待处理, 1-已处理, 2-已转办, 3-已跳过, 4-已回退
  taskStatus?: number
  action?: string
  comment?: string
  attachmentUrls?: string
  createTime?: string
  endTime?: string
  timeoutTime?: string
  urgency?: number // 0-正常，1-即将超时，2-已超时
  remainingMinutes?: number
  formData?: string
  formSchema?: string
}

export interface FlowNodeConfig {
  id: string
  type: 'start' | 'approval' | 'cc' | 'condition' | 'end'
  name: string
  x: number
  y: number
  properties?: Record<string, unknown>
}

export interface FlowSubNodeConfig {
  id: string
  name: string
  assigneeValue?: string
  approveMode?: 'or' | 'and'
  timeoutHours?: number
  timeoutAction?: 'remind' | 'pass' | 'reject'
}

export interface FlowNodeProperties {
  assigneeType?: 'ROLE'
  assigneeValue?: string
  approveMode?: 'or' | 'and'
  timeoutHours?: number
  timeoutAction?: 'remind' | 'pass' | 'reject'
  approveTimeField?: string
  ccUsers?: string
  subNodesEnabled?: boolean
  subNodeMode?: 'sequential' | 'parallel'
  subNodes?: FlowSubNodeConfig[]
}

export interface FlowEdgeConfig {
  id: string
  sourceNodeId: string
  targetNodeId: string
  properties?: Record<string, unknown>
}

export interface NextNode {
  nodeId: string
  nodeName: string
  nodeType: string
  branchName?: string
  condition?: string
}

export interface ApprovalAction {
  action: 'pass' | 'reject' | 'rollback' | 'transfer'
  comment?: string
  transferTo?: number
  targetNodeId?: string
  attachmentUrls?: string
}

export interface SubNodeProgress {
  subNodeId: string
  subNodeName: string
  status: 'completed' | 'current' | 'pending'
  tasks: FlowTask[]
  isMyTurn: boolean
}

export interface NodeProgress {
  nodeId: string
  nodeName: string
  nodeType: string
  status: 'completed' | 'current' | 'pending'
  tasks: FlowTask[]
  isMyTurn: boolean
  subNodes?: SubNodeProgress[]
}

export interface ProcessProgress {
  instance: FlowInstance
  definition: FlowDefinition
  nodes: NodeProgress[]
  records: ApprovalRecord[]
  remainingNodeCount: number
}

export interface ApprovalRecord {
  id?: number
  taskId: number
  instanceId: number
  nodeId: string
  nodeName: string
  operatorId: number
  operatorName?: string
  actionType: string
  actionResult: string
  comment?: string
  attachmentUrls?: string
  createTime?: string
}

export interface Attachment {
  id: number
  instanceId: number
  taskId?: number
  recordId?: number
  nodeId?: string
  nodeName?: string
  fileName: string
  fileUrl: string
  fileSize?: number
  uploaderId?: number
  uploaderName?: string
  uploaderType?: string
  createTime?: string
}
