# MT5 Trade Copier SaaS — Dashboard (CodeIgniter 4)

Web management and monitoring dashboard for the **MT5 Trade Copier SaaS Platform**, built natively with **CodeIgniter 4** and **MySQL 8** (supporting SQLite3 in local dev).

---

## 🎯 Architecture Overview

```text
┌─────────────────────────────────────────────────────────────┐
│                 COPIER DASHBOARD (CI4)                      │
│                                                             │
│  - Authentication & Role-Based Access Control (RBAC)        │
│  - Master Sources Management & API Key Provisioning         │
│  - Follower Terminals Management & Lot Sizing Risk Controls │
│  - Master → Follower Routing Matrix & Symbol Transformations│
│  - Real-time Trade Events & Delivery Breakdown Monitoring   │
│  - Position Mappings (Master ↔ Follower) & Slippage Tracker │
│  - Follower Execution Logs & MT5 OrderSend Error Inspector  │
│  - Live Telemetry Heartbeat & Latency Monitor               │
│  - Comprehensive Audit Trail & User Management              │
│  - Global Risk Controls & Emergency Copier Kill-Switch      │
└──────────────────────────────┬──────────────────────────────┘
                               │
               Shared MySQL 8 Database Schema
                               │
┌──────────────────────────────┴──────────────────────────────┐
│                  GO COPIER SERVER ENGINE                    │
│                                                             │
│  - High-throughput asynchronous WebSocket connections       │
│  - MasterReader.ex5 observation intake & validation         │
│  - Target routing to active FollowerCopier.ex5 terminals    │
│  - Sequencing, ACKs, idempotent replays, reconciliations    │
└─────────────────────────────────────────────────────────────┘
```

---

## 🚀 Getting Started

### 1. Prerequisites
- **PHP**: 8.1+ with extensions: `intl`, `mbstring`, `mysqli` / `pdo_sqlite`, `curl`, `json`, `sodium`
- **Composer**: 2.0+
- **Database**: MySQL 8.0+ or SQLite3 (local fallback)

### 2. Installation & Configuration
Clone the repository and install Composer dependencies:
```bash
composer install
```

Copy and verify the environment file:
```bash
cp env .env
```

Ensure your `.env` contains your database connection and app settings:
```ini
CI_ENVIRONMENT = development
app.baseURL = 'http://localhost:8081/'

# Database (MySQL 8)
database.default.hostname = 127.0.0.1
database.default.database = copytrade_db
database.default.username = root
database.default.password = 
database.default.DBDriver = MySQLi
database.default.port = 3306
```

### 3. Database Migrations & Initial Setup
Run migrations to set up the shared copier tables and dashboard tables:
```bash
php spark migrate
```

Seed the default system settings:
```bash
php spark db:seed AdminSeeder
```

Create your secure administrator account:
```bash
php spark user:create-admin
```

### 4. Run the Development Server
Start the built-in development server:
```bash
php spark serve --port 8081
```

Open [http://localhost:8081](http://localhost:8081) in your browser and log in with your newly created credentials.

---

## 📋 Role-Based Access Control (RBAC)

| Feature / Module | ADMIN | OPERATOR | VIEWER |
| :--- | :---: | :---: | :---: |
| View Dashboard KPIs & Live Feeds | ✅ | ✅ | ✅ |
| View Trade Events & Deliveries | ✅ | ✅ | ✅ |
| View Position Maps & Executions | ✅ | ✅ | ✅ |
| View System Health & Heartbeats | ✅ | ✅ | ✅ |
| Register & Edit Master Sources | ✅ | ✅ | ❌ |
| Register & Edit Follower Terminals | ✅ | ✅ | ❌ |
| Configure Routing Matrix & Lot Sizing | ✅ | ✅ | ❌ |
| Toggle Status & Rotate API Keys | ✅ | ✅ | ❌ |
| Delete Masters / Followers | ✅ | ❌ | ❌ |
| Manage Dashboard Users | ✅ | ❌ | ❌ |
| Modify Global Risk Settings | ✅ | ❌ | ❌ |

---

## 🔒 Security & API Key Provisioning
When a new Master Source or Follower Terminal is created, the dashboard generates a cryptographically secure 32-character token. The raw token is presented **once** in the UI with a quick-copy button. Only the **SHA-256 hash** is persisted in the database, aligning with the Go Copier Server's verification mechanism.
