import { WorkflowEvent } from "../data/entities.js";
const WorkflowEventTypes = {
  // Workflow lifecycle
  WORKFLOW_STARTED: "WORKFLOW_STARTED",
  WORKFLOW_COMPLETED: "WORKFLOW_COMPLETED",
  WORKFLOW_FAILED: "WORKFLOW_FAILED",
  WORKFLOW_CANCELLED: "WORKFLOW_CANCELLED",
  WORKFLOW_PAUSED: "WORKFLOW_PAUSED",
  WORKFLOW_RESUMED: "WORKFLOW_RESUMED",
  // Step lifecycle
  STEP_ENTERED: "STEP_ENTERED",
  STEP_EXITED: "STEP_EXITED",
  STEP_FAILED: "STEP_FAILED",
  STEP_SKIPPED: "STEP_SKIPPED",
  // Transition events
  TRANSITION_EXECUTED: "TRANSITION_EXECUTED",
  TRANSITION_REJECTED: "TRANSITION_REJECTED",
  TRANSITION_POST_CONDITION_FAILED: "TRANSITION_POST_CONDITION_FAILED",
  TRANSITION_FAILED: "TRANSITION_FAILED",
  // Activity events
  ACTIVITY_SCHEDULED: "ACTIVITY_SCHEDULED",
  ACTIVITY_STARTED: "ACTIVITY_STARTED",
  ACTIVITY_COMPLETED: "ACTIVITY_COMPLETED",
  ACTIVITY_FAILED: "ACTIVITY_FAILED",
  ACTIVITY_RETRY: "ACTIVITY_RETRY",
  // User task events
  USER_TASK_CREATED: "USER_TASK_CREATED",
  USER_TASK_ASSIGNED: "USER_TASK_ASSIGNED",
  USER_TASK_STARTED: "USER_TASK_STARTED",
  USER_TASK_COMPLETED: "USER_TASK_COMPLETED",
  USER_TASK_CANCELLED: "USER_TASK_CANCELLED",
  USER_TASK_ESCALATED: "USER_TASK_ESCALATED",
  // Sub-workflow events (Phase 8)
  SUB_WORKFLOW_STARTED: "SUB_WORKFLOW_STARTED",
  SUB_WORKFLOW_COMPLETED: "SUB_WORKFLOW_COMPLETED",
  SUB_WORKFLOW_FAILED: "SUB_WORKFLOW_FAILED",
  // Compensation events (Phase 8)
  COMPENSATION_STARTED: "COMPENSATION_STARTED",
  COMPENSATION_COMPLETED: "COMPENSATION_COMPLETED",
  COMPENSATION_PARTIAL: "COMPENSATION_PARTIAL",
  COMPENSATION_FAILED: "COMPENSATION_FAILED",
  COMPENSATION_ACTIVITY_STARTED: "COMPENSATION_ACTIVITY_STARTED",
  COMPENSATION_ACTIVITY_COMPLETED: "COMPENSATION_ACTIVITY_COMPLETED",
  COMPENSATION_ACTIVITY_FAILED: "COMPENSATION_ACTIVITY_FAILED",
  // Signal events (Phase 9)
  SIGNAL_AWAITING: "SIGNAL_AWAITING",
  SIGNAL_RECEIVED: "SIGNAL_RECEIVED",
  SIGNAL_TIMEOUT: "SIGNAL_TIMEOUT",
  // Timer events (Phase 9)
  TIMER_AWAITING: "TIMER_AWAITING",
  TIMER_FIRED: "TIMER_FIRED",
  TIMER_CANCELLED: "TIMER_CANCELLED"
};
async function logWorkflowEvent(em, event) {
  const workflowEvent = em.create(WorkflowEvent, {
    workflowInstanceId: event.workflowInstanceId,
    stepInstanceId: event.stepInstanceId || null,
    branchInstanceId: event.branchInstanceId ?? null,
    eventType: event.eventType,
    eventData: event.eventData || {},
    userId: event.userId || null,
    tenantId: event.tenantId,
    organizationId: event.organizationId,
    occurredAt: /* @__PURE__ */ new Date()
  });
  await em.persist(workflowEvent).flush();
  return workflowEvent;
}
async function logWorkflowEvents(em, events) {
  const workflowEvents = events.map(
    (event) => em.create(WorkflowEvent, {
      workflowInstanceId: event.workflowInstanceId,
      stepInstanceId: event.stepInstanceId || null,
      branchInstanceId: event.branchInstanceId ?? null,
      eventType: event.eventType,
      eventData: event.eventData || {},
      userId: event.userId || null,
      tenantId: event.tenantId,
      organizationId: event.organizationId,
      occurredAt: /* @__PURE__ */ new Date()
    })
  );
  await em.persist(workflowEvents).flush();
  return workflowEvents;
}
async function getWorkflowEvents(em, instanceId, options) {
  const where = {
    workflowInstanceId: instanceId
  };
  if (options?.eventTypes && options.eventTypes.length > 0) {
    where.eventType = { $in: options.eventTypes };
  }
  if (options?.stepInstanceId) {
    where.stepInstanceId = options.stepInstanceId;
  }
  if (options?.fromDate || options?.toDate) {
    where.occurredAt = {};
    if (options.fromDate) {
      where.occurredAt.$gte = options.fromDate;
    }
    if (options.toDate) {
      where.occurredAt.$lte = options.toDate;
    }
  }
  const events = await em.find(
    WorkflowEvent,
    where,
    {
      orderBy: { occurredAt: "ASC" },
      limit: options?.limit,
      offset: options?.offset
    }
  );
  return events;
}
async function getStepEvents(em, stepInstanceId) {
  const events = await em.find(
    WorkflowEvent,
    { stepInstanceId },
    { orderBy: { occurredAt: "ASC" } }
  );
  return events;
}
async function getLatestEvent(em, instanceId, eventType) {
  const where = {
    workflowInstanceId: instanceId
  };
  if (eventType) {
    where.eventType = eventType;
  }
  const event = await em.findOne(
    WorkflowEvent,
    where,
    { orderBy: { occurredAt: "DESC" } }
  );
  return event;
}
async function countEvents(em, instanceId, eventType) {
  const where = {
    workflowInstanceId: instanceId
  };
  if (eventType) {
    where.eventType = eventType;
  }
  return await em.count(WorkflowEvent, where);
}
async function getEventStatistics(em, instanceId) {
  const events = await em.find(
    WorkflowEvent,
    { workflowInstanceId: instanceId },
    { orderBy: { occurredAt: "ASC" } }
  );
  const eventsByType = {};
  for (const event of events) {
    eventsByType[event.eventType] = (eventsByType[event.eventType] || 0) + 1;
  }
  return {
    totalEvents: events.length,
    eventsByType,
    firstEvent: events.length > 0 ? events[0].occurredAt : void 0,
    lastEvent: events.length > 0 ? events[events.length - 1].occurredAt : void 0
  };
}
async function hasEventOccurred(em, instanceId, eventType) {
  const count = await em.count(WorkflowEvent, {
    workflowInstanceId: instanceId,
    eventType
  });
  return count > 0;
}
async function getEventTimeline(em, instanceId) {
  const events = await em.find(
    WorkflowEvent,
    { workflowInstanceId: instanceId },
    { orderBy: { occurredAt: "ASC" } }
  );
  return events.map((event) => ({
    eventType: event.eventType,
    occurredAt: event.occurredAt,
    stepInstanceId: event.stepInstanceId || void 0,
    userId: event.userId || void 0,
    summary: generateEventSummary(event)
  }));
}
function generateEventSummary(event) {
  const data = event.eventData || {};
  switch (event.eventType) {
    case WorkflowEventTypes.WORKFLOW_STARTED:
      return `Workflow started${data.workflowId ? ` (${data.workflowId})` : ""}`;
    case WorkflowEventTypes.WORKFLOW_COMPLETED:
      return `Workflow completed${data.result ? ` with result` : ""}`;
    case WorkflowEventTypes.WORKFLOW_FAILED:
      return `Workflow failed${data.error ? `: ${data.error}` : ""}`;
    case WorkflowEventTypes.STEP_ENTERED:
      return `Entered step: ${data.stepName || data.stepId || "unknown"}`;
    case WorkflowEventTypes.STEP_EXITED:
      return `Exited step: ${data.stepName || data.stepId || "unknown"}${data.executionTimeMs ? ` (${data.executionTimeMs}ms)` : ""}`;
    case WorkflowEventTypes.STEP_FAILED:
      return `Step failed: ${data.stepName || data.stepId || "unknown"}${data.error ? ` - ${data.error}` : ""}`;
    case WorkflowEventTypes.TRANSITION_EXECUTED:
      return `Transition: ${data.fromStepId} \u2192 ${data.toStepId}`;
    case WorkflowEventTypes.TRANSITION_REJECTED:
      return `Transition blocked: ${data.fromStepId} \u2192 ${data.toStepId}${data.reason ? ` (${data.reason})` : ""}`;
    case WorkflowEventTypes.USER_TASK_CREATED:
      return `User task created: ${data.taskName || "unnamed"}${data.assignedTo ? ` (assigned to ${data.assignedTo})` : ""}`;
    case WorkflowEventTypes.USER_TASK_COMPLETED:
      return `User task completed: ${data.taskName || "unnamed"}`;
    case WorkflowEventTypes.ACTIVITY_COMPLETED:
      return `Activity completed: ${data.activityName || data.activityId || "unknown"}`;
    case WorkflowEventTypes.ACTIVITY_FAILED:
      return `Activity failed: ${data.activityName || data.activityId || "unknown"}${data.error ? ` - ${data.error}` : ""}`;
    case WorkflowEventTypes.COMPENSATION_STARTED:
      return `Compensation started${data.reason ? `: ${data.reason}` : ""}`;
    case WorkflowEventTypes.COMPENSATION_COMPLETED:
      return `Compensation completed: ${data.compensatedActivities || 0}/${data.totalActivities || 0} activities`;
    case WorkflowEventTypes.COMPENSATION_PARTIAL:
      return `Partial compensation: ${data.compensatedActivities || 0}/${data.totalActivities || 0} succeeded`;
    case WorkflowEventTypes.COMPENSATION_FAILED:
      return `Compensation failed: ${data.failedCompensations?.length || 0} activities failed`;
    case WorkflowEventTypes.COMPENSATION_ACTIVITY_STARTED:
      return `Compensating: ${data.compensationActivityName || data.compensationActivityId || "unknown"}`;
    case WorkflowEventTypes.COMPENSATION_ACTIVITY_COMPLETED:
      return `Compensated: ${data.compensationActivityName || data.compensationActivityId || "unknown"}`;
    case WorkflowEventTypes.COMPENSATION_ACTIVITY_FAILED:
      return `Compensation failed: ${data.compensationActivityName || data.compensationActivityId || "unknown"}${data.error ? ` - ${data.error}` : ""}`;
    default:
      return event.eventType;
  }
}
function isValidEventType(eventType) {
  return Object.values(WorkflowEventTypes).includes(eventType);
}
function getAllEventTypes() {
  return Object.values(WorkflowEventTypes);
}
export {
  WorkflowEventTypes,
  countEvents,
  getAllEventTypes,
  getEventStatistics,
  getEventTimeline,
  getLatestEvent,
  getStepEvents,
  getWorkflowEvents,
  hasEventOccurred,
  isValidEventType,
  logWorkflowEvent,
  logWorkflowEvents
};
//# sourceMappingURL=event-logger.js.map
