# Example Calculations and Database Inserts

## Profit Distribution Example

### Scenario
- Total Equity: $5,500
- Daily Profit: $990
- 4 Active Investors

### Investors Data
| Investor | Equity | Units |
|----------|--------|-------|
| Investor A | $2,000 | 4 |
| Investor B | $1,000 | 2 |
| Investor C | $500 | 1 |
| Investor D | $2,000 | 4 |
| **Total** | $5,500 | **11** |

### Calculation Steps

#### Step 1: Calculate Safety Fund (20%)
```
safety_fund = total_profit × 20%
safety_fund = $990 × 0.20 = $198
```

#### Step 2: Calculate Sharing Profit (80%)
```
sharing_profit = total_profit - safety_fund
sharing_profit = $990 - $198 = $792
```

#### Step 3: Split Sharing Profit
```
referral_share = sharing_profit × 5% = $792 × 0.05 = $39.60
investor_share = sharing_profit × 50% = $792 × 0.50 = $396.00
ecm_share = sharing_profit × 45% = $792 × 0.45 = $356.40
```

#### Step 4: Distribute to Investors by Unit Ratio
```
Total Units: 11

Investor A: 4/11 of investor_share
  Profit = $396 × (4/11) = $396 × 0.3636 = $144.00

Investor B: 2/11 of investor_share
  Profit = $396 × (2/11) = $396 × 0.1818 = $72.00

Investor C: 1/11 of investor_share
  Profit = $396 × (1/11) = $396 × 0.0909 = $36.00

Investor D: 4/11 of investor_share
  Profit = $396 × (4/11) = $396 × 0.3636 = $144.00
```

### Database Example Inserts

#### 1. Insert Investors
```sql
INSERT INTO investors (name, email, equity, units, total_profit, available_balance, is_active, created_at, updated_at) VALUES
('Investor A', 'investora@example.com', 2000.00, 4, 0.00, 0.00, 1, NOW(), NOW()),
('Investor B', 'investorb@example.com', 1000.00, 2, 0.00, 0.00, 1, NOW(), NOW()),
('Investor C', 'investorc@example.com', 500.00, 1, 0.00, 0.00, 1, NOW(), NOW()),
('Investor D', 'investord@example.com', 2000.00, 4, 0.00, 0.00, 1, NOW(), NOW());
```

#### 2. Insert MT5 Account
```sql
INSERT INTO mt5_accounts (account_number, account_name, server, balance, equity, margin, free_margin, floating_profit, currency, leverage, is_active, last_sync_at, created_at, updated_at) VALUES
('12345678', 'ECM Master Account', 'broker.com', 5500.00, 5500.00, 1000.00, 4500.00, 0.00, 'USD', 100, 1, NOW(), NOW(), NOW());
```

#### 3. Insert Daily Profit Snapshot
```sql
INSERT INTO daily_profit_snapshots (mt5_account_id, snapshot_date, balance, equity, margin, floating_profit, daily_profit, open_trades_count, closed_trades_count, snapshot_at, created_at, updated_at) VALUES
(1, '2024-01-15', 5500.00, 5500.00, 1000.00, 0.00, 990.00, 5, 3, NOW(), NOW(), NOW());
```

#### 4. Insert Profit Distribution
```sql
INSERT INTO profit_distributions (distribution_date, mt5_account_id, total_equity, previous_equity, total_profit, safety_fund, sharing_profit, referral_share, investor_share, ecm_share, total_units, status, processed_at, created_at, updated_at) VALUES
('2024-01-15', 1, 5500.00, 4510.00, 990.00, 198.00, 792.00, 39.60, 396.00, 356.40, 11, 'COMPLETED', NOW(), NOW(), NOW());
```

#### 5. Insert Safety Fund
```sql
INSERT INTO safety_funds (profit_distribution_id, date, amount, cumulative_balance, transaction_type, description, created_at, updated_at) VALUES
(1, '2024-01-15', 198.00, 198.00, 'CREDIT', 'Daily safety fund from profit distribution', NOW(), NOW());
```

#### 6. Insert Investor Profit Logs
```sql
INSERT INTO investor_profit_logs (profit_distribution_id, investor_id, equity, units, unit_share_ratio, profit_amount, previous_balance, new_balance, created_at, updated_at) VALUES
(1, 1, 2000.00, 4, 0.36363636, 144.00, 0.00, 144.00, NOW(), NOW()),
(1, 2, 1000.00, 2, 0.18181818, 72.00, 0.00, 72.00, NOW(), NOW()),
(1, 3, 500.00, 1, 0.09090909, 36.00, 0.00, 36.00, NOW(), NOW()),
(1, 4, 2000.00, 4, 0.36363636, 144.00, 0.00, 144.00, NOW(), NOW());
```

