# Deploy to Windows Server - Quick Guide

## 🎯 Goal
Make your Sales Dashboard accessible 24/7 to your sales force (6 users) using your company Windows server.

---

## 📋 What You Need

**Before Starting:**
- ✅ Windows Server (2016+ or Windows 10/11 Pro)
- ✅ Administrator access to the server
- ✅ Server has internet access
- ✅ Company domain ready (e.g., `sales.globalerc.pt`)
- ✅ Server public IP address
- ✅ Your application files (this folder)

**Time Required:** 60-90 minutes  
**Cost:** $0 (uses company infrastructure)

---

## 🚀 Deployment in 5 Steps

### Step 1: Copy Files to Server (5 minutes)
```powershell
# On your PC: Create deployment package
Compress-Archive -Path "C:\Users\Tiago Rebelo\Desktop\Globale RC\2-Vibe Coding\Sales Dashboard App_VScode\*" `
    -DestinationPath "C:\Users\Tiago Rebelo\Desktop\SalesDashboard.zip"

# Transfer SalesDashboard.zip to the server (via RDP, file share, etc.)
# Extract on server to: C:\Sales-Dashboard
```

### Step 2: Install Python (if not already installed) (5 minutes)
1. Download Python 3.11 from https://www.python.org/downloads/
2. Run installer with these options:
   - ✅ "Add Python to PATH"
   - ✅ "Install for all users"
3. Verify: `python --version` (should show 3.11.x)

### Step 3: Run Automated Installer (5 minutes)
```powershell
# On the server, open PowerShell AS ADMINISTRATOR
cd C:\Sales-Dashboard

# Create virtual environment
python -m venv venv
.\venv\Scripts\Activate.ps1

# Install dependencies
pip install -r requirements.txt

# Run the automated installer
.\install_windows_service.ps1
```

**The script will:**
- ✅ Install all required tools (NSSM)
- ✅ Create Windows Service "SalesDashboard"
- ✅ Configure auto-start on boot
- ✅ Configure auto-restart on failure
- ✅ Set up logging
- ✅ Open firewall port
- ✅ Start the service

**Verify it's running:**
```powershell
Get-Service -Name SalesDashboard
# Status should be: Running
```

**Test local access:**
```powershell
# Open browser on server
http://localhost:5000
# Should see your login page!
```

### Step 4: Make It Accessible From Internet (30 minutes)

#### Option A: Direct Access (Simple but no SSL)
1. Open Windows Firewall for port 5000
2. Configure router to forward port 5000 to server
3. Access via: `http://SERVER_PUBLIC_IP:5000`

**⚠️ Not recommended for production (no SSL/HTTPS)**

#### Option B: IIS Reverse Proxy (Recommended - with HTTPS)
Follow the detailed guide in `WINDOWS_SERVER_DEPLOYMENT.md`, Step 3-4.

**Quick summary:**
1. Install IIS, URL Rewrite, and ARR modules
2. Create IIS website pointing to `C:\Sales-Dashboard\static`
3. Configure reverse proxy to `http://localhost:5000`
4. Install Let's Encrypt SSL certificate (free, automated)
5. Enable HTTPS redirect

**Result:** Professional URL with SSL: `https://sales.globalerc.pt`

### Step 5: Update DNS & Google OAuth (10 minutes)

#### Update DNS
```
In your domain registrar or company DNS:
- Type: A
- Name: sales (or subdomain of your choice)
- Value: YOUR_SERVER_PUBLIC_IP
- TTL: 3600
```

Wait 5-60 minutes for DNS propagation.

#### Update Google OAuth
1. Go to https://console.cloud.google.com/
2. Navigate: APIs & Services → Credentials
3. Edit your OAuth 2.0 Client ID
4. Add redirect URI:
   ```
   https://sales.globalerc.pt/oauth2callback
   ```
5. Save

---

## ✅ Testing

