// Server → Client events
export interface ServerToClientEvents {
  // Berth events
  'berth:statusChanged': (payload: {
    berthId: string;
    oldStatus?: string;
    newStatus: string;
    triggeredBy: string;
    trigger?: string;
  }) => void;
  'berth:updated': (payload: { berthId: string; changedFields: string[] }) => void;
  'berth:waitingListChanged': (payload: {
    berthId: string;
    action: string;
    entry: unknown;
  }) => void;
  'berth:maintenanceAdded': (payload: { berthId: string; logEntry: unknown }) => void;
  'berth:maintenanceUpdated': (payload: { berthId: string; logEntry: unknown }) => void;
  'berth:maintenanceRemoved': (payload: { berthId: string; logId: string }) => void;

  // Client events
  'client:created': (payload: { clientId: string; clientName: string; source: string }) => void;
  'client:updated': (payload: { clientId: string; changedFields: string[] }) => void;
  'client:archived': (payload: { clientId: string }) => void;
  'client:restored': (payload: { clientId: string }) => void;
  'client:merged': (payload: { survivingId: string; mergedId: string }) => void;
  'client:noteAdded': (payload: {
    clientId: string;
    noteId: string;
    authorName: string;
    preview: string;
  }) => void;
  'client:duplicateDetected': (payload: {
    clientAId: string;
    clientBId: string;
    score: number;
    reason: string;
  }) => void;

  // Interest events
  'interest:created': (payload: {
    interestId: string;
    clientId: string;
    berthId: string | null;
    source: string;
  }) => void;
  'interest:updated': (payload: { interestId: string; changedFields: string[] }) => void;
  'interest:stageChanged': (payload: {
    interestId: string;
    oldStage: string;
    newStage: string;
    clientName: string;
    berthNumber: string;
  }) => void;
  'interest:berthLinked': (payload: { interestId: string; berthId: string }) => void;
  'interest:berthUnlinked': (payload: { interestId: string; berthId: string }) => void;
  'interest:berthLinkUpdated': (payload: { interestId: string; berthId: string }) => void;
  'interest:archived': (payload: { interestId: string }) => void;
  'interest:outcomeSet': (payload: {
    interestId: string;
    outcome: string;
    /** Stage the deal was on when the outcome was recorded. Renamed
     * from `oldStage` after the 2026-05-14 sentinel-stage cleanup -
     * the value is now the actual stage where the deal closed, not
     * a stale "what was it before we set 'completed'" marker. */
    stageAtOutcome: string;
  }) => void;
  'interest:outcomeCleared': (payload: { interestId: string }) => void;
  'interest:noteAdded': (payload: {
    interestId: string;
    noteId: string;
    authorName: string;
    preview: string;
  }) => void;
  'interest:recommendationsGenerated': (payload: {
    interestId: string;
    count: number;
    topBerthId: string;
  }) => void;
  'interest:recommendationAdded': (payload: {
    interestId: string;
    berthId: string;
    source: string;
    matchScore: number;
  }) => void;

  // Residential events
  'residential_client:created': (payload: { id: string }) => void;
  'residential_client:updated': (payload: { id: string }) => void;
  'residential_client:archived': (payload: { id: string }) => void;
  'residential_client:restored': (payload: { id: string }) => void;
  'residential_interest:created': (payload: { id: string }) => void;
  'residential_interest:updated': (payload: { id: string }) => void;
  'residential_interest:archived': (payload: { id: string }) => void;
  'interest:qualificationChanged': (payload: {
    interestId: string;
    criterionKey: string;
    confirmed: boolean;
  }) => void;
  'interest:leadCategoryChanged': (payload: {
    interestId: string;
    oldCategory: string;
    newCategory: string;
    auto: boolean;
  }) => void;

  // Yacht events
  'yacht:created': (payload: { yachtId: string }) => void;
  'yacht:updated': (payload: { yachtId: string; changedFields: string[] }) => void;
  'yacht:archived': (payload: { yachtId: string }) => void;
  'yacht:ownership_transferred': (payload: {
    yachtId: string;
    newOwner: { type: 'client' | 'company'; id: string };
  }) => void;

  // Company events
  'company:created': (payload: { companyId: string }) => void;
  'company:updated': (payload: { companyId: string; changedFields: string[] }) => void;
  'company:archived': (payload: { companyId: string }) => void;

  // Company membership events
  'company_membership:added': (payload: {
    membershipId: string;
    companyId: string;
    clientId: string;
  }) => void;
  'company_membership:updated': (payload: {
    membershipId: string;
    changedFields: string[];
  }) => void;
  'company_membership:ended': (payload: {
    membershipId: string;
    companyId: string;
    clientId: string;
  }) => void;

  // Berth tenancy events
  'berth_tenancy:created': (payload: { tenancyId: string; berthId: string }) => void;
  'berth_tenancy:activated': (payload: { tenancyId: string; berthId: string }) => void;
  /** Metadata-only edit (start/end date, tenure type, notes) — status unchanged. */
  'berth_tenancy:updated': (payload: { tenancyId: string; berthId: string }) => void;
  'berth_tenancy:ended': (payload: { tenancyId: string; berthId: string }) => void;
  'berth_tenancy:cancelled': (payload: { tenancyId: string; berthId: string }) => void;

