import { ZodTypeAny } from 'zod';
import {
  emailProviderConnectedEventPayloadSchema,
  emailProviderDisconnectedEventPayloadSchema,
  inboundEmailReceivedEventPayloadSchema,
} from './emailWorkflowSchemas';
import { inboundWebhookReceivedEventPayloadSchema } from './inboundWebhookSchemas';
import {
  appointmentRequestApprovedEventPayloadSchema,
  appointmentRequestCancelledEventPayloadSchema,
  appointmentRequestCreatedEventPayloadSchema,
  appointmentRequestDeclinedEventPayloadSchema,
  appointmentAssignedEventPayloadSchema,
  appointmentCanceledEventPayloadSchema,
  appointmentCompletedEventPayloadSchema,
  appointmentCreatedEventPayloadSchema,
  appointmentNoShowEventPayloadSchema,
  appointmentRescheduledEventPayloadSchema,
  capacityThresholdReachedEventPayloadSchema,
  scheduleBlockCreatedEventPayloadSchema,
  scheduleBlockDeletedEventPayloadSchema,
  scheduleEntryCreatedEventPayloadSchema,
  scheduleEntryDeletedEventPayloadSchema,
  scheduleEntryUpdatedEventPayloadSchema,
  technicianArrivedEventPayloadSchema,
  technicianCheckedOutEventPayloadSchema,
  technicianDispatchedEventPayloadSchema,
  technicianEnRouteEventPayloadSchema,
} from './schedulingEventSchemas';
import {
  projectApprovalGrantedEventPayloadSchema,
  projectApprovalRejectedEventPayloadSchema,
  projectApprovalRequestedEventPayloadSchema,
  projectAssignedEventPayloadSchema,
  projectClosedEventPayloadSchema,
  projectCreatedEventPayloadSchema,
  projectStatusChangedEventPayloadSchema,
  projectTaskAdditionalAgentAssignedEventPayloadSchema,
  projectTaskAssignedEventPayloadSchema,
  projectTaskCompletedEventPayloadSchema,
  projectTaskCreatedEventPayloadSchema,
  projectTaskDependencyBlockedEventPayloadSchema,
  projectTaskDependencyUnblockedEventPayloadSchema,
  projectTaskStatusChangedEventPayloadSchema,
  projectTaskUpdatedEventPayloadSchema,
  projectUpdatedEventPayloadSchema,
  taskCommentAddedEventPayloadSchema,
  taskCommentUpdatedEventPayloadSchema,
} from './projectEventSchemas';
import { timeEntryApprovedEventPayloadSchema, timeEntrySubmittedEventPayloadSchema } from './timeEventSchemas';
import {
  contractCreatedEventPayloadSchema,
  contractRenewalUpcomingEventPayloadSchema,
  contractStatusChangedEventPayloadSchema,
  contractUpdatedEventPayloadSchema,
  creditNoteAppliedEventPayloadSchema,
  creditNoteCreatedEventPayloadSchema,
  creditNoteVoidedEventPayloadSchema,
  invoiceDueDateChangedEventPayloadSchema,
  invoiceFinalizedEventPayloadSchema,
  invoiceGeneratedEventPayloadSchema,
  invoiceOverdueEventPayloadSchema,
  invoiceSentEventPayloadSchema,
  invoiceStatusChangedEventPayloadSchema,
  invoiceWrittenOffEventPayloadSchema,
  paymentAppliedEventPayloadSchema,
  paymentFailedEventPayloadSchema,
  paymentRecordedEventPayloadSchema,
  paymentRefundedEventPayloadSchema,
  recurringBillingRunCompletedEventPayloadSchema,
  recurringBillingRunFailedEventPayloadSchema,
  recurringBillingRunStartedEventPayloadSchema,
} from './billingEventSchemas';
import {
  clientArchivedEventPayloadSchema,
  clientCreatedEventPayloadSchema,
  clientMergedEventPayloadSchema,
  clientOwnerAssignedEventPayloadSchema,
  clientStatusChangedEventPayloadSchema,
  clientUpdatedEventPayloadSchema,
  contactArchivedEventPayloadSchema,
  contactCreatedEventPayloadSchema,
  contactMergedEventPayloadSchema,
  contactPrimarySetEventPayloadSchema,
  contactUpdatedEventPayloadSchema,
  interactionLoggedEventPayloadSchema,
  noteCreatedEventPayloadSchema,
  tagAppliedEventPayloadSchema,
  tagDefinitionCreatedEventPayloadSchema,
  tagDefinitionUpdatedEventPayloadSchema,
  tagRemovedEventPayloadSchema,
} from './crmEventSchemas';
import {
  documentAssociatedEventPayloadSchema,
  documentDeletedEventPayloadSchema,
  documentDetachedEventPayloadSchema,
  documentGeneratedEventPayloadSchema,
  documentSignatureExpiredEventPayloadSchema,
  documentSignatureRequestedEventPayloadSchema,
  documentSignedEventPayloadSchema,
  documentUploadedEventPayloadSchema,
} from './documentEventSchemas';
import {
  csatAlertTriggeredEventPayloadSchema,
  emailBouncedEventPayloadSchema,
  emailComplaintReceivedEventPayloadSchema,
  emailDeliveredEventPayloadSchema,
  emailUnsubscribedEventPayloadSchema,
  inboundEmailReplyReceivedEventPayloadSchema,
  notificationDeliveredEventPayloadSchema,
  notificationFailedEventPayloadSchema,
  notificationReadEventPayloadSchema,
  notificationSentEventPayloadSchema,
  outboundEmailFailedEventPayloadSchema,
  outboundEmailQueuedEventPayloadSchema,
  outboundEmailSentEventPayloadSchema,
  surveyExpiredEventPayloadSchema,
  surveyReminderSentEventPayloadSchema,
  surveyResponseReceivedEventPayloadSchema,
  surveySentEventPayloadSchema,
} from './communicationsEventSchemas';
import {
  externalMappingChangedEventPayloadSchema,
  integrationConnectedEventPayloadSchema,
  integrationDisconnectedEventPayloadSchema,
  integrationSyncCompletedEventPayloadSchema,
  integrationSyncFailedEventPayloadSchema,
  integrationSyncStartedEventPayloadSchema,
  integrationTokenExpiringEventPayloadSchema,
  integrationTokenRefreshFailedEventPayloadSchema,
  integrationWebhookReceivedEventPayloadSchema,
} from './integrationEventSchemas';
import {
  assetAssignedEventPayloadSchema,
  assetCreatedEventPayloadSchema,
  assetUnassignedEventPayloadSchema,
  assetUpdatedEventPayloadSchema,
  assetWarrantyExpiringEventPayloadSchema,
  fileUploadedEventPayloadSchema,
  mediaProcessingFailedEventPayloadSchema,
  mediaProcessingSucceededEventPayloadSchema,
} from './assetMediaEventSchemas';
import {
  ticketAdditionalAgentAssignedEventPayloadSchema,
  ticketApprovalGrantedEventPayloadSchema,
  ticketApprovalRejectedEventPayloadSchema,
  ticketApprovalRequestedEventPayloadSchema,
  ticketAssignedEventPayloadSchema,
  ticketClosedEventPayloadSchema,
  ticketCommentAddedEventPayloadSchema,
  ticketCreatedEventPayloadSchema,
  ticketCustomerRepliedEventPayloadSchema,
  ticketEscalatedEventPayloadSchema,
  ticketInternalNoteAddedEventPayloadSchema,
  ticketMergedEventPayloadSchema,
  ticketMessageAddedEventPayloadSchema,
  ticketPriorityChangedEventPayloadSchema,
  ticketQueueChangedEventPayloadSchema,
  ticketReopenedEventPayloadSchema,
  ticketResponseStateChangedEventPayloadSchema,
  ticketSlaStageBreachedEventPayloadSchema,
  ticketSlaStageEnteredEventPayloadSchema,
  ticketSlaStageMetEventPayloadSchema,
  ticketSplitEventPayloadSchema,
  ticketStatusChangedEventPayloadSchema,
  ticketTagsChangedEventPayloadSchema,
  ticketTimeEntryAddedEventPayloadSchema,
  ticketUnassignedEventPayloadSchema,
  ticketUpdatedEventPayloadSchema,
} from './ticketEventSchemas';

