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

/**
 * Configuration for storage (S3-compatible) connection
 */
export interface IStorageConfig {
  endpoint: string;
  port?: number;
  accessKey: string;
  accessSecret: string;
  useSsl?: boolean;
  region?: string;
}

/**
 * Configuration for MongoDB connection
 */
export interface IMongoConfig {
  mongoDbUrl: string;
  mongoDbName: string;
}

/**
 * Combined configuration for tsview
 */
export interface ITsViewConfig {
  s3?: IStorageConfig;
  mongo?: IMongoConfig;
}

/**
 * Configuration from smartconfig.json for @git.zone/tsview
 */
export interface ISmartconfigConfig {
  port?: number;           // Fixed port to use (optional)
  killIfBusy?: boolean;    // Kill process on port if busy (default: false)
  openBrowser?: boolean;   // Open browser on start (default: true)
}

/**
 * Environment configuration from .nogit/env.json (gitzone service format)
 */
export interface IEnvConfig {
  MONGODB_URL?: string;
  MONGODB_HOST?: string;
  MONGODB_PORT?: string;
  MONGODB_USER?: string;
  MONGODB_PASS?: string;
  MONGODB_NAME?: string;
  S3_HOST?: string;
  S3_PORT?: string;
  S3_ACCESSKEY?: string;
  S3_SECRETKEY?: string;
  S3_BUCKET?: string;
  S3_ENDPOINT?: string;
  S3_USESSL?: boolean | string;
}

// ===========================================
// TypedRequest interfaces for S3 API
// ===========================================

export interface IReq_ListBuckets extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_ListBuckets
> {
  method: 'listBuckets';
  request: {};
  response: {
    buckets: string[];
  };
}

