# 📤 Manual Delivery System - HaikzBot

## ✨ Fitur Baru: Manual Delivery

Sekarang kamu bisa **kirim produk manual** ke user tanpa perlu auto-delivery! Cocok untuk:
- Order via ticket (pembayaran manual)
- Giveaway/hadiah
- Kompensasi/refund
- Testing

---

## 🎯 Cara Pakai

### 1. Via Command Discord `/send`

```
/send user:@buyer product_id:1 quantity:1
```

**Parameter:**
- `user` - mention user atau user object
- `product_id` - ID produk (lihat di `/product list` atau dashboard)
- `quantity` - jumlah (1-10, default: 1)

**Contoh:**
```
/send user:@JohnDoe product_id:1 quantity:2
```

Bot akan:
1. ✅ Cek stock tersedia
2. ✅ Ambil stock dari database
3. ✅ Kirim DM ke user dengan isi produk
4. ✅ Mark stock sebagai sold
5. ✅ Log ke audit log

**Kalau gagal:**
- ❌ Stock tidak cukup → tidak dikurangi
- ❌ DM gagal (user block bot) → stock di-rollback otomatis

---

### 2. Via Dashboard

1. Buka dashboard → **📤 Manual Delivery**
2. Isi form:
   - **User ID** - Discord User ID (18 digit)
   - **Product** - pilih dari dropdown
   - **Quantity** - jumlah (1-10)
   - **Note** - catatan internal (opsional)
3. Klik **"📤 Kirim ke User"**
4. Konfirmasi
5. Selesai! ✅

**Cara dapat User ID:**
- Klik kanan user di Discord → Copy User ID
- Atau dari ticket channel (user ID ada di channel name)

---

## 📦 Enhanced Stock Management

### Fitur Baru di Dashboard Stock:

#### 1. **Table View dengan Details**
- ID stock
- Content preview (40 karakter pertama)
- Status (Available/Sold)
- Sold To (user ID)
- Actions (View/Delete)

#### 2. **View Full Content**
- Klik tombol **"👁️ View"** pada stock item
- Modal muncul dengan full content
- Bisa copy ke clipboard
- Show info sold (kalau sudah sold)

#### 3. **Filter & Stats**
- Total items
- Available count (badge hijau)
- Sold count (badge merah)

---

## 🔍 Cara Lihat Stock Details

### Via Dashboard:

1. Buka dashboard → **📦 Stock**
2. Pilih produk dari dropdown
3. List stock muncul dalam table
4. Klik **"👁️ View"** pada item yang mau dilihat
5. Modal muncul dengan:
   - Full content (email:pass, license key, dll)
   - Status (Available/Sold)
   - Sold to (kalau sudah sold)
   - Sold date
6. Klik **"📋 Copy"** untuk copy content
7. Close modal

**Keamanan:**
- Content di-hide di table (cuma preview 40 char)
- Full content cuma muncul di modal
- Perlu dashboard token untuk akses

---

## 📊 Manual Delivery Log

Dashboard menampilkan **Recent Manual Deliveries** (50 terakhir):
- Date & time
- Target user (user ID)
- Product name & ID
- Quantity
- Note (kalau ada)

**Berguna untuk:**
- Audit trail
- Cek siapa yang sudah dikasih
- Tracking giveaway/kompensasi

---

## 🎮 Use Cases

### Use Case 1: Order via Ticket (Manual Payment)
```
Scenario:
1. User buka ticket → mau beli Discord Nitro
2. Staff kasih nomor rekening
3. User transfer → kirim bukti
4. Staff verifikasi pembayaran
5. Staff ketik: /send user:@user product_id:1 quantity:1
6. User dapat DM dengan produk
```

### Use Case 2: Giveaway
```
Scenario:
1. Giveaway di channel #giveaway
2. Winner: @JohnDoe
3. Dashboard → Manual Delivery
4. User ID: 123456789012345678
5. Product: Discord Nitro (1 Bulan)
6. Quantity: 1
7. Note: "Giveaway winner - May 2026"
8. Send!
```

### Use Case 3: Kompensasi/Refund
```
Scenario:
1. User komplain produk rusak
2. Staff verifikasi → valid
3. Staff kirim replacement via /send
4. Note: "Replacement for order INV-123"
```

### Use Case 4: Testing
```
Scenario:
1. Owner mau test delivery system
2. /send user:@myself product_id:1 quantity:1
3. Cek DM → produk masuk
4. System works!
```

---

## 🔐 Permission & Security