#### 7. Update Investor Balances
```sql
UPDATE investors SET
  available_balance = available_balance + 144.00,
  total_profit = total_profit + 144.00,
  last_profit_distribution_at = NOW(),
  updated_at = NOW()
WHERE id = 1;

UPDATE investors SET
  available_balance = available_balance + 72.00,
  total_profit = total_profit + 72.00,
  last_profit_distribution_at = NOW(),
  updated_at = NOW()
WHERE id = 2;

UPDATE investors SET
  available_balance = available_balance + 36.00,
  total_profit = total_profit + 36.00,
  last_profit_distribution_at = NOW(),
  updated_at = NOW()
WHERE id = 3;

UPDATE investors SET
  available_balance = available_balance + 144.00,
  total_profit = total_profit + 144.00,
  last_profit_distribution_at = NOW(),
  updated_at = NOW()
WHERE id = 4;
```

#### 8. Referral Commission Example
Assume Investor B was referred by Investor A:

Insert Referral:
```sql
INSERT INTO referrals (referrer_id, referee_id, referral_code, is_active, referred_at, created_at, updated_at) VALUES
(1, 2, 'REF-ABC123', 1, '2024-01-01', NOW(), NOW());
```

Insert Referral Commission (5% of Investor B's profit):
```sql
INSERT INTO referral_commissions (profit_distribution_id, referrer_id, referee_id, commission_date, referee_profit, commission_rate, commission_amount, status, paid_at, created_at, updated_at) VALUES
(1, 1, 2, '2024-01-15', 72.00, 0.05, 3.60, 'PAID', NOW(), NOW(), NOW());
```

Update Referrer Balance:
```sql
UPDATE investors SET
  available_balance = available_balance + 3.60,
  total_profit = total_profit + 3.60,
  updated_at = NOW()
WHERE id = 1;
```

### Final Balances After Distribution
| Investor | Previous Balance | Investor Share | Referral Commission | Final Balance |
|----------|------------------|----------------|---------------------|---------------|
| Investor A | $0.00 | $144.00 | $3.60 | $147.60 |
| Investor B | $0.00 | $72.00 | $0.00 | $72.00 |
| Investor C | $0.00 | $36.00 | $0.00 | $36.00 |
| Investor D | $0.00 | $144.00 | $0.00 | $144.00 |

### API Response Example

#### POST /api/v1/mt5/sync
**Request:**
```json
{
  "api_key": "your-secure-api-key",
  "timestamp": 1705339200,
  "account": {
    "account_number": "12345678",
    "account_name": "ECM Master Account",
    "server": "broker.com",
    "balance": 5500.00,
    "equity": 5500.00,
    "margin": 1000.00,
    "free_margin": 4500.00,
    "floating_profit": 0.00,
    "currency": "USD",
    "leverage": 100
  },
  "trades": []
}
```

**Response (202 Accepted):**
```json
{
  "success": true,
  "message": "MT5 sync job queued successfully",
  "data": {
    "job_queued": true
  }
}
```

#### GET /api/v1/profit/distributions
**Response:**
```json
{
  "success": true,
  "data": {
    "current_page": 1,
    "data": [
      {
        "id": 1,
        "distribution_date": "2024-01-15",
        "mt5_account_id": 1,
        "total_equity": 5500.00,
        "previous_equity": 4510.00,
        "total_profit": 990.00,
        "safety_fund": 198.00,
        "sharing_profit": 792.00,
        "referral_share": 39.60,
        "investor_share": 396.00,
        "ecm_share": 356.40,
        "total_units": 11,
        "status": "COMPLETED",
        "processed_at": "2024-01-16T00:10:00.000000Z",
        "created_at": "2024-01-16T00:10:00.000000Z",
        "updated_at": "2024-01-16T00:10:00.000000Z"
      }
    ],
    "first_page_url": "...",
    "from": 1,
    "last_page": 1,
    "last_page_url": "...",
    "links": [...],
    "next_page_url": null,
    "path": "...",
    "per_page": 20,
    "prev_page_url": null,
    "to": 1,
    "total": 1
  }
}
```

#### GET /api/v1/investor/1/profit-logs
**Response:**
```json
{
  "success": true,
  "data": {
    "current_page": 1,
    "data": [
      {
        "id": 1,
        "profit_distribution_id": 1,
        "investor_id": 1,
        "equity": 2000.00,
        "units": 4,
        "unit_share_ratio": 0.36363636,
        "profit_amount": 144.00,
        "previous_balance": 0.00,
        "new_balance": 144.00,
        "created_at": "2024-01-16T00:10:00.000000Z",
        "updated_at": "2024-01-16T00:10:00.000000Z"
      }
    ],
    "first_page_url": "...",
    "from": 1,
    "last_page": 1,
    "last_page_url": "...",
    "links": [...],
    "next_page_url": null,
    "path": "...",
    "per_page": 20,
    "prev_page_url": null,
    "to": 1,
    "total": 1
  }
}
```