export const workflowEventPayloadSchemas: Record<string, ZodTypeAny> = {
  // Existing/already-present
  'payload.TicketCreated.v1': ticketCreatedEventPayloadSchema,
  'payload.TicketUpdated.v1': ticketUpdatedEventPayloadSchema,
  'payload.TicketClosed.v1': ticketClosedEventPayloadSchema,
  'payload.TicketResponseStateChanged.v1': ticketResponseStateChangedEventPayloadSchema,
  'payload.ProjectCreated.v1': projectCreatedEventPayloadSchema,
  'payload.InvoiceGenerated.v1': invoiceGeneratedEventPayloadSchema,
  'payload.InvoiceFinalized.v1': invoiceFinalizedEventPayloadSchema,
  'payload.InboundEmailReceived.v1': inboundEmailReceivedEventPayloadSchema,
  'payload.InboundWebhookReceived.v1': inboundWebhookReceivedEventPayloadSchema,
  'payload.EmailProviderConnected.v1': emailProviderConnectedEventPayloadSchema,
  'payload.EmailProviderDisconnected.v1': emailProviderDisconnectedEventPayloadSchema,

  // Tickets
  'payload.TicketStatusChanged.v1': ticketStatusChangedEventPayloadSchema,
  'payload.TicketPriorityChanged.v1': ticketPriorityChangedEventPayloadSchema,
  'payload.TicketAssigned.v1': ticketAssignedEventPayloadSchema,
  'payload.TicketAdditionalAgentAssigned.v1': ticketAdditionalAgentAssignedEventPayloadSchema,
  'payload.TicketCommentAdded.v1': ticketCommentAddedEventPayloadSchema,
  'payload.TicketUnassigned.v1': ticketUnassignedEventPayloadSchema,
  'payload.TicketReopened.v1': ticketReopenedEventPayloadSchema,
  'payload.TicketMerged.v1': ticketMergedEventPayloadSchema,
  'payload.TicketSplit.v1': ticketSplitEventPayloadSchema,
  'payload.TicketTagsChanged.v1': ticketTagsChangedEventPayloadSchema,
  'payload.TicketQueueChanged.v1': ticketQueueChangedEventPayloadSchema,
  'payload.TicketEscalated.v1': ticketEscalatedEventPayloadSchema,
  'payload.TicketMessageAdded.v1': ticketMessageAddedEventPayloadSchema,
  'payload.TicketCustomerReplied.v1': ticketCustomerRepliedEventPayloadSchema,
  'payload.TicketInternalNoteAdded.v1': ticketInternalNoteAddedEventPayloadSchema,
  'payload.TicketTimeEntryAdded.v1': ticketTimeEntryAddedEventPayloadSchema,
  'payload.TicketSlaStageEntered.v1': ticketSlaStageEnteredEventPayloadSchema,
  'payload.TicketSlaStageMet.v1': ticketSlaStageMetEventPayloadSchema,
  'payload.TicketSlaStageBreached.v1': ticketSlaStageBreachedEventPayloadSchema,
  'payload.TicketApprovalRequested.v1': ticketApprovalRequestedEventPayloadSchema,
  'payload.TicketApprovalGranted.v1': ticketApprovalGrantedEventPayloadSchema,
  'payload.TicketApprovalRejected.v1': ticketApprovalRejectedEventPayloadSchema,

  // Scheduling
  'payload.AppointmentRequestCreated.v1': appointmentRequestCreatedEventPayloadSchema,
  'payload.AppointmentRequestApproved.v1': appointmentRequestApprovedEventPayloadSchema,
  'payload.AppointmentRequestDeclined.v1': appointmentRequestDeclinedEventPayloadSchema,
  'payload.AppointmentRequestCancelled.v1': appointmentRequestCancelledEventPayloadSchema,
  'payload.ScheduleEntryCreated.v1': scheduleEntryCreatedEventPayloadSchema,
  'payload.ScheduleEntryUpdated.v1': scheduleEntryUpdatedEventPayloadSchema,
  'payload.ScheduleEntryDeleted.v1': scheduleEntryDeletedEventPayloadSchema,
  'payload.AppointmentCreated.v1': appointmentCreatedEventPayloadSchema,
  'payload.AppointmentRescheduled.v1': appointmentRescheduledEventPayloadSchema,
  'payload.AppointmentCanceled.v1': appointmentCanceledEventPayloadSchema,
  'payload.AppointmentCompleted.v1': appointmentCompletedEventPayloadSchema,
  'payload.AppointmentNoShow.v1': appointmentNoShowEventPayloadSchema,
  'payload.AppointmentAssigned.v1': appointmentAssignedEventPayloadSchema,
  'payload.ScheduleBlockCreated.v1': scheduleBlockCreatedEventPayloadSchema,
  'payload.ScheduleBlockDeleted.v1': scheduleBlockDeletedEventPayloadSchema,
  'payload.CapacityThresholdReached.v1': capacityThresholdReachedEventPayloadSchema,
  'payload.TechnicianDispatched.v1': technicianDispatchedEventPayloadSchema,
  'payload.TechnicianEnRoute.v1': technicianEnRouteEventPayloadSchema,
  'payload.TechnicianArrived.v1': technicianArrivedEventPayloadSchema,
  'payload.TechnicianCheckedOut.v1': technicianCheckedOutEventPayloadSchema,

  // Projects
  'payload.ProjectAssigned.v1': projectAssignedEventPayloadSchema,
  'payload.ProjectClosed.v1': projectClosedEventPayloadSchema,
  'payload.ProjectUpdated.v1': projectUpdatedEventPayloadSchema,
  'payload.ProjectStatusChanged.v1': projectStatusChangedEventPayloadSchema,
  'payload.ProjectTaskCreated.v1': projectTaskCreatedEventPayloadSchema,
  'payload.ProjectTaskUpdated.v1': projectTaskUpdatedEventPayloadSchema,
  'payload.ProjectTaskAssigned.v1': projectTaskAssignedEventPayloadSchema,
  'payload.ProjectTaskAdditionalAgentAssigned.v1': projectTaskAdditionalAgentAssignedEventPayloadSchema,
  'payload.ProjectTaskStatusChanged.v1': projectTaskStatusChangedEventPayloadSchema,
  'payload.ProjectTaskCompleted.v1': projectTaskCompletedEventPayloadSchema,
  'payload.ProjectTaskDependencyBlocked.v1': projectTaskDependencyBlockedEventPayloadSchema,
  'payload.ProjectTaskDependencyUnblocked.v1': projectTaskDependencyUnblockedEventPayloadSchema,
  'payload.ProjectApprovalRequested.v1': projectApprovalRequestedEventPayloadSchema,
  'payload.ProjectApprovalGranted.v1': projectApprovalGrantedEventPayloadSchema,
  'payload.ProjectApprovalRejected.v1': projectApprovalRejectedEventPayloadSchema,
  'payload.TaskCommentAdded.v1': taskCommentAddedEventPayloadSchema,
  'payload.TaskCommentUpdated.v1': taskCommentUpdatedEventPayloadSchema,

  // Time
  'payload.TimeEntrySubmitted.v1': timeEntrySubmittedEventPayloadSchema,
  'payload.TimeEntryApproved.v1': timeEntryApprovedEventPayloadSchema,

  // Billing
  'payload.InvoiceSent.v1': invoiceSentEventPayloadSchema,
  'payload.InvoiceStatusChanged.v1': invoiceStatusChangedEventPayloadSchema,
  'payload.InvoiceDueDateChanged.v1': invoiceDueDateChangedEventPayloadSchema,
  'payload.InvoiceOverdue.v1': invoiceOverdueEventPayloadSchema,
  'payload.InvoiceWrittenOff.v1': invoiceWrittenOffEventPayloadSchema,
  'payload.PaymentRecorded.v1': paymentRecordedEventPayloadSchema,
  'payload.PaymentApplied.v1': paymentAppliedEventPayloadSchema,
  'payload.PaymentFailed.v1': paymentFailedEventPayloadSchema,
  'payload.PaymentRefunded.v1': paymentRefundedEventPayloadSchema,
  'payload.CreditNoteCreated.v1': creditNoteCreatedEventPayloadSchema,
  'payload.CreditNoteApplied.v1': creditNoteAppliedEventPayloadSchema,
  'payload.CreditNoteVoided.v1': creditNoteVoidedEventPayloadSchema,
  'payload.ContractCreated.v1': contractCreatedEventPayloadSchema,
  'payload.ContractUpdated.v1': contractUpdatedEventPayloadSchema,
  'payload.ContractStatusChanged.v1': contractStatusChangedEventPayloadSchema,
  'payload.ContractRenewalUpcoming.v1': contractRenewalUpcomingEventPayloadSchema,
  'payload.RecurringBillingRunStarted.v1': recurringBillingRunStartedEventPayloadSchema,
  'payload.RecurringBillingRunCompleted.v1': recurringBillingRunCompletedEventPayloadSchema,
  'payload.RecurringBillingRunFailed.v1': recurringBillingRunFailedEventPayloadSchema,

  // CRM
  'payload.ClientCreated.v1': clientCreatedEventPayloadSchema,
  'payload.ClientUpdated.v1': clientUpdatedEventPayloadSchema,
  'payload.ClientStatusChanged.v1': clientStatusChangedEventPayloadSchema,
  'payload.ClientOwnerAssigned.v1': clientOwnerAssignedEventPayloadSchema,
  'payload.ClientMerged.v1': clientMergedEventPayloadSchema,
  'payload.ClientArchived.v1': clientArchivedEventPayloadSchema,
  'payload.ContactCreated.v1': contactCreatedEventPayloadSchema,
  'payload.ContactUpdated.v1': contactUpdatedEventPayloadSchema,
  'payload.ContactPrimarySet.v1': contactPrimarySetEventPayloadSchema,
  'payload.ContactArchived.v1': contactArchivedEventPayloadSchema,
  'payload.ContactMerged.v1': contactMergedEventPayloadSchema,
  'payload.InteractionLogged.v1': interactionLoggedEventPayloadSchema,
  'payload.NoteCreated.v1': noteCreatedEventPayloadSchema,
  'payload.TagDefinitionCreated.v1': tagDefinitionCreatedEventPayloadSchema,
  'payload.TagDefinitionUpdated.v1': tagDefinitionUpdatedEventPayloadSchema,
  'payload.TagApplied.v1': tagAppliedEventPayloadSchema,
  'payload.TagRemoved.v1': tagRemovedEventPayloadSchema,

  // Documents
  'payload.DocumentUploaded.v1': documentUploadedEventPayloadSchema,
  'payload.DocumentDeleted.v1': documentDeletedEventPayloadSchema,
  'payload.DocumentAssociated.v1': documentAssociatedEventPayloadSchema,
  'payload.DocumentDetached.v1': documentDetachedEventPayloadSchema,
  'payload.DocumentGenerated.v1': documentGeneratedEventPayloadSchema,
  'payload.DocumentSignatureRequested.v1': documentSignatureRequestedEventPayloadSchema,
  'payload.DocumentSigned.v1': documentSignedEventPayloadSchema,
  'payload.DocumentSignatureExpired.v1': documentSignatureExpiredEventPayloadSchema,

  // Email + Notifications + Surveys
  'payload.InboundEmailReplyReceived.v1': inboundEmailReplyReceivedEventPayloadSchema,
  'payload.OutboundEmailQueued.v1': outboundEmailQueuedEventPayloadSchema,
  'payload.OutboundEmailSent.v1': outboundEmailSentEventPayloadSchema,
  'payload.OutboundEmailFailed.v1': outboundEmailFailedEventPayloadSchema,
  'payload.EmailDelivered.v1': emailDeliveredEventPayloadSchema,
  'payload.EmailBounced.v1': emailBouncedEventPayloadSchema,
  'payload.EmailComplaintReceived.v1': emailComplaintReceivedEventPayloadSchema,
  'payload.EmailUnsubscribed.v1': emailUnsubscribedEventPayloadSchema,
  'payload.NotificationSent.v1': notificationSentEventPayloadSchema,
  'payload.NotificationDelivered.v1': notificationDeliveredEventPayloadSchema,
  'payload.NotificationFailed.v1': notificationFailedEventPayloadSchema,
  'payload.NotificationRead.v1': notificationReadEventPayloadSchema,
  'payload.SurveySent.v1': surveySentEventPayloadSchema,
  'payload.SurveyResponseReceived.v1': surveyResponseReceivedEventPayloadSchema,
  'payload.SurveyReminderSent.v1': surveyReminderSentEventPayloadSchema,
  'payload.SurveyExpired.v1': surveyExpiredEventPayloadSchema,
  'payload.CsatAlertTriggered.v1': csatAlertTriggeredEventPayloadSchema,

  // Integrations
  'payload.IntegrationSyncStarted.v1': integrationSyncStartedEventPayloadSchema,
  'payload.IntegrationSyncCompleted.v1': integrationSyncCompletedEventPayloadSchema,
  'payload.IntegrationSyncFailed.v1': integrationSyncFailedEventPayloadSchema,
  'payload.IntegrationWebhookReceived.v1': integrationWebhookReceivedEventPayloadSchema,
  'payload.IntegrationConnected.v1': integrationConnectedEventPayloadSchema,
  'payload.IntegrationDisconnected.v1': integrationDisconnectedEventPayloadSchema,
  'payload.IntegrationTokenExpiring.v1': integrationTokenExpiringEventPayloadSchema,
  'payload.IntegrationTokenRefreshFailed.v1': integrationTokenRefreshFailedEventPayloadSchema,
  'payload.ExternalMappingChanged.v1': externalMappingChangedEventPayloadSchema,

  // Assets + Media
  'payload.AssetCreated.v1': assetCreatedEventPayloadSchema,
  'payload.AssetUpdated.v1': assetUpdatedEventPayloadSchema,
  'payload.AssetAssigned.v1': assetAssignedEventPayloadSchema,
  'payload.AssetUnassigned.v1': assetUnassignedEventPayloadSchema,
  'payload.AssetWarrantyExpiring.v1': assetWarrantyExpiringEventPayloadSchema,
  'payload.FileUploaded.v1': fileUploadedEventPayloadSchema,
  'payload.MediaProcessingSucceeded.v1': mediaProcessingSucceededEventPayloadSchema,
  'payload.MediaProcessingFailed.v1': mediaProcessingFailedEventPayloadSchema,
};

export type WorkflowEventPayloadSchemaRef = keyof typeof workflowEventPayloadSchemas;
