# 📝 How to Add Past Draws and Winners

Step-by-step guide to backfill historical draw data for imported groups.

---

## 🎯 Use Case

You imported a group that started 6 months ago. Now you need to record:
- **Month 1 Draw**: John Doe won ₹99,000
- **Month 2 Draw**: Jane Smith won ₹98,500
- **Month 3 Draw**: Bob Johnson won ₹98,000
- ... and so on

---

## 🚀 Quick Steps

### Method 1: Using the UI (Coming Soon)

**Step 1**: Go to **Group Details Page**

**Step 2**: Click **"Actions" menu** (top right)

**Step 3**: Select **"Add Past Draw Result"**

**Step 4**: Fill in the form:
- Month number (e.g., 1, 2, 3)
- Select winner from member list
- Enter prize amount

**Step 5**: Click **"Add Draw"**

**Repeat** for each past month!

---

### Method 2: Using the API Directly (For Now)

Since the UI buttons aren't wired yet, you can use the API directly:

#### 1. Get Your Auth Token

Login and copy your token from browser DevTools:
```
F12 → Application → Local Storage → auth_token
```

#### 2. Add Past Draw via API

```bash
curl -X POST https://chitfund.deepteklabs.com/api/monthly-draws \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN_HERE" \
  -d '{
    "group_id": "YOUR_GROUP_ID",
    "month": 6,
    "year": 2024,
    "winner_id": "MEMBER_USER_ID",
    "prize_amount": 98000,
    "is_past_draw": true,
    "client_seed": "PAST_DRAW_JUNE_2024"
  }'
```

#### 3. Repeat for Each Month

Change the parameters:
- `month`: 6, 7, 8, etc.
- `year`: 2024
- `winner_id`: Different member for each draw
- `prize_amount`: Amount they won

---

## 🔍 How to Get IDs

### Get Group ID:
1. Go to Group Details page
2. Check URL or browser console
3. Or list all groups via API:
```bash
curl https://chitfund.deepteklabs.com/api/chit-groups/manager \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### Get Member IDs:
1. View group members in UI
2. Or list members via API:
```bash
curl https://chitfund.deepteklabs.com/api/members/YOUR_GROUP_ID/members \
  -H "Authorization: Bearer YOUR_TOKEN"
```

---

## 📋 Example: Complete Backfill

### Scenario:
- Group started: June 2024
- Current: December 2024 (Month 7)
- Need to add: 6 past draws

### API Calls:

```bash
# Set your token
TOKEN="your_auth_token_here"
GROUP_ID="your_group_id_here"

# Month 1 - June 2024
curl -X POST https://chitfund.deepteklabs.com/api/monthly-draws \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "group_id": "'$GROUP_ID'",
    "month": 6,
    "year": 2024,
    "winner_id": "john_doe_user_id",
    "prize_amount": 99000,
    "is_past_draw": true
  }'

# Month 2 - July 2024
curl -X POST https://chitfund.deepteklabs.com/api/monthly-draws \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "group_id": "'$GROUP_ID'",
    "month": 7,
    "year": 2024,
    "winner_id": "jane_smith_user_id",
    "prize_amount": 98500,
    "is_past_draw": true
  }'

# Month 3 - August 2024
curl -X POST https://chitfund.deepteklabs.com/api/monthly-draws \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "group_id": "'$GROUP_ID'",
    "month": 8,
    "year": 2024,
    "winner_id": "bob_johnson_user_id",
    "prize_amount": 98000,
    "is_past_draw": true
  }'

# Continue for months 4, 5, 6...
```

---

## 🛠️ Add UI Buttons (For You to Implement)

I created the dialogs but need to add buttons to access them. Here's where:

### Location: Group Details Page Actions Menu

Add these options to the PopupMenuButton in `group_details_page.dart`:

```dart
PopupMenuItem(
  value: 'add_past_draw',
  child: Row(
    children: [
      Icon(Icons.history, color: Colors.blue.shade600),
      SizedBox(width: 12.w),
      const Text('Add Past Draw Result'),
    ],
  ),
),
PopupMenuItem(
  value: 'add_past_payments',
  child: Row(
    children: [
      Icon(Icons.payment_outlined, color: Colors.green.shade600),
      SizedBox(width: 12.w),
      const Text('Add Past Payments'),
    ],
  ),
),
```

Then handle the selection:

```dart
onSelected: (value) {
  if (value == 'add_past_draw') {
    _showAddPastDrawDialog();
  } else if (value == 'add_past_payments') {
    _showAddPastPaymentsDialog();
  }
}

void _showAddPastDrawDialog() {
  // Need to determine which month to add
  final currentMonth = _determineCurrentMonth();
  
  showDialog(
    context: context,
    builder: (context) => AddPastDrawDialog(
      group: widget.group,
      monthNumber: currentMonth,
    ),
  ).then((result) {
    if (result == true) {
      _chitGroupService.loadGroupMonthlyDraws(widget.group.id);
    }
  });
}

void _showAddPastPaymentsDialog() {
  final currentMonth = _determineCurrentMonth();
  
  showDialog(
    context: context,
    builder: (context) => AddPastPaymentsDialog(
      group: widget.group,
      monthNumber: currentMonth,
    ),
  ).then((result) {
    if (result == true) {
      _paymentService.loadGroupPayments(widget.group.id);
    }
  });
}
```

---

## 📊 Recommended Workflow

### Option A: Use API (Current - Works Now)
1. Get group ID and member IDs
2. Use curl commands above
3. Add all past draws via API
4. Verify in UI

### Option B: Add UI Buttons (Better UX)
1. Add buttons to group details page
2. Import dialogs I created
3. Click through UI to add draws
4. More user-friendly

---

## 🎓 Quick Reference

### API Endpoint:
```
POST /api/monthly-draws
```

### Required Fields:
- `group_id` - The imported group's ID
- `month` - Month number (1-12)
- `year` - Year (e.g., 2024)
- `winner_id` - User ID of the winner
- `prize_amount` - Amount won
- `is_past_draw: true` - Flag for past draws

### Optional:
- `client_seed` - Any string (e.g., "PAST_JUNE_2024")

---

## ⚠️ Important Notes

1. **Add members FIRST** before adding draws
2. **Winner must be a member** of the group
3. **Can't duplicate** - only one draw per month/year
4. **Month/year** must match group timeline
5. **Prize amounts** should be realistic

---

## 🔗 Files You Need

The dialogs are already created:
- ✅ `AddPastDrawDialog` - Ready to use
- ✅ `AddPastPaymentsDialog` - Ready to use
- ⚠️ Just need to add buttons in `group_details_page.dart`

---

## 💡 Quick Solution

**For now**, use the API method (curl commands) to add past draws.

**For better UX**, I can add the buttons to the group details page - just let me know!

---

**See the API examples above to add past draws immediately!** 🚀

Or say "add the UI buttons" and I'll wire them up for you.

