# Messages App

Thread and message management for Dexxy.

## Purpose

This app is the core content storage for Dexxy. It stores conversations from all connected platforms as Threads and Messages, providing a unified view of community interactions regardless of their original source (GitHub issues, Slack threads, Discord channels, etc.).

## Models

### Tag

Labels for organizing threads.

| Field | Type | Description |
|-------|------|-------------|
| `workspace` | ForeignKey | Workspace scope |
| `name` | CharField | Tag name |
| `color` | CharField | Hex color code (optional) |

### Thread

A conversation from any connected source.

| Field | Type | Description |
|-------|------|-------------|
| `workspace` | ForeignKey | Workspace scope |
| `source` | ForeignKey | Origin source (Slack, GitHub, etc.) |
| `external_id` | CharField | ID in the source system |
| `title` | CharField | Thread title |
| `status` | CharField | Thread state |
| `tags` | ManyToManyField | Applied tags |
| `metadata` | JSONField | Source-specific data |
| `activity_at` | DateTimeField | Last activity timestamp |
| `created_at` | DateTimeField | Creation timestamp |

**Status Values:**
- `open` - Active/unresolved
- `closed` - Resolved/completed
- `archived` - Hidden from default views

### Message

An individual message within a thread.

| Field | Type | Description |
|-------|------|-------------|
| `thread` | ForeignKey | Parent thread |
| `author` | ForeignKey | Message author (Member) |
| `external_id` | CharField | ID in the source system |
| `content` | TextField | Message body |
| `sent_at` | DateTimeField | When message was sent |
| `metadata` | JSONField | Source-specific data |

## GraphQL API

### Queries

- `threads(workspace_id, status?, source_id?)` - List threads with filters
- `thread(id)` - Get a specific thread
- `messages(thread_id)` - Get messages in a thread
- `tags(workspace_id)` - List workspace tags

### Mutations

- `createTag(input)` - Create a new tag
- `deleteTag(id)` - Delete a tag
- `updateThread(id, input)` - Update thread status/title/tags
- `createMessage(input)` - Add a message to a thread

## Key Patterns

1. **Unified Data Model**: All platforms → same Thread/Message structure
2. **External ID Tracking**: Maintains link to source system
3. **Activity Ordering**: `activity_at` enables chronological feeds
4. **Flexible Metadata**: JSON fields preserve source-specific details
5. **Tag System**: Workspace-scoped tags for organization

## Usage Examples

```python
from messages.models import Thread, Message
from sources.models import Source

# Create a thread from GitHub
thread = Thread.objects.create(
    workspace=workspace,
    source=github_source,
    external_id="issue_123",
    title="Bug: Login not working",
    status="open",
    metadata={"labels": ["bug", "priority-high"]}
)

# Add a message
Message.objects.create(
    thread=thread,
    author=member,
    external_id="comment_456",
    content="I can reproduce this on Chrome.",
    sent_at=timezone.now()
)
```

## Dependencies

- `accounts.Workspace`
- `sources.Source`
- `members.Member`

## File Structure

```
messages/
├── models.py      # Tag, Thread, Message models
├── graphql.py     # GraphQL types, queries, mutations
├── apps.py        # Django app config
└── migrations/    # Database migrations
```