  // Document events
  'document:created': (payload: { documentId: string; type?: string; interestId?: string }) => void;
  'document:updated': (payload: { documentId: string; changedFields?: string[] }) => void;
  'document:deleted': (payload: { documentId: string }) => void;
  'document:sent': (payload: {
    documentId: string;
    type?: string;
    signerCount?: number;
    documensoId?: string;
  }) => void;
  'document:signed': (payload: {
    documentId: string;
    signerName: string;
    signerRole: string;
    remainingSigners: number;
  }) => void;
  'document:signer:signed': (payload: {
    documentId: string;
    signerName?: string;
    signerEmail?: string;
    signerRole?: string;
    order?: number;
  }) => void;
  'document:completed': (payload: {
    documentId: string;
    type?: string;
    interestId?: string;
    clientName?: string;
  }) => void;
  'document:expired': (payload: { documentId: string }) => void;
  'document:cancelled': (payload: { documentId: string }) => void;
  'document:rejected': (payload: { documentId: string; signerEmail?: string | null }) => void;
  'document:signer:opened': (payload: { documentId: string; signerEmail?: string }) => void;
  'document:reminderSent': (payload: { documentId: string; recipientEmail: string }) => void;

  // Document template events
  'documentTemplate:created': (payload: {
    templateId: string;
    name?: string;
    type?: string;
  }) => void;
  'documentTemplate:updated': (payload: { templateId: string; changedFields?: string[] }) => void;
  'documentTemplate:deleted': (payload: { templateId: string }) => void;

  // Financial events
  'expense:created': (payload: {
    expenseId: string;
    amount: number;
    currency: string;
    category: string;
  }) => void;
  'expense:updated': (payload: { expenseId: string; changedFields: string[] }) => void;
  'expense:archived': (payload: { expenseId: string }) => void;
  'invoice:created': (payload: {
    invoiceId: string;
    invoiceNumber: string;
    total: number;
    clientName: string;
  }) => void;
  'invoice:updated': (payload: { invoiceId: string; changedFields: string[] }) => void;
  'invoice:sent': (payload: {
    invoiceId: string;
    invoiceNumber: string;
    recipientEmail: string;
  }) => void;
  'invoice:paid': (payload: { invoiceId: string; invoiceNumber: string; amount: number }) => void;
  'invoice:overdue': (payload: {
    invoiceId: string;
    invoiceNumber: string;
    daysPastDue: number;
  }) => void;
  'payment:created': (payload: {
    paymentId: string;
    interestId: string;
    paymentType: string;
  }) => void;
  'payment:updated': (payload: { paymentId: string; interestId: string }) => void;
  'payment:deleted': (payload: { paymentId: string; interestId: string }) => void;

  // Reminder & Calendar events
  'reminder:created': (payload: {
    reminderId: string;
    title: string;
    assignedTo: string;
    dueAt: string;
  }) => void;
  'reminder:updated': (payload: { reminderId: string; changedFields: string[] }) => void;
  'reminder:completed': (payload: {
    reminderId: string;
    title: string;
    completedBy: string;
  }) => void;
  'reminder:overdue': (payload: { reminderId: string; title: string; dueAt: string }) => void;
  'reminder:snoozed': (payload: { reminderId: string; snoozedUntil: string }) => void;
  'calendar:synced': (payload: { eventCount: number; lastSyncAt: string }) => void;
  'calendar:disconnected': (payload: { reason: string }) => void;

  // Notification events
  'notification:new': (payload: {
    notificationId: string;
    type: string;
    title: string;
    description: string;
    link: string;
  }) => void;
  'notification:unreadCount': (payload: { count: number }) => void;

  // Report events
  'report:queued': (payload: { reportId: string; reportType: string; name: string }) => void;
  'report:ready': (payload: { reportId: string; name: string }) => void;
  'report:failed': (payload: { reportId: string; name: string; error: string }) => void;

  // System events
  'system:alert': (payload: { alertType: string; message: string; severity: string }) => void;
  'system:jobFailed': (payload: { queueName: string; jobId: string; error: string }) => void;
  'registration:new': (payload: {
    clientId: string;
    interestId: string;
    clientName: string;
    berthNumber: string;
  }) => void;

  // File events
  'file:uploaded': (payload: {
    fileId: string;
    filename: string;
    clientId?: string;
    category?: string;
  }) => void;
  'file:updated': (payload: { fileId: string; changedFields?: string[] }) => void;
  'file:deleted': (payload: { fileId: string; filename?: string }) => void;

  // Phase B alert framework
  'alert:created': (payload: {
    alertId: string;
    portId: string;
    ruleId: string;
    severity: 'info' | 'warning' | 'critical';
    title: string;
    link: string;
  }) => void;
  'alert:resolved': (payload: { alertId: string; portId: string; ruleId: string }) => void;
  'alert:dismissed': (payload: { alertId: string; portId: string }) => void;
}

// Client → Server events (minimal - most actions go through REST API)
export interface ClientToServerEvents {
  'join:entity': (payload: { type: 'berth' | 'client' | 'interest'; id: string }) => void;
  'leave:entity': (payload: { type: 'berth' | 'client' | 'interest'; id: string }) => void;
}
