# Messaging

### Message Encryption

The SDK implements end-to-end encryption for secure messaging.

#### Encryption Setup

```typescript
// Initialize encryption
await client.message.initializeEncryption();

// Generate encryption keys
const keys = await client.message.generateKeys();

// Exchange public keys
await client.message.exchangeKeys(recipientPublicKey, publicKey);
```

#### Encryption Types

```typescript
enum EncryptionType {
  SYMMETRIC = 'symmetric',
  ASYMMETRIC = 'asymmetric',
  HYBRID = 'hybrid'
}

interface EncryptionConfig {
  type: EncryptionType;
  algorithm: string;
  keySize: number;
  padding: string;
}
```

### Sending Messages

#### Basic Message Sending

```typescript
// Send a text message
const message = await client.message.send(recipientPublicKey, {
  content: 'Hello!',
  type: 'text'
});

// Send a message with media
const mediaMessage = await client.message.send(recipientPublicKey, {
  content: 'Check this out!',
  type: 'media',
  media: [{
    type: 'image',
    url: 'ipfs://QmHash...'
  }]
});
```

#### Message Types

```typescript
interface Message {
  publicKey: PublicKey;
  sender: PublicKey;
  recipient: PublicKey;
  content: string;
  type: MessageType;
  encrypted: boolean;
  createdAt: number;
  status: MessageStatus;
  metadata?: Record<string, any>;
}

enum MessageType {
  TEXT = 'text',
  MEDIA = 'media',
  FILE = 'file',
  LOCATION = 'location',
  CONTACT = 'contact'
}

enum MessageStatus {
  SENT = 'sent',
  DELIVERED = 'delivered',
  READ = 'read',
  FAILED = 'failed'
}
```

### Message Threads

#### Thread Management

```typescript
// Create a new thread
const thread = await client.message.createThread(recipientPublicKey);

// Get thread messages
const messages = await client.message.getThread(threadPublicKey);

// Get all threads
const threads = await client.message.getAllThreads();

// Delete a thread
await client.message.deleteThread(threadPublicKey);
```

#### Thread Interface

```typescript
interface Thread {
  publicKey: PublicKey;
  participants: PublicKey[];
  lastMessage: Message;
  unreadCount: number;
  createdAt: number;
  updatedAt: number;
  metadata?: Record<string, any>;
}
```

### Message Security

#### Security Features

```typescript
// Enable message encryption
await client.message.enableEncryption();

// Set encryption preferences
await client.message.setEncryptionPreferences({
  type: EncryptionType.HYBRID,
  requireEncryption: true,
  autoDelete: true,
  deleteAfter: 7 * 24 * 60 * 60 * 1000 // 7 days
});

// Verify message integrity
const isValid = await client.message.verifyMessage(messagePublicKey);
```

#### Security Configuration

```typescript
interface SecurityConfig {
  encryption: {
    type: EncryptionType;
    requireEncryption: boolean;
    keyRotation: number;
  };
  privacy: {
    readReceipts: boolean;
    typingIndicators: boolean;
    onlineStatus: boolean;
  };
  retention: {
    autoDelete: boolean;
    deleteAfter: number;
  };
}
```

### Message Queries

#### Basic Queries

```typescript
// Get a single message
const message = await client.message.get(messagePublicKey);

// Get messages by thread
const threadMessages = await client.message.getByThread(threadPublicKey);

// Get unread messages
const unread = await client.message.getUnread();
```

#### Advanced Queries

```typescript
// Search messages
const searchResults = await client.message.search({
  query: 'important',
  filters: {
    dateRange: {
      start: new Date('2023-01-01'),
      end: new Date()
    },
    type: MessageType.TEXT
  }
});

// Get message statistics
const stats = await client.message.getStats();

// Get message metadata
const metadata = await client.message.getMetadata(messagePublicKey);
```

### Message Events

The SDK provides event listeners for message-related activities.

```typescript
// Listen for new messages
client.message.onNew((message) => {
  console.log('New message:', message);
});

// Listen for message status changes
client.message.onStatusChange((status) => {
  console.log('Message status changed:', status);
});

// Listen for typing indicators
client.message.onTyping((typing) => {
  console.log('User is typing:', typing);
});
```

### Message Management

#### Message Actions

```typescript
// Mark message as read
await client.message.markAsRead(messagePublicKey);

// Delete a message
await client.message.delete(messagePublicKey);

// Forward a message
await client.message.forward(messagePublicKey, newRecipientPublicKey);

// Reply to a message
await client.message.reply(messagePublicKey, 'Reply content');
```

#### Message Moderation

```typescript
// Report a message
await client.message.report(messagePublicKey, {
  reason: 'spam',
  details: 'This message contains spam content'
});

// Block a sender
await client.message.blockSender(senderPublicKey);

// Mute a thread
await client.message.muteThread(threadPublicKey);
```

### Message Media

#### Media Handling

```typescript
// Upload message media
const media = await client.message.uploadMedia(file, {
  type: 'image',
  maxSize: 10 * 1024 * 1024 // 10MB
});

// Send media message
const message = await client.message.send(recipientPublicKey, {
  type: 'media',
  media: [media]
});

// Download message media
const file = await client.message.downloadMedia(mediaPublicKey);
```
