# Customer Risk Level Switcher

## 📋 Deskripsi

Script untuk switch customer risk level antara **L1** dan **L2** dengan aman dan mudah.

## 🎯 Risk Levels

Sistem memiliki beberapa risk level:
- **PB** - Pre-Bad
- **L1** - Level 1 (Low Risk)
- **L2** - Level 2 (Medium Risk)
- **CCM** - Credit Collection Monitoring
- **MB** - Medium Bad
- **ML** - Medium Loss

Script ini fokus pada switch antara **L1** ↔ **L2**.

## 🔧 Cara Penggunaan

### Option 1: Menggunakan Bash Script (Recommended)

```bash
# Switch semua customer dari L1 ke L2
./scripts/switch-risk-level.sh --from=L1 --to=L2

# Switch semua customer dari L2 ke L1
./scripts/switch-risk-level.sh --from=L2 --to=L1

# Preview dulu (dry run)
./scripts/switch-risk-level.sh --from=L1 --to=L2 --dry-run

# Switch customer tertentu saja
./scripts/switch-risk-level.sh --from=L1 --to=L2 --customer-id=abc-123-def

# Switch untuk resort tertentu
./scripts/switch-risk-level.sh --from=L2 --to=L1 --resort-id=RESORT001
```

### Option 2: Menggunakan Artisan Command Langsung

```bash
# Switch semua customer dari L1 ke L2
php artisan customer:switch-risk-level --from=L1 --to=L2

# Preview dulu (dry run)
php artisan customer:switch-risk-level --from=L1 --to=L2 --dry-run

# Switch customer tertentu
php artisan customer:switch-risk-level --from=L1 --to=L2 --customer-id=abc-123-def

# Switch untuk resort tertentu
php artisan customer:switch-risk-level --from=L2 --to=L1 --resort-id=RESORT001
```

## 📝 Options

| Option | Required | Description |
|--------|----------|-------------|
| `--from=L1\|L2` | ✅ Yes | Source risk level |
| `--to=L1\|L2` | ✅ Yes | Target risk level |
| `--customer-id=ID` | ❌ No | Switch customer spesifik saja |
| `--resort-id=ID` | ❌ No | Filter berdasarkan resort |
| `--dry-run` | ❌ No | Preview tanpa apply changes |
| `--help` | ❌ No | Tampilkan help |

## 📚 Examples

### 1. Switch Semua L1 ke L2

```bash
# Dry run dulu untuk preview
./scripts/switch-risk-level.sh --from=L1 --to=L2 --dry-run

# Kalau sudah oke, jalankan tanpa --dry-run
./scripts/switch-risk-level.sh --from=L1 --to=L2
```

**Output:**
```
======================================
  Customer Risk Level Switcher
======================================

Configuration:
  From: L1
  To:   L2

Running command...

Switching customers from risk level L1 to L2
Found 15 customer(s) to update

+--------------------------------------+------------------+-----------+--------------+-----------+
| ID                                   | Name             | Resort ID | Current Risk | New Risk  |
+--------------------------------------+------------------+-----------+--------------+-----------+
| cust-001                             | John Doe         | RESORT001 | L1           | L2        |
| cust-002                             | Jane Smith       | RESORT002 | L1           | L2        |
...

Do you want to proceed with the update? (yes/no) [no]:
> yes

✅ Successfully updated 15 customer(s) from L1 to L2
```

### 2. Switch Customer Tertentu

```bash
# Switch 1 customer dari L1 ke L2
./scripts/switch-risk-level.sh --from=L1 --to=L2 --customer-id=cust-001
```

### 3. Switch per Resort

```bash
# Switch semua L2 ke L1 di resort tertentu
./scripts/switch-risk-level.sh --from=L2 --to=L1 --resort-id=RESORT001
```

### 4. Preview Mode (Safe)

```bash
# Selalu dry-run dulu untuk safety
./scripts/switch-risk-level.sh --from=L1 --to=L2 --dry-run
```

## 🔒 Safety Features

### 1. Confirmation Prompt
Script akan meminta konfirmasi sebelum melakukan perubahan.

### 2. Dry Run Mode
Gunakan `--dry-run` untuk preview tanpa apply changes.

### 3. Transaction Safety
Menggunakan database transaction untuk rollback jika ada error.

### 4. Logging
Semua perubahan dicatat di log:
```
[2026-02-18 15:00:00] local.info: Switched 15 customers from L1 to L2. IDs: cust-001, cust-002, ...
```

### 5. Validation
- ✅ Only L1 or L2 allowed
- ✅ From and To must be different
- ✅ Shows preview table before update

## 📊 Check Current Risk Levels

### View customers by risk level:
```bash
# Using tinker
php artisan tinker

>>> Customer::where('risk_level', 'L1')->count();
>>> Customer::where('risk_level', 'L2')->count();

>>> Customer::where('risk_level', 'L1')->get(['id', 'name', 'resort_id']);
```

### SQL Query:
```sql
-- Count by risk level
SELECT risk_level, COUNT(*) as count
FROM customers
WHERE risk_level IN ('L1', 'L2')
GROUP BY risk_level;

-- List all L1 customers
SELECT id, name, resort_id, risk_level
FROM customers
WHERE risk_level = 'L1'
ORDER BY resort_id, name;
```

## 🎯 Use Cases

### Case 1: Periodic Risk Review
```bash
# Setiap bulan, review dan adjust risk levels
./scripts/switch-risk-level.sh --from=L1 --to=L2 --resort-id=RESORT001
```

### Case 2: Customer Request
```bash
# Customer improve payment behavior, downgrade risk
./scripts/switch-risk-level.sh --from=L2 --to=L1 --customer-id=cust-001
```

### Case 3: Portfolio Rebalancing
```bash
# Rebalance entire portfolio
./scripts/switch-risk-level.sh --from=L1 --to=L2 --dry-run
./scripts/switch-risk-level.sh --from=L1 --to=L2
```

## 📁 Files Created

1. ✅ `app/Console/Commands/SwitchCustomerRiskLevel.php` - Console command
2. ✅ `scripts/switch-risk-level.sh` - Bash wrapper script
3. ✅ `docs/switch-risk-level.md` - This documentation

## 🚨 Troubleshooting

### Error: "No customers found"
```bash
# Check available risk levels first
php artisan tinker
>>> Customer::select('risk_level', DB::raw('count(*) as count'))->groupBy('risk_level')->get();
```

### Error: "Invalid risk level"
```bash
# Make sure to use uppercase L1 or L2
./scripts/switch-risk-level.sh --from=L1 --to=L2  # ✅ Correct
./scripts/switch-risk-level.sh --from=l1 --to=l2  # ❌ Wrong
```

### Want to rollback changes?
```bash
# Just switch back!
./scripts/switch-risk-level.sh --from=L2 --to=L1 --resort-id=RESORT001
```

## 📝 Best Practices

1. ✅ **Always dry-run first** - Preview sebelum apply
2. ✅ **Test on single customer** - Try 1 customer dulu
3. ✅ **Backup database** - Before bulk updates
4. ✅ **Check logs** - After update
5. ✅ **Verify changes** - Confirm di database

## 🔗 Related Commands

```bash
# Update risk levels automatically based on disbursement date
php artisan app:update-risk-levels

# Check scheduled tasks
php artisan schedule:list
```