### Siapa yang bisa pakai `/send`?
- ✅ Owner (OWNER_ID di .env)
- ✅ Administrator (permission Administrator)
- ❌ Staff biasa (tidak bisa)
- ❌ Member biasa (tidak bisa)

### Siapa yang bisa pakai Dashboard Manual Delivery?
- ✅ Siapa pun dengan dashboard token
- Token = full access, jadi **JANGAN SHARE!**

### Audit Log
Semua manual delivery tercatat di:
- Database: `audit_log` table
- Action: `manual_delivery`
- Data: user ID, product, quantity, stock IDs, note

Query audit log:
```sql
SELECT * FROM audit_log 
WHERE action = 'manual_delivery' 
ORDER BY created_at DESC;
```

---

## 🐛 Troubleshooting

### `/send` command tidak muncul
**Solusi:**
```bash
npm run deploy
# Tunggu "✅ Registered to guild"
# Restart Discord (Ctrl+R)
```

### Error: "Stock tidak cukup"
**Solusi:**
- Cek stock: `/stock view product_id:X`
- Atau dashboard → Stock → pilih produk
- Tambah stock kalau habis

### Error: "Gagal kirim DM"
**Kemungkinan:**
- User block bot
- User DM closed (Privacy Settings)
- User left server

**Solusi:**
- Minta user buka DM
- Atau kirim manual via ticket channel

### Stock tidak berkurang setelah `/send`
**Cek:**
- Apakah DM berhasil terkirim?
- Kalau DM gagal, stock auto-rollback (tidak dikurangi)
- Cek log: `tail -f /tmp/discord-bot.log | grep send`

### Dashboard Manual Delivery: "Failed to send DM"
**Solusi sama dengan di atas**

---

## 📝 Command Reference

### `/send`
```
/send user:@buyer product_id:1 quantity:1
```

**Parameters:**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| user | User | ✅ Yes | Target user (mention atau user object) |
| product_id | Integer | ✅ Yes | Product ID (dari `/product list`) |
| quantity | Integer | ❌ No | Jumlah (1-10, default: 1) |

**Response:**
- ✅ Success: Embed dengan info delivery
- ❌ Error: Message dengan alasan error

---

## 🎯 Tips & Best Practices

### 1. Selalu Cek Stock Dulu
Sebelum `/send`, cek stock tersedia:
```
/stock view product_id:1
```

### 2. Pakai Note di Dashboard
Kalau kirim via dashboard, isi note untuk tracking:
```
Note: "Giveaway winner - May 2026"
Note: "Replacement for order INV-123"
Note: "Testing delivery system"
```

### 3. Backup Stock Sebelum Manual Delivery Massal
Kalau mau kirim ke banyak user sekaligus:
```bash
sqlite3 data/bot.db ".backup data/backup-before-giveaway.db"
```

### 4. Verifikasi User ID
Pastikan User ID benar (18 digit):
```
Valid: 837371531708334103
Invalid: 83737 (terlalu pendek)
Invalid: abc123 (bukan angka)
```

### 5. Limit Quantity
Max 10 per transaksi untuk prevent abuse. Kalau perlu lebih, kirim multiple kali.

---

## 📊 Statistics

Cek berapa kali manual delivery dipakai:
```sql
-- Total manual deliveries
SELECT COUNT(*) FROM audit_log WHERE action = 'manual_delivery';

-- Per product
SELECT 
  JSON_EXTRACT(data, '$.product_name') AS product,
  COUNT(*) AS deliveries,
  SUM(JSON_EXTRACT(data, '$.quantity')) AS total_qty
FROM audit_log 
WHERE action = 'manual_delivery'
GROUP BY product;

-- Per user (siapa yang paling sering kirim)
SELECT user_id, COUNT(*) AS deliveries
FROM audit_log 
WHERE action = 'manual_delivery'
GROUP BY user_id
ORDER BY deliveries DESC;
```

---

## 🚀 What's Next?

Fitur yang bisa ditambah nanti:
- [ ] Bulk delivery (kirim ke multiple users sekaligus)
- [ ] Schedule delivery (kirim di waktu tertentu)
- [ ] Custom message (custom DM message per delivery)
- [ ] Delivery confirmation (user harus confirm terima)
- [ ] Export delivery log (CSV/Excel)

---

**Dashboard URL:** http://localhost:4002  
**Token:** `622e5783c187f3e3da7ad0da3fc34f9b9b47894925476d7fdf29d75b152a880b`

Selamat menggunakan Manual Delivery System! 🎉
