# SMS App - Auto-Deploy Setup Guide

## 🚀 Real-Time FTP Deployment

Your SMS app is configured to **automatically upload changes** to your FTPS server as you modify files.

---

## Quick Start

### Option 1: VS Code Integration (Recommended)

1. **Open the project in VS Code:**
   ```
   File → Open Folder → D:\SMS
   ```

2. **Open the Task Runner:**
   - Press `Ctrl+Shift+P`
   - Type: `Tasks: Run Task`
   - Select: `Watch & Deploy`

3. **VS Code will:**
   - ✓ Auto-save files after 500ms of inactivity
   - ✓ Watch for file changes
   - ✓ Deploy to FTP automatically
   - ✓ Show deploy status in the integrated terminal

### Option 2: Node.js Watcher

**Requirements:** Node.js installed (download from nodejs.org)

```bash
cd D:\SMS
node watch-deploy.js
```

**Terminal output:**
```
📂 Watching: D:\SMS
🔗 Target:  ftps://ftp.shanesmith.com.au:21
✓ Watcher ready. Making changes will trigger FTP upload.

[12:34:56] 📝 api.php changed
[12:34:57] 🚀 Deploying to FTP server...
[12:34:59] ✓ Deploy successful
```

### Option 3: PowerShell Watcher (Windows)

```bash
# Option A: Run directly
powershell -ExecutionPolicy Bypass -File watch-deploy.ps1

# Option B: Double-click start-watcher.bat
start-watcher.bat
```

### Option 4: Manual One-Time Deploy

```bash
cd D:\SMS
php deploy.php
```

---

## How It Works

### File Watcher Behavior

- **Watches:** All files in `D:\SMS` and subdirectories
- **Ignores:** Files in `.deployignore` (data files, credentials, etc.)
- **Debounce:** Waits 1 second after change before deploying
- **Min Interval:** Waits at least 3 seconds between deploys (prevents spam)

### Example Workflow

```
1. You edit api.php in VS Code
2. VS Code auto-saves (after 500ms idle)
3. File watcher detects change
4. Waits 1 second (debounce)
5. Runs deploy.php
6. Files upload to FTPS
7. Ready for testing on production
```

---

## VS Code Configuration

### Auto-Save Settings (`.vscode/settings.json`)

```json
{
  "files.autoSave": "afterDelay",
  "files.autoSaveDelay": 500
}
```

**Change to:**
- `"onFocusChange"` - Save when you switch tabs/windows
- `"onWindowChange"` - Save when you switch applications
- `"off"` - Disable auto-save

### Start Watcher on Folder Open

The watcher will prompt to start when you open the folder. Choose:
- **Start:** Runs `Deploy to FTP` task immediately
- **Don't Show Again:** Disable the prompt

---

## 🔧 Configuration

### Deployment Settings

**File:** `deploy.php` (lines 7-10)

```php
$ftpHost = 'your.ftp.server.com';
$ftpPort = 21;
$ftpUser = 'your_ftp_username';
$ftpPass = 'YOUR_FTP_PASSWORD';
$ftpPath = '/public_html/sms'; // Change to your remote path
```

### Watcher Settings

**Node.js:** `watch-deploy.js` (lines 8-9)
```javascript
const DEBOUNCE_MS = 1000; // Wait time before deploy
const MIN_DEPLOY_INTERVAL = 3000; // Cooldown between deploys
```

**PowerShell:** `watch-deploy.ps1` (lines 6-7)
```powershell
$debounceMs = 1000
$minDeployInterval = 3000
```

---

## 📊 Automatic Deployment Flow

```
File Modified in VS Code
         ↓
Auto-Save (500ms idle)
         ↓
File Watcher Detects Change
         ↓
Debounce (1000ms wait)
         ↓
Check Min Interval (3000ms)
         ↓
Run: php deploy.php
         ↓
Connect to FTPS
         ↓
Upload Changed Files
         ↓
Log Results to Terminal
         ↓
Ready for Production Testing ✓
```

---

## 🛡️ Security Notes

### Files NOT Uploaded (Protected)

Listed in `.deployignore`:
- `messages.json` - User conversations
- `token_cache.json` - OAuth2 tokens
- `webhook_log.json` - Debug logs
- `.env` - Environment variables
- `.git/` - Git history

### Protect Your Credentials

1. **Change FTP password immediately** (in hosting control panel)
2. **Move credentials to .env:**
   ```php
   // In deploy.php
   $ftpPass = getenv('FTP_PASS');
   ```
3. **Add .env to .gitignore** (already done)

---

## 🐛 Troubleshooting

### "watch-deploy.js not found"
- Ensure you're in `D:\SMS` directory
- File is created by setup script

### Watcher starts but doesn't deploy
- Check FTP credentials in `deploy.php`
- Verify FTPS server is accessible: `ping ftp.shanesmith.com.au`
- Check permissions on remote FTP directory

### "Node is not recognized"
- Node.js not installed: https://nodejs.org/
- Or use PowerShell watcher instead: `watch-deploy.ps1`

### Uploads too frequent
- Increase `DEBOUNCE_MS` in watch script
- VS Code formatting tools trigger extra saves; disable with `"editor.formatOnSave": false`

### Nothing uploading in VS Code
- Open VS Code terminal: `` Ctrl+` ``
- Run: `Tasks: Run Task` → `Watch & Deploy`
- Check terminal for errors

---

## ✅ Verify Setup

### Check files were created:
```bash
cd D:\SMS
ls -la .vscode/
ls -la *.js
ls -la *.ps1
ls -la *.bat
```

### Test manual deploy:
```bash
php deploy.php
```

### Test file watcher:
1. Start watcher: `node watch-deploy.js`
2. Edit `index.php` and save
3. Check terminal for upload notification

---

## 📋 Commands Reference

| Task | Command |
|------|---------|
| Manual deploy | `php deploy.php` |
| Node watcher | `node watch-deploy.js` |
| PowerShell watcher | `powershell -ExecutionPolicy Bypass -File watch-deploy.ps1` |
| Auto-start (Windows) | Double-click `start-watcher.bat` |
| VS Code task | `Ctrl+Shift+P` → `Tasks: Run Task` → `Watch & Deploy` |

---

## 🎯 Best Practices

1. **Keep watcher running** while developing
2. **Check FTP logs** periodically for failures
3. **Test on remote** server before major changes
4. **Backup** before large refactors
5. **Regenerate credentials** monthly

---

**Ready? Open `D:\SMS` in VS Code and start the Watch & Deploy task!** 🚀
