/**
 * Message Protocol Types
 *
 * Types for federation message protocol (QUERY, COMMAND, EVENT).
 */

import type { FederationMessageType, FederationMessageStatus } from "@prisma/client";

/**
 * Query message payload (sent to remote instance)
 */
export interface QueryMessage {
  /** Unique message identifier for deduplication */
  messageId: string;
  /** Sending instance's federation ID */
  instanceId: string;
  /** Query string to execute */
  query: string;
  /** Optional context for query execution */
  context?: Record<string, unknown>;
  /** Request timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the query payload */
  signature: string;
}

/**
 * Query response payload
 */
export interface QueryResponse {
  /** Unique message identifier for this response */
  messageId: string;
  /** Original query messageId (for correlation) */
  correlationId: string;
  /** Responding instance's federation ID */
  instanceId: string;
  /** Whether the query was successful */
  success: boolean;
  /** Query result data */
  data?: unknown;
  /** Error message (if success=false) */
  error?: string;
  /** Response timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the response payload */
  signature: string;
}

/**
 * Query message details response
 */
export interface QueryMessageDetails {
  /** Message ID */
  id: string;
  /** Workspace ID */
  workspaceId: string;
  /** Connection ID */
  connectionId: string;
  /** Message type */
  messageType: FederationMessageType;
  /** Unique message identifier */
  messageId: string;
  /** Correlation ID (for responses) */
  correlationId?: string;
  /** Query string */
  query?: string;
  /** Response data */
  response?: unknown;
  /** Message status */
  status: FederationMessageStatus;
  /** Error message */
  error?: string;
  /** Creation timestamp */
  createdAt: Date;
  /** Last update timestamp */
  updatedAt: Date;
  /** Delivery timestamp */
  deliveredAt?: Date;
}

/**
 * Command message payload (sent to remote instance)
 */
export interface CommandMessage {
  /** Unique message identifier for deduplication */
  messageId: string;
  /** Sending instance's federation ID */
  instanceId: string;
  /** Command type to execute */
  commandType: string;
  /** Command-specific payload */
  payload: Record<string, unknown>;
  /** Request timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the command payload */
  signature: string;
}

/**
 * Command response payload
 */
export interface CommandResponse {
  /** Unique message identifier for this response */
  messageId: string;
  /** Original command messageId (for correlation) */
  correlationId: string;
  /** Responding instance's federation ID */
  instanceId: string;
  /** Whether the command was successful */
  success: boolean;
  /** Command result data */
  data?: unknown;
  /** Error message (if success=false) */
  error?: string;
  /** Response timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the response payload */
  signature: string;
}

/**
 * Command message details response
 */
export interface CommandMessageDetails {
  /** Message ID */
  id: string;
  /** Workspace ID */
  workspaceId: string;
  /** Connection ID */
  connectionId: string;
  /** Message type */
  messageType: FederationMessageType;
  /** Unique message identifier */
  messageId: string;
  /** Correlation ID (for responses) */
  correlationId?: string;
  /** Command type */
  commandType?: string;
  /** Command payload */
  payload?: Record<string, unknown>;
  /** Response data */
  response?: unknown;
  /** Message status */
  status: FederationMessageStatus;
  /** Error message */
  error?: string;
  /** Creation timestamp */
  createdAt: Date;
  /** Last update timestamp */
  updatedAt: Date;
  /** Delivery timestamp */
  deliveredAt?: Date;
}

/**
 * Event message payload (sent to remote instance)
 */
export interface EventMessage {
  /** Unique message identifier for deduplication */
  messageId: string;
  /** Sending instance's federation ID */
  instanceId: string;
  /** Event type (e.g., "task.created", "user.updated") */
  eventType: string;
  /** Event-specific payload */
  payload: Record<string, unknown>;
  /** Request timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the event payload */
  signature: string;
}

/**
 * Event acknowledgment payload
 */
export interface EventAck {
  /** Unique message identifier for this acknowledgment */
  messageId: string;
  /** Original event messageId (for correlation) */
  correlationId: string;
  /** Acknowledging instance's federation ID */
  instanceId: string;
  /** Whether the event was received successfully */
  received: boolean;
  /** Error message (if received=false) */
  error?: string;
  /** Acknowledgment timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the acknowledgment payload */
  signature: string;
}

/**
 * Event message details response
 */
export interface EventMessageDetails {
  /** Message ID */
  id: string;
  /** Workspace ID */
  workspaceId: string;
  /** Connection ID */
  connectionId: string;
  /** Message type */
  messageType: FederationMessageType;
  /** Unique message identifier */
  messageId: string;
  /** Correlation ID (for acknowledgments) */
  correlationId?: string;
  /** Event type */
  eventType?: string;
  /** Event payload */
  payload?: Record<string, unknown>;
  /** Response data */
  response?: unknown;
  /** Message status */
  status: FederationMessageStatus;
  /** Error message */
  error?: string;
  /** Creation timestamp */
  createdAt: Date;
  /** Last update timestamp */
  updatedAt: Date;
  /** Delivery timestamp */
  deliveredAt?: Date;
}

/**
 * Event subscription details
 */
export interface SubscriptionDetails {
  /** Subscription ID */
  id: string;
  /** Workspace ID */
  workspaceId: string;
  /** Connection ID */
  connectionId: string;
  /** Event type subscribed to */
  eventType: string;
  /** Additional metadata */
  metadata: Record<string, unknown>;
  /** Whether subscription is active */
  isActive: boolean;
  /** Creation timestamp */
  createdAt: Date;
  /** Last update timestamp */
  updatedAt: Date;
}
