# Auto-Quotation Condition Evaluation Guide

## 📋 Simple Overview

The condition checker evaluates rules to decide if a lead should get an auto-quotation.

---

## 🔍 How 2 Conditions Work

### Example Setup:
```
Condition 1: product = "Oil"
Connector:   AND
Condition 2: quantity > 100
```

### Evaluation Flow:

```
┌─────────────────────────────────────────────────────────┐
│ STEP 1: Check Condition #1                             │
├─────────────────────────────────────────────────────────┤
│ Field:     product                                      │
│ Operator:  =                                            │
│ Expected:  "Oil"                                        │
│ Actual:    "Oil"  (fetched from lead)                   │
│ Result:    ✅ TRUE (they match!)                        │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│ STEP 2: Check Condition #2                             │
├─────────────────────────────────────────────────────────┤
│ Field:     quantity                                     │
│ Operator:  >                                            │
│ Expected:  100                                          │
│ Actual:    150  (fetched from lead)                     │
│ Result:    ✅ TRUE (150 > 100)                          │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│ STEP 3: Combine Results with AND                       │
├─────────────────────────────────────────────────────────┤
│ Condition 1:  TRUE                                      │
│ Connector:    AND                                       │
│ Condition 2:  TRUE                                      │
│                                                         │
│ Calculation:  TRUE AND TRUE = TRUE                     │
│                                                         │
│ Final Result: ✅ MATCH! Create quotation.               │
└─────────────────────────────────────────────────────────┘
```

---

## 🔗 Understanding AND vs OR

### AND Logic (All must be TRUE)
```
TRUE  AND TRUE  = ✅ TRUE
TRUE  AND FALSE = ❌ FALSE
FALSE AND FALSE = ❌ FALSE
```
**Rule: ALL conditions must pass**

### OR Logic (At least one must be TRUE)
```
TRUE  OR TRUE  = ✅ TRUE
TRUE  OR FALSE = ✅ TRUE
FALSE OR FALSE = ❌ FALSE
```
**Rule: AT LEAST ONE condition must pass**

---

## 📍 Where the Logic Lives

**All condition checking is in ONE place:**
```
app/Services/AutoQuotation/Helpers/ConditionEvaluator.php
└── evaluate() method  ← All logic is here!
```

### What It Does (3 Steps):

1. **Loop through each condition**
   - Get actual value from lead
   - Compare with expected value
   - Store TRUE or FALSE

2. **Combine results using connectors**
   - Apply AND/OR logic between conditions
   - Build final TRUE/FALSE result

3. **Return final result**
   - TRUE = Create quotation
   - FALSE = Skip this lead

---

## 🐛 Debugging

### Enable Detailed Logs:
```bash
php artisan auto-quote:process --limit=1
```

### Check Logs:
```bash
tail -f storage/logs/laravel.log
```

### What You'll See:
```
🔍 Starting condition evaluation (lead_id: 123)
📋 Condition #0 evaluated: field=product, operator==, expected=Oil, actual=Oil → ✅ MATCH
📋 Condition #1 evaluated: field=quantity, operator=>, expected=100, actual=150 → ✅ MATCH
🔗 Combining: Step 1: TRUE AND TRUE = TRUE
🎯 Final Result: ✅ ALL CONDITIONS PASSED
```

---

## 📝 Example Scenarios

### Scenario 1: Both Conditions Pass (AND)
```
Condition 1: product = "Oil"          → ✅ TRUE (matches)
AND
Condition 2: quantity > 100           → ✅ TRUE (150 > 100)

Result: TRUE AND TRUE = ✅ CREATE QUOTATION
```

### Scenario 2: One Condition Fails (AND)
```
Condition 1: product = "Oil"          → ✅ TRUE (matches)
AND
Condition 2: quantity > 200           → ❌ FALSE (150 < 200)

Result: TRUE AND FALSE = ❌ SKIP LEAD
```

### Scenario 3: One Condition Passes (OR)
```
Condition 1: product = "Gas"          → ❌ FALSE (doesn't match)
OR
Condition 2: quantity > 100           → ✅ TRUE (150 > 100)

Result: FALSE OR TRUE = ✅ CREATE QUOTATION
```

---

## 🎯 Quick Reference

| Operator | Example | Description |
|----------|---------|-------------|
| `=` | product = "Oil" | Exact match |
| `!=` | product != "Gas" | Not equal |
| `>` | quantity > 100 | Greater than |
| `>=` | quantity >= 100 | Greater or equal |
| `<` | quantity < 500 | Less than |
| `<=` | quantity <= 500 | Less or equal |
| `like` | product like "Oil" | Contains text |
| `in` | product in ["Oil","Gas"] | In array |

---

## 💡 Tips

1. **Keep conditions simple** - Test with 1-2 conditions first
2. **Use logs** - Always check logs when debugging
3. **Test both AND and OR** - Make sure you understand the difference
4. **One place** - All logic is in `evaluate()` method

---

## 📞 Common Issues

### Issue: Conditions not matching
**Solution:** Check the logs to see actual vs expected values

### Issue: AND/OR not working as expected
**Solution:** Remember - AND requires ALL true, OR requires AT LEAST ONE true

### Issue: Can't debug
**Solution:** Check `storage/logs/laravel.log` for detailed evaluation steps
