# Windows Server Deployment - Files Overview

This folder contains everything needed to deploy your Sales Dashboard to a Windows Server.

## 📦 Deployment Files Created

### 1. **run_production.py** ⭐
**What it does:** Starts your Flask application using Waitress WSGI server (production-ready for Windows)  
**When to use:** Automatically used by the Windows Service  
**Details:**
- Configured for 6 concurrent users (threads=6)
- Listens on port 5000
- Production environment mode
- HTTPS-aware (behind reverse proxy)

---

### 2. **install_windows_service.ps1** ⭐⭐⭐ (MOST IMPORTANT)
**What it does:** Automated PowerShell script that does EVERYTHING to install the Windows Service  
**When to use:** Run this ONCE on the Windows Server as Administrator  
**What it installs:**
- Chocolatey (package manager)
- NSSM (service manager)
- Creates "SalesDashboard" Windows Service
- Configures auto-start, auto-restart, logging
- Opens firewall port 5000
- Validates installation

**How to use:**
```powershell
# On Windows Server, open PowerShell AS ADMINISTRATOR
cd C:\Sales-Dashboard
.\install_windows_service.ps1
```

---

### 3. **WINDOWS_SERVER_DEPLOYMENT.md** ⭐⭐⭐ (COMPLETE GUIDE)
**What it is:** Comprehensive 400+ line deployment guide  
**When to use:** Read this for step-by-step instructions  
**Covers:**
- Prerequisites checklist
- Step-by-step deployment (8 major steps)
- IIS reverse proxy setup
- SSL certificate (Let's Encrypt)
- DNS configuration
- Google OAuth update
- Security hardening
- Monitoring and troubleshooting
- Production checklist

**Start here if you want detailed instructions!**

---

### 4. **WINDOWS_DEPLOYMENT_CHECKLIST.md** ⭐⭐
**What it is:** Printable checklist with checkboxes  
**When to use:** Print it out and check off each step as you complete it  
**Contains:**
- Pre-deployment checklist
- Installation steps with checkboxes
- Testing checklist
- Post-deployment monitoring
- Quick command reference
- Emergency contacts section

**Perfect for tracking your progress!**

---

### 5. **DEPLOY_TO_WINDOWS_SERVER.md** ⭐⭐ (QUICK START)
**What it is:** Quick 5-step deployment guide  
**When to use:** If you want a simpler overview without all the details  
**Covers:**
- 5 main deployment steps
- Testing procedures
- Monitoring commands
- Common troubleshooting
- Success criteria

**Start here if you prefer concise instructions!**

---

### 6. **web.config** ⭐
**What it is:** IIS configuration file for reverse proxy  
**When to use:** Copy this to your IIS website root  
**Configures:**
- Reverse proxy to `http://localhost:5000`
- HTTPS redirect (forces SSL)
- Security headers
- Static content caching
- Compression

**Location on server:** `C:\inetpub\wwwroot\SalesDashboard\web.config` (or your IIS site path)

---

### 7. **requirements.txt** (UPDATED) ✅
**What it is:** Python dependencies list  
**Changes made:** Added `waitress` (Windows WSGI server)  
**When to use:** During installation when you run `pip install -r requirements.txt`

---

## 🚀 Quick Start (3 Steps to Get Running)

### Step 1: Copy to Server
```powershell
# On your PC: Create ZIP
Compress-Archive -Path "*" -DestinationPath "SalesDashboard.zip"

# Transfer to server and extract to: C:\Sales-Dashboard
```

### Step 2: Install Python (if needed)
Download Python 3.11 from https://www.python.org/downloads/  
✅ Check "Add to PATH"  
✅ Check "Install for all users"

### Step 3: Run Automated Installer
```powershell
# On server: PowerShell AS ADMINISTRATOR
cd C:\Sales-Dashboard
python -m venv venv
.\venv\Scripts\Activate.ps1
pip install -r requirements.txt
.\install_windows_service.ps1
```

**Done!** Your service is now running on `http://localhost:5000`

---

## 📖 Recommended Reading Order

**For first-time deployment:**
1. Read `DEPLOY_TO_WINDOWS_SERVER.md` (quick overview)
2. Use `WINDOWS_DEPLOYMENT_CHECKLIST.md` (print and follow)
3. Reference `WINDOWS_SERVER_DEPLOYMENT.md` (for detailed steps)

**For advanced configuration:**
1. Read `WINDOWS_SERVER_DEPLOYMENT.md` (complete guide)
2. Customize `web.config` (if using IIS)
3. Review security section in deployment guide

---

## 🎯 What Happens After Deployment

**Service Name:** `SalesDashboard`  
**Service Status:** Running (auto-start on boot)  
**Internal URL:** `http://localhost:5000`  
**External URL:** `https://sales.globalerc.pt` (after IIS + DNS setup)  
**Logs Location:** `C:\Sales-Dashboard\logs\`

**Access for sales team:**
- 24/7 availability
- Professional domain with HTTPS
- 6 concurrent users supported
- No dependency on your PC
- Auto-restart on failures

---

## ✅ Deployment Success Criteria

After deployment, verify:
- [ ] Service "SalesDashboard" is Running
- [ ] Can access `http://localhost:5000` on server
- [ ] Can access `https://sales.globalerc.pt` from internet
- [ ] Google login works
- [ ] Dashboard loads data
- [ ] Client Intelligence buttons work
- [ ] All 6 sales users can access
- [ ] Service survives server reboot

---

## 🆘 Quick Troubleshooting

```powershell
# Check service status
Get-Service -Name SalesDashboard

# View logs
Get-Content C:\Sales-Dashboard\logs\service-output.log -Tail 50

# Restart service
Restart-Service -Name SalesDashboard

# Check if port is open
netstat -ano | findstr :5000
```

---

## 💡 Architecture Overview

```
Internet
    ↓
DNS (sales.globalerc.pt)
    ↓
Windows Server
    ↓
IIS (Port 80/443) ← Let's Encrypt SSL
    ↓ [Reverse Proxy via web.config]
Flask App (Port 5000) ← Waitress WSGI Server
    ↓ [Windows Service]
NSSM Service Manager ← Auto-start, Auto-restart
    ↓
run_production.py
    ↓
app.py (Your Flask Application)
    ↓
Google Sheets (Data Source)
```

---

## 📊 File Dependencies

```
install_windows_service.ps1 (RUN THIS FIRST)
    ↓ installs service
    ↓ uses ↓
run_production.py (Started by service)
    ↓ imports ↓
app.py (Your Flask app)
    ↓ reads ↓
requirements.txt (Dependencies)

web.config (For IIS only - optional but recommended)
    ↓ configures ↓
IIS Reverse Proxy (External access)
```

---

## 🎓 Key Concepts

**NSSM:** "Non-Sucking Service Manager" - Makes any program run as a Windows Service  
**Waitress:** Production-ready WSGI server optimized for Windows  
**WSGI:** Web Server Gateway Interface - connects Flask to web servers  
**IIS:** Internet Information Services - Microsoft's web server  
**ARR:** Application Request Routing - IIS module for reverse proxy  
**Let's Encrypt:** Free automated SSL certificates  
**win-acme:** Windows tool to get Let's Encrypt certificates

---

## 📞 Support Resources

**Official Documentation:**
- Flask: https://flask.palletsprojects.com/
- Waitress: https://docs.pylonsproject.org/projects/waitress/
- NSSM: https://nssm.cc/
- IIS: https://www.iis.net/
- win-acme: https://www.win-acme.com/

**Your Deployment Guides:**
- `WINDOWS_SERVER_DEPLOYMENT.md` - Detailed step-by-step
- `DEPLOY_TO_WINDOWS_SERVER.md` - Quick 5-step guide
- `WINDOWS_DEPLOYMENT_CHECKLIST.md` - Progress tracker

---

## ✨ Summary

**You have everything needed to deploy!**

1. ✅ Automated installer script
2. ✅ Production server launcher
3. ✅ Complete deployment guide (400+ lines)
4. ✅ Quick start guide
5. ✅ Printable checklist
6. ✅ IIS configuration file
7. ✅ Updated requirements

**Total Cost:** $0 (uses company infrastructure)  
**Deployment Time:** 60-90 minutes  
**Result:** Professional 24/7 sales dashboard accessible to your team! 🚀

---

**Ready to deploy? Open `DEPLOY_TO_WINDOWS_SERVER.md` and start!**
