# ROA2WEB - Windows Deployment

**Arhitectură:** Ultrathin Monolith | **Serviciu:** `ROA2WEB-Backend` (port 8000)

---

## Quick Start

### 1. Instalare Server Nou

```powershell
# Pe Windows Server (PowerShell Administrator)
cd C:\path\to\deployment\windows\scripts
.\Install-ROA2WEB.ps1
```

### 2. Configurare

```powershell
notepad C:\inetpub\wwwroot\roa2web\backend\.env

# Module control:
MODULE_REPORTS_ENABLED=true
MODULE_DATA_ENTRY_ENABLED=true
MODULE_TELEGRAM_ENABLED=true
```

### 3. Start

```powershell
Start-Service ROA2WEB-Backend
```

---

## Deployment (Actualizări)

### Opțiunea A: Din Windows (Publish-And-Deploy.ps1)

```powershell
# Pe mașina de dezvoltare Windows
cd deployment\windows\scripts
.\Publish-And-Deploy.ps1

# Non-interactiv:
.\Publish-And-Deploy.ps1 -NonInteractive -Action Build -Component All
```

### Opțiunea B: Din Linux/LXC (deploy.sh)

```bash
# Pe mașina de dezvoltare Linux
cd deployment/linux
./deploy.sh              # Full deploy (frontend + backend)
./deploy.sh frontend     # Doar frontend
./deploy.sh backend      # Doar backend
./deploy.sh test         # Test conexiune SSH
```

**Ambele metode:**
1. Build frontend (npm) + copiere backend
2. Transfer la server (`C:\Temp\deploy-YYYYMMDD-HHmmss\`)
3. Server-ul auto-deploy în 5 minute (sau manual)

---

## Management

### Consolă Interactivă (Recomandat)

```powershell
.\ROA2WEB-Console.ps1
```

### Comenzi Rapide

```powershell
# Service
Start-Service ROA2WEB-Backend
Stop-Service ROA2WEB-Backend
Restart-Service ROA2WEB-Backend
Get-Service ROA2WEB-Backend

# Status & Health
.\ROA2WEB-Console.ps1 -NonInteractive -Action Status
Invoke-WebRequest http://localhost:8000/health

# Logs
Get-Content C:\inetpub\wwwroot\roa2web\logs\backend-stdout.log -Tail 50
Get-Content C:\inetpub\wwwroot\roa2web\logs\backend-stderr.log -Tail 20

# Deploy manual
.\ROA2WEB-Console.ps1 -NonInteractive -Action DeployAll -PackagePath "C:\Temp\deploy-XXXXXXXX"
```

---

## Structura Server

```
C:\inetpub\wwwroot\roa2web\
├── backend\          # FastAPI (Reports, Data Entry, Telegram modules)
│   └── .env          # Configurare (MODULE_*_ENABLED flags)
├── frontend\         # Vue.js SPA + web.config
├── shared\           # Module Python partajate (auth, db)
├── logs\             # backend-stdout.log, backend-stderr.log
└── backups\          # Backup-uri automate
```

---

## Arhitectură

```
Client → IIS (80/443)
           │
           ├─ /roa2web/api/* → ROA2WEB-Backend (localhost:8000)
           │                   ├── Reports Module → Oracle DB
           │                   ├── Data Entry Module → SQLite
           │                   └── Telegram Module
           │
           └─ /roa2web/* → Frontend (Vue.js SPA)
```

**Single Service:** `ROA2WEB-Backend` pe port 8000
**Module Control:** Via `.env` flags (fără restart cod)

---

## Workflow Deployment

```
DEV MACHINE                          WINDOWS SERVER
───────────                          ──────────────

Publish-And-Deploy.ps1
      sau                     ───►   C:\Temp\deploy-*\
deploy.sh (Linux)                           │
                                            ▼
                                     Check-And-Deploy.ps1
                                     (scheduled task, 5 min)
                                            │
                                            ▼
                                     ROA2WEB-Console.ps1
                                     ├── Stop Service
                                     ├── Backup
                                     ├── Deploy Files
                                     ├── Start Service
                                     └── Health Check
                                            │
                                            ▼
                                     ✅ PRODUCTION RUNNING
```

---

## Data Preserved During Deploy

During deployment, the following are **automatically preserved** (not overwritten):

### Files Preserved

| Item | Location | Description |
|------|----------|-------------|
| `.env` | `backend/.env` | Environment configuration |
| `data/` | `backend/data/` | All SQLite databases and uploads |

### Data Directory Structure

```
backend/data/
├── receipts/           # Data Entry module
│   ├── receipts*.db    # SQLite database (bonuri, aprobări, workflow)
│   └── uploads/        # User-uploaded files (PDF/images)
├── telegram/           # Telegram Bot module
│   └── telegram*.db    # Bot auth/session data
└── cache/              # Reports module
    └── roa2web_cache*.db  # L2 cache (can be regenerated)
```

### Preservation Mechanism

The `ROA2WEB-Console.ps1` script:

1. **Before deploy**: Copies `data/` to `%TEMP%\roa2web-data-backup-*`
2. **During deploy**: Deletes and replaces entire `backend/` folder
3. **After deploy**: Restores `data/` from backup

**Safe to lose** (regenerated automatically):
- `cache/*.db` - Reports cache, rebuilt on first query

**Critical data** (must be preserved):
- `receipts/*.db` - User data, approvals, workflow states
- `telegram/*.db` - Bot authentication tokens

### Manual Backup

```powershell
# Create manual backup before major operations
Copy-Item -Path "C:\inetpub\wwwroot\roa2web\backend\data" `
          -Destination "C:\backups\roa2web-data-$(Get-Date -Format 'yyyyMMdd')" `
          -Recurse
```

---

## Troubleshooting

| Problemă | Verificare | Soluție |
|----------|------------|---------|
| Service nu pornește | `Get-Content ...\backend-stderr.log -Tail 30` | Verifică .env, port 8000 |
| API 502/504 | `Invoke-WebRequest http://localhost:8000/health` | Restart service |
| Frontend nu se încarcă | `iisreset` | Verifică IIS, web.config |
| Auto-deploy nu merge | `Get-ScheduledTask ROA2WEB-AutoDeploy` | `.\Setup-AutoDeploy.ps1` |

---

## Fișiere web.config

| Fișier | Scop | Când se folosește |
|--------|------|-------------------|
| `public/web.config` | Sub-aplicație IIS (/roa2web) | La fiecare deploy (via Vite) |
| `deployment/windows/config/web.config` | Server IIS complet | La instalare nouă |

**Notă:** Ambele au configurat `no-cache` pentru API (backend gestionează cache-ul).

---

## Documentație Detaliată

- **HTTPS Setup:** `docs/HTTPS_SETUP.md`
- **2-Tier IIS:** `docs/TWO-TIER-IIS-DEPLOYMENT.md`
- **Telegram Bot:** `docs/TELEGRAM-BOT-DEPLOYMENT.md`

---

## Cerințe Sistem

| Resursă | Minim | Recomandat |
|---------|-------|------------|
| OS | Windows Server 2016 | Windows Server 2019+ |
| RAM | 4 GB | 8 GB (16 GB cu OCR) |
| CPU | 2 cores | 4 cores |
| Python | 3.11+ | 3.11+ |
| IIS | URL Rewrite + ARR | URL Rewrite + ARR |

---

*ROA2WEB - Ultrathin Monolith Architecture*
*Last Updated: 2025-01-22*
