# 🎯 Test User View Switcher - User Guide

## Overview
The test user (`tiagovibecoding@gmail.com`) now has a **View Switcher** that allows you to toggle between viewing José Amor's data and Hélder Oliveira's data. This is perfect for demonstrating how each salesperson sees their own restricted view.

---

## ✨ What's New

### For Test User Only
A dropdown selector appears in the top-right corner of the dashboard (next to your user info) labeled:
```
👁️ Vista de Teste: [José Amor ▼] or [Hélder Oliveira ▼]
```

### How It Works
1. **Login** with `tiagovibecoding@gmail.com`
2. **Default View:** José Amor (you'll see all his clients and sales)
3. **Switch View:** Click the dropdown and select "Hélder Oliveira"
4. **Page Reloads:** Dashboard automatically refreshes with Hélder's data
5. **Switch Back:** Select "José Amor" to return to his view

---

## 🎓 Use Cases for Presentation

### Scenario 1: Show José Amor His View
1. Login as test user
2. Keep default "José Amor" selected
3. Navigate the dashboard:
   - See only José's clients
   - View his sales data
   - Click "📊 Inteligência" on his clients
   - Show his metrics and recommendations
4. **Say:** "This is what José Amor sees when he logs in - only his clients and data"

### Scenario 2: Show Hélder Oliveira His View
1. Login as test user
2. Switch to "Hélder Oliveira" in dropdown
3. Page reloads with different data:
   - Different client list
   - Different sales numbers
   - Different metrics
4. **Say:** "And this is Hélder's view - completely different clients and sales"

### Scenario 3: Compare Both Views
1. Open dashboard in two browser windows side-by-side
2. Window 1: Select "José Amor"
3. Window 2: Select "Hélder Oliveira"
4. **Show:** How each salesperson sees only their own data

---

## 📊 What Changes When You Switch Views

| Feature | José Amor View | Hélder Oliveira View |
|---------|----------------|----------------------|
| **Clients** | Only José's clients | Only Hélder's clients |
| **Sales Data** | José's sales numbers | Hélder's sales numbers |
| **Maps/Zones** | José's territories | Hélder's territories |
| **Client Intelligence** | José's client metrics | Hélder's client metrics |
| **Priority Actions** | José's action items | Hélder's action items |
| **Exports** | José's data only | Hélder's data only |
| **Commission Rate** | 8.5% | 7% |

---

## 🔒 Security Features

✅ **Only Available to Test User**
- `joseamor.globalerc@gmail.com` - Cannot switch, sees only his data
- `helderoliveira.globalerc@gmail.com` - Cannot switch, sees only his data
- `tiagovibecoding@gmail.com` - Can switch between both views

✅ **Session-Based**
- View selection stored in user session
- Persists across page navigations
- Resets when you logout

✅ **Server-Side Enforcement**
- Data filtering happens on the server
- Not bypassable from browser
- API calls respect the selected view

---

## 🛠️ Technical Details

### Implementation
- **Helper Function:** `get_user_comercials(user_email)`
  - Returns list of comercials user can see
  - For test user: returns `[session.get('test_user_view')]`
  - For real users: returns `SALES_ACCESS_MAP[email]`

- **Switch Endpoint:** `/switch-test-view` (POST)
  - Accepts: `{"view": "José Amor"}` or `{"view": "Hélder Oliveira"}`
  - Updates: `session['test_user_view']`
  - Returns: Success message

- **UI Component:** Dropdown in dashboard header
  - Only rendered if `user_email == 'tiagovibecoding@gmail.com'`
  - JavaScript auto-reloads page on change
  - Preserves filter selections

### Data Filtering
All these functions now use `get_user_comercials()`:
- ✅ Dashboard main data
- ✅ Zona clients view
- ✅ Client details page
- ✅ Client Intelligence Panel
- ✅ Annual reports
- ✅ Performance reports
- ✅ Export functions

---

## 💡 Best Practices for Demos

### Before the Presentation
1. Test both views thoroughly
2. Note key differences (client names, numbers)
3. Prepare 2-3 specific clients to showcase per view
4. Check that Client Intelligence works for both

### During the Presentation
**For José Amor's Team:**
1. Start with José's view
2. Show his client list
3. Demo Client Intelligence on his top client
4. Show priority actions specific to his portfolio

**For Hélder Oliveira's Team:**
1. Switch to Hélder's view
2. Show his different client list
3. Demo Client Intelligence on his top client
4. Show his priority actions

**For Combined Demo:**
1. Show José's view first
2. Point out his metrics (e.g., "José has 45 clients, €250K revenue")
3. Switch to Hélder's view
4. Point out Hélder's metrics (e.g., "Hélder has 38 clients, €210K revenue")
5. Emphasize data isolation and privacy

### After Switching Views
- Wait 2-3 seconds for page reload
- Verify the data changed (check client names)
- Confirm commissions differ (José 8.5%, Hélder 7%)

---

## 🎬 Demo Script Example

```
"Let me show you how the role-based access control works for our sales team.

[Login as tiagovibecoding@gmail.com]

Right now, I'm viewing José Amor's dashboard. Notice in the top right, 
I have a special test mode switcher. 

[Point to dropdown]

José can only see his 45 clients and his sales data. Let me show you 
the Client Intelligence feature for one of his top clients.

[Click 📊 Inteligência on a client]

See? Complete analytics for this client - revenue trends, product mix, 
margins, buying patterns. José can use this to prioritize his visits.

[Go back to dashboard]

Now, let me switch to Hélder Oliveira's view.

[Select 'Hélder Oliveira' in dropdown, wait for reload]

Notice the data completely changed! Different clients, different sales 
numbers. Hélder has 38 clients and his own metrics.

[Click 📊 Inteligência on one of Hélder's clients]

Same powerful analytics, but for Hélder's clients only. This ensures 
each salesperson sees only their data - privacy and focus.

[Switch back to José's view if needed]

This is exactly what each salesperson will see when they login from 
anywhere - office, home, mobile device. Clean, focused, secure."
```

---

## 🚨 Important Notes

### Don't Forget
- ⚠️ The dropdown only appears for `tiagovibecoding@gmail.com`
- ⚠️ Real salespeople won't see this switcher
- ⚠️ View persists until you switch again or logout
- ⚠️ Logout clears the view selection (defaults to José)

### Known Behavior
- ✅ Page reloads when switching (expected)
- ✅ Filters reset on view switch (expected)
- ✅ Commission rates change automatically
- ✅ Maps and zones update to show correct territories

### Testing Checklist
Before each demo, verify:
- [ ] Can login as test user
- [ ] Dropdown appears in top right
- [ ] José's view shows correctly (default)
- [ ] Can switch to Hélder's view
- [ ] Data changes after switch
- [ ] Client Intelligence works for both
- [ ] Can switch back to José
- [ ] Logout and login resets to José

---

## 📈 View Comparison Data

### José Amor (Typical Data)
- **Clients:** ~45 active clients
- **Revenue:** ~€250,000 monthly
- **Top Zone:** Norte
- **Commission:** 8.5%
- **Specialty:** Industrial clients

### Hélder Oliveira (Typical Data)
- **Clients:** ~38 active clients
- **Revenue:** ~€210,000 monthly
- **Top Zone:** Centro
- **Commission:** 7%
- **Specialty:** Retail/distribution

*(Actual numbers depend on your Google Sheets data)*

---

## 🔄 Reverting/Troubleshooting

### If View Switcher Not Visible
1. Confirm you're logged in as `tiagovibecoding@gmail.com`
2. Hard refresh the page (Ctrl+F5)
3. Check browser console for JavaScript errors
4. Logout and login again

### If View Doesn't Switch
1. Check browser console for errors
2. Verify network request to `/switch-test-view` succeeds
3. Try manually: `session['test_user_view']` = 'Hélder Oliveira' in Python
4. Restart Flask server if needed

### To Remove Feature Later
If you want to remove this for production:
1. Search for `get_user_comercials` and replace with `SALES_ACCESS_MAP[user_email]`
2. Remove the `/switch-test-view` route
3. Remove the UI dropdown from dashboard HTML
4. Remove the JavaScript handler

---

## ✅ Quick Start

**Right now, to test:**

1. Open browser: `http://localhost:5000`
2. Login with: `tiagovibecoding@gmail.com`
3. Look for dropdown: "👁️ Vista de Teste: José Amor"
4. Click dropdown, select "Hélder Oliveira"
5. Watch page reload with different data
6. Click "📊 Inteligência" on any client
7. See metrics for selected view's clients

**You're ready to present!** 🎉

---

## 🎯 Summary

This feature gives you the power to demonstrate both salespeople's views without logging out and switching accounts. Perfect for:

- ✅ Sales force training
- ✅ Feature demonstrations
- ✅ Privacy/security proof
- ✅ Before/after comparisons
- ✅ Testing data filtering
- ✅ Client Intelligence demos

**The real users (José and Hélder) will never see this switcher - they only see their own data, always.**
