# Auto-Update Scheduler - Weather & Disaster Risk Scores

## ✅ Scheduled Tasks (Every 2 Hours)

Laravel Scheduler telah dikonfigurasi untuk auto-update data setiap 2 jam.

### Tasks yang Berjalan:

1. **Weather Sync** - Setiap 2 jam
   ```
   php artisan countries:sync --weather
   ```
   - Update weather data untuk semua negara
   - Log: `storage/logs/weather-sync.log`

2. **Disaster Sync** - Setiap 2 jam  
   ```
   php artisan countries:sync --disasters
   ```
   - Update earthquake/disaster data
   - Log: `storage/logs/disaster-sync.log`

3. **Risk Score Recalculation** - Setiap 2 jam
   ```
   php artisan countries:sync --risk
   ```
   - Recalculate semua risk scores
   - Log: `storage/logs/risk-sync.log`

4. **Economy Sync** - Sekali per hari (03:00)
   ```
   php artisan countries:sync --economy
   ```
   - Update data GDP, inflation dari World Bank
   - Log: `storage/logs/economy-sync.log`

---

## 🚀 Cara Menjalankan Scheduler

### **Option 1: Schedule Worker (Recommended for Development)**
Jalankan di terminal dan biarkan running:
```bash
php artisan schedule:work
```

### **Option 2: Windows Task Scheduler (Recommended for Production)**

1. Buka **Task Scheduler** Windows
2. Create New Task dengan settings:
   - **Trigger**: Repeat every 1 minute
   - **Action**: Start a program
   - **Program**: `C:\xampp\php\php.exe`
   - **Arguments**: `C:\xampp\htdocs\global-supply-chain\artisan schedule:run`
   - **Start in**: `C:\xampp\htdocs\global-supply-chain`

### **Option 3: Manual Sync**
Untuk testing atau update manual:
```bash
php artisan countries:sync --weather --disasters --risk
```

---

## 📊 Monitoring

### Check Logs:
```bash
tail -f storage/logs/weather-sync.log
tail -f storage/logs/disaster-sync.log  
tail -f storage/logs/risk-sync.log
```

### Verify Schedule:
```bash
php artisan schedule:list
```

---

## ⚠️ Important Notes

**Weather Score = 0 adalah NORMAL** jika:
- Storm risk = 0% (cuaca cerah)
- Tidak ada badai atau cuaca ekstrem

**Disaster Score = 0 adalah NORMAL** jika:
- Tidak ada gempa bumi dalam periode terakhir
- Negara memang jarang mengalami gempa

Score 0 **BUKAN error**, tapi menandakan kondisi **AMAN**.

---

## 📈 Expected Behavior

Setelah scheduler berjalan:
- ✅ Weather data ter-update setiap 2 jam
- ✅ Disaster data ter-update setiap 2 jam
- ✅ Risk scores recalculated setiap 2 jam
- ✅ Countries dengan weather/disaster events akan punya score > 0
- ✅ Countries tanpa events tetap score = 0 (aman)

---

## 🔍 Verification

Check current stats:
```bash
php artisan tinker
>>> App\Models\RiskScore::where('weather_score', '>', 0)->count();
>>> App\Models\RiskScore::where('disaster_score', '>', 0)->count();
```

View top risk countries:
```bash
php artisan tinker
>>> App\Models\RiskScore::orderByDesc('total_score')->limit(10)->get(['country_id', 'total_score', 'weather_score', 'disaster_score']);
```
