/**
 * Connection Protocol Types
 *
 * Types for federation connection handshake protocol.
 */

import type { FederationCapabilities } from "./instance.types";
import type { FederationConnectionStatus } from "@prisma/client";

/**
 * Connection request payload (sent to remote instance)
 */
export interface ConnectionRequest {
  /** Requesting instance's federation ID */
  instanceId: string;
  /** Requesting instance's base URL */
  instanceUrl: string;
  /** Requesting instance's public key */
  publicKey: string;
  /** Requesting instance's capabilities */
  capabilities: FederationCapabilities;
  /** Request timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the request payload */
  signature: string;
}

/**
 * Connection response payload
 */
export interface ConnectionResponse {
  /** Whether the connection was accepted */
  accepted: boolean;
  /** Responding instance's federation ID */
  instanceId: string;
  /** Responding instance's public key */
  publicKey: string;
  /** Responding instance's capabilities */
  capabilities: FederationCapabilities;
  /** Rejection reason (if accepted=false) */
  reason?: string;
  /** Response timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the response payload */
  signature: string;
}

/**
 * Disconnect request payload
 */
export interface DisconnectRequest {
  /** Disconnecting instance's federation ID */
  instanceId: string;
  /** Reason for disconnection */
  reason?: string;
  /** Request timestamp (Unix milliseconds) */
  timestamp: number;
  /** RSA signature of the request payload */
  signature: string;
}

/**
 * Signable message (any object that can be signed)
 */
export type SignableMessage = Record<string, unknown>;

/**
 * Signature validation result
 */
export interface SignatureValidationResult {
  /** Whether the signature is valid */
  valid: boolean;
  /** Error message if validation failed */
  error?: string;
}

/**
 * Connection initiation request DTO
 */
export interface InitiateConnectionDto {
  /** URL of the remote instance to connect to */
  remoteUrl: string;
}

/**
 * Connection acceptance DTO
 */
export interface AcceptConnectionDto {
  /** Optional metadata to store with the connection */
  metadata?: Record<string, unknown>;
}

/**
 * Connection rejection DTO
 */
export interface RejectConnectionDto {
  /** Reason for rejection */
  reason: string;
}

/**
 * Connection disconnection DTO
 */
export interface DisconnectConnectionDto {
  /** Reason for disconnection */
  reason?: string;
}

/**
 * Connection details response
 */
export interface ConnectionDetails {
  /** Connection ID */
  id: string;
  /** Workspace ID */
  workspaceId: string;
  /** Remote instance federation ID */
  remoteInstanceId: string;
  /** Remote instance URL */
  remoteUrl: string;
  /** Remote instance public key */
  remotePublicKey: string;
  /** Remote instance capabilities */
  remoteCapabilities: FederationCapabilities;
  /** Connection status */
  status: FederationConnectionStatus;
  /** Additional metadata */
  metadata: Record<string, unknown>;
  /** Creation timestamp */
  createdAt: Date;
  /** Last update timestamp */
  updatedAt: Date;
  /** Connection established timestamp */
  connectedAt: Date | null;
  /** Disconnection timestamp */
  disconnectedAt: Date | null;
}