```powershell
# 1. Check service is running
Get-Service -Name SalesDashboard

# 2. Check logs for errors
Get-Content C:\Sales-Dashboard\logs\service-output.log -Tail 50

# 3. Test from browser
# From server: http://localhost:5000
# From internet: https://sales.globalerc.pt
```

**Test checklist:**
- [ ] Service Status = Running
- [ ] Login page loads
- [ ] Google login works
- [ ] Dashboard displays data
- [ ] Client Intelligence buttons visible
- [ ] Can access from outside network
- [ ] HTTPS works (green padlock)

---

## 📊 Monitoring

**Check service status:**
```powershell
Get-Service -Name SalesDashboard
```

**View logs:**
```powershell
# Application logs
Get-Content C:\Sales-Dashboard\logs\service-output.log -Tail 50 -Wait

# Error logs
Get-Content C:\Sales-Dashboard\logs\service-error.log -Tail 50
```

**Restart if needed:**
```powershell
Restart-Service -Name SalesDashboard
```

**Check if port is listening:**
```powershell
netstat -ano | findstr :5000
```

---

## 🆘 Troubleshooting

### Service won't start
```powershell
# Check error logs
Get-Content C:\Sales-Dashboard\logs\service-error.log -Tail 50

# Common issues:
# - Python not in PATH
# - Missing dependencies: pip install -r requirements.txt
# - Wrong working directory in service config
```

### Can't access from internet
```powershell
# 1. Check service is running
Get-Service -Name SalesDashboard

# 2. Check firewall allows port 5000
netsh advfirewall firewall show rule name=SalesDashboard

# 3. Check DNS propagation
nslookup sales.globalerc.pt

# 4. If using IIS, check IIS logs
C:\inetpub\logs\LogFiles\
```

### Google OAuth not working
- Verify redirect URI exactly matches: `https://sales.globalerc.pt/oauth2callback`
- No trailing slash, correct protocol (https)
- DNS must be propagated first

---

## 📚 Additional Resources

**Detailed Guides:**
- `WINDOWS_SERVER_DEPLOYMENT.md` - Complete step-by-step (400+ lines)
- `WINDOWS_DEPLOYMENT_CHECKLIST.md` - Printable checklist with checkboxes
- `install_windows_service.ps1` - Automated installer script (heavily commented)

**Feature Documentation:**
- `CLIENT_INTELLIGENCE_GUIDE.md` - How to use Client Intelligence Panel
- `CLIENT_INTELLIGENCE_QUICKSTART.md` - Quick reference
- `README.md` - General application documentation

---

## 🎉 Success!

Once deployed, your sales team can access the dashboard:
- **URL:** `https://sales.globalerc.pt`
- **24/7 availability** (runs as Windows Service)
- **Auto-restart** on failures or reboot
- **Professional HTTPS** with valid certificate
- **No dependency on your PC**
- **No monthly fees** (uses company infrastructure)

---

## 💡 Key Advantages

| Feature | Your Solution | Alternative (PythonAnywhere) |
|---------|--------------|------------------------------|
| Cost | $0/month | $10/month |
| Payment | None needed | Credit card required |
| Control | Full control | Limited |
| Uptime | 24/7 (service) | 24/7 (managed) |
| Domain | Company domain | custom.pythonanywhere.com |
| SSL | Free (Let's Encrypt) | Free (included) |
| Scalable | Yes (add threads) | Yes (upgrade plan) |
| Data location | Your server | Their servers |

**You chose the best option for your company!** ✅

---

## 📞 Need Help?

1. Check `WINDOWS_SERVER_DEPLOYMENT.md` (detailed guide)
2. Review logs: `C:\Sales-Dashboard\logs\`
3. Check service status: `Get-Service -Name SalesDashboard`
4. Restart service: `Restart-Service -Name SalesDashboard`

**Common commands are in `WINDOWS_DEPLOYMENT_CHECKLIST.md`**

---

**Ready to deploy? Start with Step 1 above!** 🚀
