import type * as plugins from '../plugins.js';

// Re-export storage change event from smartbucket
import type { IStorageChangeEvent } from '@push.rocks/smartbucket';
export type { IStorageChangeEvent };

/**
 * MongoDB change event - wraps smartdata watcher output
 */
export interface IMongoChangeEvent {
  type: 'insert' | 'update' | 'delete' | 'replace' | 'drop' | 'invalidate';
  database: string;
  collection: string;
  documentId?: string;
  document?: Record<string, unknown>;
  updateDescription?: {
    updatedFields?: Record<string, unknown>;
    removedFields?: string[];
  };
  timestamp: string;
}

/**
 * Combined activity event for the activity stream
 */
export interface IActivityEvent {
  id: string;
  source: 'mongodb' | 'storage';
  event: IMongoChangeEvent | IStorageChangeEvent;
  timestamp: string;
}

// ===========================================
// TypedRequest interfaces for streaming subscriptions
// ===========================================

/**
 * Subscribe to MongoDB collection changes
 */
export interface IReq_SubscribeMongo extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_SubscribeMongo
> {
  method: 'subscribeMongo';
  request: {
    database: string;
    collection: string;
  };
  response: {
    success: boolean;
    subscriptionId: string;
  };
}

/**
 * Unsubscribe from MongoDB collection changes
 */
export interface IReq_UnsubscribeMongo extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_UnsubscribeMongo
> {
  method: 'unsubscribeMongo';
  request: {
    database: string;
    collection: string;
  };
  response: {
    success: boolean;
  };
}

/**
 * Subscribe to storage bucket/prefix changes
 */
export interface IReq_SubscribeStorage extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_SubscribeStorage
> {
  method: 'subscribeS3';
  request: {
    bucket: string;
    prefix?: string;
  };
  response: {
    success: boolean;
    subscriptionId: string;
  };
}

/**
 * Unsubscribe from storage bucket/prefix changes
 */
export interface IReq_UnsubscribeStorage extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_UnsubscribeStorage
> {
  method: 'unsubscribeS3';
  request: {
    bucket: string;
    prefix?: string;
  };
  response: {
    success: boolean;
  };
}

/**
 * Subscribe to activity stream (all changes from MongoDB and storage)
 */
export interface IReq_SubscribeActivity extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_SubscribeActivity
> {
  method: 'subscribeActivity';
  request: {};
  response: {
    success: boolean;
    subscriptionId: string;
  };
}

/**
 * Unsubscribe from activity stream
 */
export interface IReq_UnsubscribeActivity extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_UnsubscribeActivity
> {
  method: 'unsubscribeActivity';
  request: {};
  response: {
    success: boolean;
  };
}

/**
 * Get recent activity events (for initial load or reconnection)
 */
export interface IReq_GetRecentActivity extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetRecentActivity
> {
  method: 'getRecentActivity';
  request: {
    limit?: number; // Default: 100
  };
  response: {
    events: IActivityEvent[];
  };
}

// ===========================================
// Server-to-client push event interfaces
// ===========================================

/**
 * Server pushes MongoDB change to client
 */
export interface IReq_PushMongoChange extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_PushMongoChange
> {
  method: 'pushMongoChange';
  request: {
    event: IMongoChangeEvent;
  };
  response: {
    received: boolean;
  };
}

/**
 * Server pushes storage change to client
 */
export interface IReq_PushStorageChange extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_PushStorageChange
> {
  method: 'pushS3Change';
  request: {
    event: IStorageChangeEvent;
  };
  response: {
    received: boolean;
  };
}

/**
 * Server pushes activity event to client
 */
export interface IReq_PushActivityEvent extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_PushActivityEvent
> {
  method: 'pushActivityEvent';
  request: {
    event: IActivityEvent;
  };
  response: {
    received: boolean;
  };
}

/**
 * Connection tag for tracking subscriptions
 */
export interface ISubscriptionTag extends plugins.typedrequestInterfaces.ITag {
  name: 'subscription';
  payload: {
    type: 'mongo' | 'storage' | 'activity';
    key: string; // e.g., "db/collection" or "bucket/prefix" or "activity"
  };
}
