﻿# INVESTIGATION.md - EVIDENCE-BASED COMPLETE
Date: 2025-09-21  
Feature: Messaging
Bug: Message styling UX problems

## EVIDENCE FROM BROWSER DEV TOOLS
✅ HTML Elements: ALL messages have class="message received" 
✅ Console Error: TypeError Cannot read properties of undefined (reading 'target')
 Zero messages ever get class="message sent"

## ROOT CAUSES IDENTIFIED (MULTIPLE BUGS)

### BUG #1: JavaScript TypeError - Line 253
**Code**: event.target.closest('.conversation-item').classList.add('active');
**Problem**: openConversation(userId, userName) has NO event parameter
**Evidence**: Function signature line 246 vs usage line 253
**Impact**: Breaks execution, currentUserId may not be set

### BUG #2: Wrong Logic Design - Line 247  
**Code**: currentUserId = userId; (where userId = conv.partner.id)
**Problem**: currentUserId stores PARTNER ID, not YOUR own ID
**Evidence**: Line 235 shows  passed to openConversation
**Impact**: Compares sender vs partner ID instead of sender vs your ID

### BUG #3: Backward Styling Logic - Line 177
**Code**: msg.sender_id === currentUserId ? 'sent' : 'received'
**Problem**: Should identify YOUR messages vs THEIR messages
**Current Logic**: "Is sender the partner?" (wrong question)
**Should Be**: "Is sender you?" (correct question)

## EVIDENCE CHAIN
1. currentUserId gets partner ID (line 247) 
2. Your messages: YOUR_ID === PARTNER_ID  false  'received' class
3. Partner messages: PARTNER_ID === PARTNER_ID  true  'sent' class  
4. BUT browser shows ALL 'received'  JavaScript error prevents proper execution

## FILES INVOLVED
- app/templates/messages/chat.html (lines 177, 247, 253)

## INVESTIGATION COMPLETE - READY FOR FIX PLAN
