# Chat Deletion Feature - Deployment Guide

## Summary

Fixed the chat deletion functionality to make it **permanent** with proper warnings. Users now get a clear warning dialog before deletion, and the system removes data from both the server database and local IndexedDB storage.

---

## Changes Made

### 1. Backend (Go) - DELETE Endpoints Added

**Files Modified:**
- `go-backend/internal/repository/chat_repository.go` - Added `DeleteConversation()` and `DeleteMessage()` methods
- `go-backend/internal/handlers/chat_handler.go` - Added DELETE handler endpoints
- `go-backend/cmd/api/main.go` - Registered DELETE routes

**New API Endpoints:**
- `DELETE /api/v1/conversations/:id` - Permanently deletes conversation and all messages
- `DELETE /api/v1/messages/:id` - Permanently deletes a single message

**Security:**
- Verifies user is a participant before allowing deletion
- Returns 401 Unauthorized if user doesn't have permission

### 2. Flutter App - Permanent Deletion

**Files Modified:**
- `sojorn_app/lib/services/api_service.dart` - Added `deleteConversation()` and `deleteMessage()` API methods
- `sojorn_app/lib/services/secure_chat_service.dart` - Updated to call backend DELETE API
- `sojorn_app/lib/screens/secure_chat/secure_chat_screen.dart` - Enhanced warning dialog

**Key Changes:**
- Delete now removes data from **both** server database and local IndexedDB
- New warning dialog with:
  - ⚠️ Red warning icon and "PERMANENT DELETION" title
  - Bullet points showing what will be deleted
  - Red warning box stating "THIS ACTION CANNOT BE UNDONE"
  - Red "DELETE PERMANENTLY" button
  - Cannot be dismissed by tapping outside
- Loading indicator during deletion
- Success/error feedback

---

## Deployment Steps

### Step 1: Delete All Existing Chats from Database

**SSH into your server:**
```bash
ssh -i "C:\Users\Patrick\.ssh\mpls.pem" patrick@194.238.28.122
```

**Password:** `P22k154ever!`

**Run the SQL script:**
```bash
sudo -u postgres psql sojorn
```

Then paste this SQL:
```sql
BEGIN;

-- Delete all messages first (foreign key dependency)
DELETE FROM public.secure_messages;

-- Delete all conversations
DELETE FROM public.encrypted_conversations;

COMMIT;

-- Verify deletion
SELECT COUNT(*) as message_count FROM public.secure_messages;
SELECT COUNT(*) as conversation_count FROM public.encrypted_conversations;
```

Expected output:
```
message_count: 0
conversation_count: 0
```

Type `\q` to exit psql.

### Step 2: Deploy Go Backend

**From your local machine, deploy the updated backend:**
```powershell
cd c:\Webs\Sojorn\go-backend
.\scripts\deploy.sh
```

Or manually:
```bash
# On server
cd /home/patrick/sojorn-backend
git pull
go build -o sojorn-api ./cmd/api
sudo systemctl restart sojorn-api
sudo systemctl status sojorn-api
```

### Step 3: Hot Restart Flutter App

**No deployment needed** - just hot restart the Flutter web app:
1. In your browser, press `R` or refresh the page
2. Or run: `flutter run -d chrome --web-port 8080`

---

## Testing the Fix

1. **Start a new conversation** with another user
2. **Send a few test messages**
3. **Click the 3-dot menu** in the chat screen
4. **Select "Delete Chat"**
5. **Verify the warning dialog shows:**
   - Red warning icon
   - "PERMANENT DELETION" title
   - List of what will be deleted
   - Red warning box
   - "DELETE PERMANENTLY" button
6. **Click "DELETE PERMANENTLY"**
7. **Verify:**
   - Loading indicator appears
   - Success message shows
   - Chat screen closes
   - Conversation is removed from list
   - Messages are gone from database (check with SQL)
   - Messages are gone from IndexedDB (check browser DevTools > Application > IndexedDB)

---

## What's Fixed

### Before:
- ❌ Delete only removed local IndexedDB data
- ❌ Server data remained (encrypted messages still in DB)
- ❌ Weak warning dialog
- ❌ Deletion wasn't permanent
- ❌ Other user could still see messages

### After:
- ✅ Deletes from **both** server database and local storage
- ✅ Strong warning dialog with multiple warnings
- ✅ **PERMANENT** deletion - cannot be undone
- ✅ Both users lose all messages
- ✅ Proper loading and error handling
- ✅ Authorization checks (only participants can delete)

---

## SQL Script Location

The SQL script to delete all chats is saved at:
`c:\Webs\Sojorn\migrations_archive\delete_all_chats.sql`

---

## Notes

- The E2EE key fixes from earlier are still in place
- Users will need to hot restart to get the new OTK fixes
- After deleting all chats, users can start fresh with properly working E2EE
- The delete function now works correctly for future conversations