export interface IReq_CreateBucket extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_CreateBucket
> {
  method: 'createBucket';
  request: {
    bucketName: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_DeleteBucket extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_DeleteBucket
> {
  method: 'deleteBucket';
  request: {
    bucketName: string;
  };
  response: {
    success: boolean;
  };
}

export interface IStorageObject {
  key: string;
  size?: number;
  lastModified?: string;
  isPrefix?: boolean;
}

export interface IReq_ListObjects extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_ListObjects
> {
  method: 'listObjects';
  request: {
    bucketName: string;
    prefix?: string;
    delimiter?: string;
  };
  response: {
    objects: IStorageObject[];
    prefixes: string[];
  };
}

export interface IReq_GetObject extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetObject
> {
  method: 'getObject';
  request: {
    bucketName: string;
    key: string;
  };
  response: {
    content: string; // base64
    contentType: string;
    size: number;
    lastModified: string;
  };
}

export interface IReq_GetObjectMetadata extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetObjectMetadata
> {
  method: 'getObjectMetadata';
  request: {
    bucketName: string;
    key: string;
  };
  response: {
    contentType: string;
    size: number;
    lastModified: string;
  };
}

export interface IReq_PutObject extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_PutObject
> {
  method: 'putObject';
  request: {
    bucketName: string;
    key: string;
    content: string; // base64
    contentType: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_DeleteObject extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_DeleteObject
> {
  method: 'deleteObject';
  request: {
    bucketName: string;
    key: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_CopyObject extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_CopyObject
> {
  method: 'copyObject';
  request: {
    sourceBucket: string;
    sourceKey: string;
    destBucket: string;
    destKey: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_DeletePrefix extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_DeletePrefix
> {
  method: 'deletePrefix';
  request: {
    bucketName: string;
    prefix: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_MoveObject extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_MoveObject
> {
  method: 'moveObject';
  request: {
    bucketName: string;
    sourceKey: string;
    destKey: string;
  };
  response: {
    success: boolean;
    error?: string;
  };
}

export interface IReq_MovePrefix extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_MovePrefix
> {
  method: 'movePrefix';
  request: {
    bucketName: string;
    sourcePrefix: string;
    destPrefix: string;
  };
  response: {
    success: boolean;
    movedCount: number;
    error?: string;
  };
}

export interface IReq_GetObjectUrl extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetObjectUrl
> {
  method: 'getObjectUrl';
  request: {
    bucketName: string;
    key: string;
  };
  response: {
    url: string;
  };
}

// ===========================================
// TypedRequest interfaces for MongoDB API
// ===========================================

export interface IMongoDatabase {
  name: string;
  sizeOnDisk?: number;
  empty?: boolean;
}

export interface IReq_ListDatabases extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_ListDatabases
> {
  method: 'listDatabases';
  request: {};
  response: {
    databases: IMongoDatabase[];
  };
}

export interface IMongoCollection {
  name: string;
  count?: number;
}

export interface IReq_ListCollections extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_ListCollections
> {
  method: 'listCollections';
  request: {
    databaseName: string;
  };
  response: {
    collections: IMongoCollection[];
  };
}

export interface IReq_CreateDatabase extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_CreateDatabase
> {
  method: 'createDatabase';
  request: {
    databaseName: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_DropDatabase extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_DropDatabase
> {
  method: 'dropDatabase';
  request: {
    databaseName: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_CreateCollection extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_CreateCollection
> {
  method: 'createCollection';
  request: {
    databaseName: string;
    collectionName: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_DropCollection extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_DropCollection
> {
  method: 'dropCollection';
  request: {
    databaseName: string;
    collectionName: string;
  };
  response: {
    success: boolean;
  };
}

export interface IReq_FindDocuments extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_FindDocuments
> {
  method: 'findDocuments';
  request: {
    databaseName: string;
    collectionName: string;
    filter?: Record<string, unknown>;
    projection?: Record<string, unknown>;
    sort?: Record<string, number>;
    skip?: number;
    limit?: number;
  };
  response: {
    documents: Record<string, unknown>[];
    total: number;
  };
}

export interface IReq_GetDocument extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetDocument
> {
  method: 'getDocument';
  request: {
    databaseName: string;
    collectionName: string;
    documentId: string;
  };
  response: {
    document: Record<string, unknown> | null;
  };
}

export interface IReq_InsertDocument extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_InsertDocument
> {
  method: 'insertDocument';
  request: {
    databaseName: string;
    collectionName: string;
    document: Record<string, unknown>;
  };
  response: {
    insertedId: string;
  };
}

export interface IReq_UpdateDocument extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_UpdateDocument
> {
  method: 'updateDocument';
  request: {
    databaseName: string;
    collectionName: string;
    documentId: string;
    update: Record<string, unknown>;
  };
  response: {
    success: boolean;
    modifiedCount: number;
  };
}

export interface IReq_DeleteDocument extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_DeleteDocument
> {
  method: 'deleteDocument';
  request: {
    databaseName: string;
    collectionName: string;
    documentId: string;
  };
  response: {
    success: boolean;
    deletedCount: number;
  };
}

export interface IReq_RunAggregation extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_RunAggregation
> {
  method: 'runAggregation';
  request: {
    databaseName: string;
    collectionName: string;
    pipeline: Record<string, unknown>[];
  };
  response: {
    results: Record<string, unknown>[];
  };
}

export interface IMongoIndex {
  name: string;
  keys: Record<string, number>;
  unique?: boolean;
  sparse?: boolean;
}

export interface IReq_ListIndexes extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_ListIndexes
> {
  method: 'listIndexes';
  request: {
    databaseName: string;
    collectionName: string;
  };
  response: {
    indexes: IMongoIndex[];
  };
}

export interface IReq_CreateIndex extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_CreateIndex
> {
  method: 'createIndex';
  request: {
    databaseName: string;
    collectionName: string;
    keys: Record<string, number>;
    options?: {
      unique?: boolean;
      sparse?: boolean;
      name?: string;
    };
  };
  response: {
    indexName: string;
  };
}

export interface IReq_DropIndex extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_DropIndex
> {
  method: 'dropIndex';
  request: {
    databaseName: string;
    collectionName: string;
    indexName: string;
  };
  response: {
    success: boolean;
  };
}

export interface ICollectionStats {
  count: number;
  size: number;
  avgObjSize: number;
  storageSize: number;
  indexCount: number;
}

export interface IReq_GetCollectionStats extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetCollectionStats
> {
  method: 'getCollectionStats';
  request: {
    databaseName: string;
    collectionName: string;
  };
  response: {
    stats: ICollectionStats;
  };
}

export interface IReq_GetServerStatus extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetServerStatus
> {
  method: 'getServerStatus';
  request: {};
  response: {
    version: string;
    uptime: number;
    connections: {
      current: number;
      available: number;
    };
  };
}

export interface IDatabaseStats {
  collections: number;
  views: number;
  objects: number;
  avgObjSize: number;
  dataSize: number;
  storageSize: number;
  indexes: number;
  indexSize: number;
}

export interface IReq_GetDatabaseStats extends plugins.typedrequestInterfaces.implementsTR<
  plugins.typedrequestInterfaces.ITypedRequest,
  IReq_GetDatabaseStats
> {
  method: 'getDatabaseStats';
  request: {
    databaseName: string;
  };
  response: {
    stats: IDatabaseStats | null;
  };
}

// ===========================================
// Re-export streaming interfaces
// ===========================================
export * from '../streaming/interfaces.streaming.js';
