# Phase 12 Rollback — Session Cleanup + Monitoring

## What Phase 12 Added

**New files:**
- `app/Console/Commands/WaniCleanupCommand.php` — cleanup logic

**Modified files:**
- `routes/console.php` — scheduled the cleanup command every 5 min
- `config/logging.php` — added `wani` log channel

**Database changes:** NONE (read-only cleanup)

## How to Roll Back

```bash
cd c:/xampp/htdocs/Android_App/pmwani_mobile_app_backend

# 1. Restore files
cp rollback/phase-12/console.php.before routes/console.php
cp rollback/phase-12/logging.php.before config/logging.php

# 2. Delete cleanup command
rm app/Console/Commands/WaniCleanupCommand.php

# 3. Clear cache
php artisan config:clear
```

Back to Phase 11 state (routes + logs unchanged elsewhere).

## What Phase 12 Does

### Cleanup command: `php artisan wani:cleanup`

Runs every 5 minutes via scheduler. Removes:

| What | Criteria |
|---|---|
| Expired auth tokens | `wani_tokens.expires_at < now()` |
| Stale OTP transactions | `wani_auth_transactions.created_at < now() - 1 hour` |
| Expired wifi sessions | `wifi_sessions` where status=active and started_at + 1800s < now() |

Flags:
- `--dry-run` → reports what WOULD be deleted, doesn't touch DB

### Dedicated log channel

All PM-WANI v1 operations now log to:
```
storage/logs/wani-2026-04-22.log (rotates daily, 30-day retention)
```

Separates PM-WANI logs from general `laravel.log` for easier debugging.

## How to Manually Test Cleanup

```bash
# Dry run (safe)
php artisan wani:cleanup --dry-run

# Actually clean
php artisan wani:cleanup
```

Expected output:
```
Cleanup complete:
+---------------------------+-------+
| Metric                    | Count |
+---------------------------+-------+
| Expired tokens removed    | 3     |
| Stale transactions removed| 5     |
| Sessions expired          | 1     |
+---------------------------+-------+
```

## Server Deployment

### 1. Upload files

```bash
cd c:/xampp/htdocs/Android_App/pmwani_mobile_app_backend

scp -P 21212 app/Console/Commands/WaniCleanupCommand.php immunity@147.93.30.127:/tmp/WaniCleanupCommand.php
scp -P 21212 routes/console.php immunity@147.93.30.127:/tmp/console.php
scp -P 21212 config/logging.php immunity@147.93.30.127:/tmp/logging.php

ssh immunity@147.93.30.127 -p 21212 "\
  sudo cp /tmp/WaniCleanupCommand.php /var/www/mobile_app_backend/app/Console/Commands/WaniCleanupCommand.php && \
  sudo cp /tmp/console.php /var/www/mobile_app_backend/routes/console.php && \
  sudo cp /tmp/logging.php /var/www/mobile_app_backend/config/logging.php && \
  sudo chown www-data:www-data /var/www/mobile_app_backend/app/Console/Commands/WaniCleanupCommand.php /var/www/mobile_app_backend/routes/console.php /var/www/mobile_app_backend/config/logging.php && \
  cd /var/www/mobile_app_backend && sudo php artisan config:clear"
```

### 2. Add cron entry on server (critical — scheduler won't run without this)

```bash
ssh immunity@147.93.30.127 -p 21212

# On server:
sudo crontab -e -u www-data

# Add this line:
* * * * * cd /var/www/mobile_app_backend && php artisan schedule:run >> /dev/null 2>&1
```

Without this cron entry, `wani:cleanup` will never auto-run. Verify:

```bash
# Check that scheduler sees the command:
sudo -u www-data php /var/www/mobile_app_backend/artisan schedule:list

# Expected output includes:
# */5 * * * *    php artisan wani:cleanup    Next Due: XX seconds from now
```

### 3. Verify first manual run

```bash
sudo -u www-data php /var/www/mobile_app_backend/artisan wani:cleanup --dry-run
```

Should report counts without deleting.

## Server Rollback

```bash
cd c:/xampp/htdocs/Android_App/pmwani_mobile_app_backend

scp -P 21212 rollback/phase-12/console.php.before immunity@147.93.30.127:/tmp/console.php
scp -P 21212 rollback/phase-12/logging.php.before immunity@147.93.30.127:/tmp/logging.php

ssh immunity@147.93.30.127 -p 21212 "\
  sudo cp /tmp/console.php /var/www/mobile_app_backend/routes/console.php && \
  sudo cp /tmp/logging.php /var/www/mobile_app_backend/config/logging.php && \
  sudo rm /var/www/mobile_app_backend/app/Console/Commands/WaniCleanupCommand.php && \
  cd /var/www/mobile_app_backend && sudo php artisan config:clear"
```

## Important Notes

- Cron is **required** for scheduler to fire. Without it, you'd need to manually run `php artisan wani:cleanup` via cron some other way.
- The `wani` log channel uses daily rotation with 30-day retention. Adjust via `WANI_LOG_DAYS` env if you need shorter/longer.
- Cleanup is **idempotent** — safe to run repeatedly.
- Dry-run mode is safe for production verification.
- Sessions with >30 min are expired, but this can be adjusted in the command if your `session_seconds` differs.
