# Email Templates & Messaging TODO

## Completed Features

### Email Template System (Implemented)
- [x] EmailTemplate model with BUSINESS and PLATFORM scopes
- [x] Categories: Appointment, Reminder, Confirmation, Marketing, Notification, Report, Other
- [x] Template variables: `{{BUSINESS_NAME}}`, `{{CUSTOMER_NAME}}`, `{{APPOINTMENT_DATE}}`, etc.
- [x] Backend API (CRUD, preview, duplicate)
- [x] Frontend Email Templates page with list/create/edit/delete
- [x] EmailTemplateForm component with code editor and preview
- [x] EmailTemplateSelector component for plugin configuration
- [x] Plugin config support for `email_template` type variables
- [x] Footer enforcement for FREE tier tenants
- [x] Default templates seed command (`python manage.py seed_email_templates`)

### Default Email Templates Created
- [x] Appointment Confirmation
- [x] Appointment Reminder - 24 Hours
- [x] Appointment Reminder - 1 Hour
- [x] Appointment Rescheduled
- [x] Appointment Cancelled
- [x] Thank You - Appointment Complete
- [x] Welcome New Customer

### Ticket Email Templates (Added)
- [x] Ticket Assigned - Notification to staff when assigned
- [x] Ticket Status Changed - Status update notification
- [x] Ticket Reply - Staff Notification - When customer replies
- [x] Ticket Reply - Customer Notification - When staff replies
- [x] Ticket Resolved - Resolution confirmation

### Plugin Documentation Updated
- [x] Documented `email_template` variable type in SCHEDULER_PLUGIN_SYSTEM.md
- [x] Added template variable types table (text, textarea, email, number, url, email_template)
- [x] Added ticket-related insertion codes to template_parser.py

---

## Pending Features

### Customer Onboarding Email Process
- [ ] Welcome email when customer creates account
- [ ] Email verification workflow
- [ ] First booking celebration email
- [ ] Loyalty program introduction email (if enabled)

### Internal Staff Messaging System
- [ ] Message model (sender, recipient, subject, body, read status)
- [ ] Conversation threading
- [ ] Messaging UI in sidebar/header
- [ ] Unread count badge
- [ ] Push notifications for new messages
- [ ] Email notification option for messages

### Ticket Email Notifications
- [x] Email templates created (see "Ticket Email Templates" in Completed)
- [x] Ticket email notification service (`tickets/email_notifications.py`)
  - TicketEmailService class with methods for each notification type
  - Renders email templates with ticket context variables
  - Falls back to default text templates if custom templates not found
  - Includes Reply-To header with ticket ID for email threading
- [x] Django signals to trigger emails on ticket events (`tickets/signals.py`)
  - Pre-save handler to track state changes (assignee, status)
  - Post-save handlers for assignment, status change, and resolution
  - Comment handler for reply notifications
- [x] Reply-To header with ticket ID for inbound processing
  - Format: `support+ticket-{id}@{domain}`
  - X-Ticket-ID header for easy parsing

### Email Reply Forwarding (Inbound Email Processing)
- [ ] Setup inbound email handling (mail.talova.net integration)
- [ ] Parse incoming emails to support@smoothschedule.com (or per-tenant)
- [ ] Extract ticket ID from subject line or headers
- [ ] Create ticket reply from email content
- [ ] Notify assignee of new reply
- [ ] Handle attachments

---

## Email Server Details

Current email server: `mail.talova.net`
Test account: `poduck@mail.talova.net`

### Inbound Email Options

1. **Mailgun/SendGrid Inbound Routes**
   - Forward to webhook endpoint
   - Parse and process programmatically
   - Best for scalability

2. **IMAP Polling**
   - Connect to mail.talova.net via IMAP
   - Poll for new messages periodically
   - Process and delete/archive

3. **Email Piping**
   - Configure MTA to pipe emails to a Django management command
   - Real-time processing
   - Requires server access

---

## Platform vs Business Templates

### Platform Templates (Scope: PLATFORM)
Used for system-wide emails:
- Tenant invitation
- Platform announcements
- System notifications

### Business Templates (Scope: BUSINESS)
Per-tenant customizable templates:
- Appointment confirmations
- Reminders
- Customer communications
- Marketing emails

---

## Running the Seed Command

```bash
# Seed templates for all tenant schemas
docker compose -f docker-compose.local.yml exec django python manage.py seed_email_templates

# Seed for specific schema
docker compose -f docker-compose.local.yml exec django python manage.py seed_email_templates --schema=demo

# Reset to defaults (overwrites existing)
docker compose -f docker-compose.local.yml exec django python manage.py seed_email_templates --reset
```
