# Fix Summary: Remote Note Attachment Bug

## Problem
Incoming remote notes with attachments were being incorrectly stored. The attachments were assigned to local `ap_notes` instead of the actual `ap_remote_notes`, causing either:
- FK violations (silent failures) when no local note had that ID
- Wrong attachments appearing on unrelated local notes when IDs collided

## Root Cause
The `attachments` table had no `ap_remote_note_id` column. When processing remote notes, the code used `Owner::ApNote(remote_note_id)`, which set the `ap_note_id` FK (pointing to local `ap_notes`) to a remote note ID.

## Changes Made

### 1. Database Migration (migration/src/m20261025_000028_add_remote_note_id_to_attachments.rs)
- Added `ap_remote_note_id` column with FK to `ap_remote_notes`
- Updated CHECK constraint to support `ap_remote_note` owner kind
- Data migration: automatically fixes existing mislinked attachments
  - Detects rows where `owner_kind='ap_note'` but the ID actually references `ap_remote_notes`
  - Moves them to the correct `ap_remote_note_id` column

### 2. Entity Update (frogue/src/entities/attachments.rs)
- Added `ap_remote_note_id: Option<i64>` field
- Added `ApRemoteNote` relation

### 3. Store Layer (frogue/src/attachments/store.rs)
- Added `Owner::ApRemoteNote(i64)` variant
- Updated `create()`, `create_remote()`, `list_for()`, `count_for()` to handle the new variant

### 4. Bug Fix (frogue/src/activitypub/remote/notes.rs)
- Changed 3 call sites from `Owner::ApNote` to `Owner::ApRemoteNote`:
  - Line 212: `handle_create` attachment creation
  - Line 373: `handle_update` attachment deletion
  - Line 398: `handle_update` attachment creation

## Test Results
✅ `note_attachments` tests: 6 passed
✅ `remote_notes` tests: 13 passed
✅ Code compiles successfully

## Production Data Impact
The migration will automatically fix the mislinked attachment in production:
- Attachment ID 6 (currently mislinked to local `ap_notes.id=3`) will be corrected to `ap_remote_notes.id=3`
- The `owner_kind` will be updated from `'ap_note'` to `'ap_remote_note'`

## Deployment
1. Deploy the new code
2. The migration will run automatically on startup
3. Existing mislinked attachments will be fixed
4. Future remote note attachments will be stored correctly
