# Trainer Role & Ticket System - Implementation Complete ✅

## Overview
Complete implementation of trainer role with login capability and ticket/chat system between students and trainers. Super admins can view all conversations.

## What Was Implemented

### 1. User Role System
**File:** `app/models/user.py`
- Updated User model to match actual database schema (UUID as VARCHAR)
- Added `role` property methods (`is_admin`, `is_trainer`)
- Roles: `student`, `trainer`, `admin`

### 2. Trainer Model Extension
**File:** `app/models/course.py`
- Added `user_id` field to Trainer model (VARCHAR foreign key to users.id)
- Allows linking trainer profiles to user accounts for login

### 3. Ticket Models
**File:** `app/models/ticket.py`
- `TrainerTicket` model with:
  - Links to course, student (user), trainer (user)
  - Ticket type: "help" or "apply"
  - Status tracking: open, in_progress, resolved, closed
  - Arabic labels for status and type
- `TrainerTicketMessage` model with:
  - Message content
  - Read/unread tracking
  - Sender identification

### 4. Database Tables Created
```sql
-- trainer_tickets table with foreign keys to courses and users
-- trainer_ticket_messages table with foreign keys to tickets and users
-- Indexes for performance
```

### 5. Tickets Blueprint
**File:** `app/routes/tickets.py`
Routes:
- `/tickets/my-tickets` - View my tickets (filtered by role)
- `/tickets/new/<course_id>/<type>` - Create new ticket form
- `/tickets/create/<course_id>` - POST to create ticket
- `/tickets/<id>` - View ticket and chat
- `/tickets/<id>/message` - POST to send message
- `/tickets/<id>/close` - Close ticket
- `/tickets/<id>/reopen` - Reopen ticket
- `/api/tickets/<id>/messages` - Poll for new messages

### 6. Admin Routes
**File:** `app/routes/admin.py`
- `/admin/tickets` - View all tickets with filters
- `/admin/tickets/<id>` - View any ticket as admin
- `/admin/tickets/<id>/assign` - Assign ticket to trainer
- `/admin/users/<id>/set-role` - Set user role

### 7. Templates Created

**User-facing:**
- `tickets/my_tickets.html` - User's ticket list
- `tickets/new_ticket.html` - New ticket form
- `tickets/detail.html` - Chat interface with real-time updates

**Admin:**
- `admin/tickets.html` - All tickets table
- `admin/ticket_detail.html` - Admin view of ticket with chat

### 8. Course Page Updates
**File:** `app/templates/courses/detail.html`
- Added two buttons in sidebar for authenticated users:
  - "طلب مساعدة" (Request Help) - Opens help ticket
  - "التقديم للمدرب" (Apply to Trainer) - Opens application ticket
- Both buttons link to ticket creation form

### 9. Navigation Updates
**File:** `app/templates/base.html`
- Added "التذاكر" (Tickets) link to main navigation

### 10. Translations
**File:** `app/i18n.py`
- Added `"nav.tickets": {"ar": "التذاكر", "en": "Tickets"}`

### 11. Admin Menu
**File:** `app/templates/admin/index.html`
- Added "التذاكر" link to admin menu

## Features

### For Students
- Request help with course content
- Apply to trainer for mentoring
- Real-time chat with trainer
- View all their tickets
- Close/reopen tickets

### For Trainers
- View tickets assigned to them
- Reply to student messages
- Close resolved tickets
- Receive notifications for new tickets

### For Admins
- View ALL tickets across platform
- Reply to any ticket
- Assign tickets to trainers
- Change user roles
- Filter tickets by status and type

### Notifications
- New ticket notification to trainer
- Reply notification to student
- In-app notifications using existing Notification model

## Security
- All routes require login
- Permission checks:
  - Students see only their tickets
  - Trainers see only assigned tickets
  - Admins see all tickets
- CSRF protection on all forms
- SQL injection protection via SQLAlchemy ORM

## Database Schema
```
trainer_tickets:
  - id (SERIAL PK)
  - course_id (FK → courses.id)
  - student_id (FK → users.id)
  - trainer_id (FK → users.id)
  - ticket_type (VARCHAR) - "help" or "apply"
  - subject (VARCHAR)
  - status (VARCHAR) - "open", "in_progress", "resolved", "closed"
  - created_at, updated_at

trainer_ticket_messages:
  - id (SERIAL PK)
  - ticket_id (FK → trainer_tickets.id)
  - sender_id (FK → users.id)
  - message (TEXT)
  - is_read (BOOLEAN)
  - created_at
```

## Testing
To test the system:
1. Create a user with role "trainer"
2. Link trainer profile to user via `user_id` in trainers table
3. Visit a course page
4. Click "طلب مساعدة" or "التقديم للمدرب"
5. Fill the form and submit
6. Trainer receives notification and can reply
7. Admin can view all tickets at `/admin/tickets`

## Files Modified/Created
- `app/models/user.py` - Updated to match database schema
- `app/models/course.py` - Added user_id to Trainer
- `app/models/ticket.py` - New ticket models
- `app/routes/tickets.py` - New tickets blueprint
- `app/routes/admin.py` - Added ticket routes
- `app/templates/tickets/*.html` - New ticket templates
- `app/templates/admin/tickets.html` - New admin tickets list
- `app/templates/admin/ticket_detail.html` - New admin ticket view
- `app/templates/courses/detail.html` - Added ticket buttons
- `app/templates/base.html` - Added tickets nav link
- `app/i18n.py` - Added translations
- `app/__init__.py` - Registered tickets blueprint
- `app/models/__init__.py` - Exported ticket models
