# Coupon Seeder Guide

## Overview
The `CouponSeeder` creates 10 test coupons covering all coupon types with both percentage and fixed discount types.

## Running the Seeder

### Option 1: Run Only Coupon Seeder
```bash
php artisan db:seed --class=CouponSeeder
```

### Option 2: Add to DatabaseSeeder
Add to `database/seeders/DatabaseSeeder.php`:
```php
public function run()
{
    $this->call([
        // ... other seeders
        CouponSeeder::class,
    ]);
}
```

Then run:
```bash
php artisan db:seed
```

## Created Coupons

### 1. General Coupons (2 coupons)

#### SAVE20 (Percentage)
- **Type:** Percentage
- **Value:** 20%
- **Client Type:** Normal
- **Min Order:** 100 SAR
- **Max Order:** 5000 SAR
- **Limit:** 100 uses
- **Per User:** 5 uses
- **Valid:** 3 months

#### FLAT50 (Fixed)
- **Type:** Fixed
- **Value:** 50 SAR
- **Client Type:** Normal
- **Min Order:** 200 SAR
- **Max Order:** 5000 SAR
- **Limit:** 150 uses
- **Per User:** 3 uses
- **Valid:** 3 months

### 2. Vendor-Specific Coupons (2 coupons)

#### VENDOR15 (Percentage)
- **Type:** Percentage
- **Value:** 15%
- **Client Type:** Normal
- **Applies To:** First vendor in database
- **Min Order:** 150 SAR
- **Max Order:** 3000 SAR
- **Limit:** 80 uses
- **Per User:** 4 uses
- **Valid:** 2 months
- **Note:** All cart items must be from this vendor

#### VENDOR30 (Fixed)
- **Type:** Fixed
- **Value:** 30 SAR
- **Client Type:** Normal
- **Applies To:** First vendor in database
- **Min Order:** 250 SAR
- **Max Order:** 3000 SAR
- **Limit:** 60 uses
- **Per User:** 2 uses
- **Valid:** 2 months

### 3. Category-Specific Coupons (2 coupons)

#### CATEGORY25 (Percentage)
- **Type:** Percentage
- **Value:** 25%
- **Client Type:** Normal
- **Applies To:** First category in database
- **Min Order:** 100 SAR
- **Max Order:** 2000 SAR
- **Limit:** 50 uses
- **Per User:** 3 uses
- **Valid:** 1 month
- **Note:** All cart items must be from this category

#### CATEGORY40 (Fixed)
- **Type:** Fixed
- **Value:** 40 SAR
- **Client Type:** Normal
- **Applies To:** First category in database
- **Min Order:** 300 SAR
- **Max Order:** 2000 SAR
- **Limit:** 40 uses
- **Per User:** 2 uses
- **Valid:** 1 month

### 4. Brand-Specific Coupons (2 coupons)

#### BRAND30 (Percentage)
- **Type:** Percentage
- **Value:** 30%
- **Client Type:** Premium (subscription required)
- **Applies To:** First brand in database
- **Min Order:** 200 SAR
- **Max Order:** 4000 SAR
- **Limit:** 70 uses
- **Per User:** 5 uses
- **Valid:** 2 months
- **Note:** All cart items must be from this brand

#### BRAND60 (Fixed)
- **Type:** Fixed
- **Value:** 60 SAR
- **Client Type:** Normal
- **Applies To:** First brand in database
- **Min Order:** 350 SAR
- **Max Order:** 4000 SAR
- **Limit:** 50 uses
- **Per User:** 3 uses
- **Valid:** 2 months

### 5. First Order Coupons (2 coupons)

#### WELCOME10 (Percentage)
- **Type:** Percentage
- **Value:** 10%
- **Client Type:** Normal
- **For:** First-time customers only
- **Min Order:** 50 SAR
- **Max Order:** 10000 SAR
- **Limit:** 1000 uses
- **Per User:** 1 use (one-time only)
- **Valid:** 1 year

#### FIRSTORDER25 (Fixed)
- **Type:** Fixed
- **Value:** 25 SAR
- **Client Type:** Normal
- **For:** First-time customers only
- **Min Order:** 150 SAR
- **Max Order:** 10000 SAR
- **Limit:** 500 uses
- **Per User:** 1 use (one-time only)
- **Valid:** 1 year

## Summary Table

| Code | Type | Value | Apply On | Client Type | Min Order | Limit | Per User | Valid |
|------|------|-------|----------|-------------|-----------|-------|----------|-------|
| SAVE20 | % | 20% | General | Normal | 100 | 100 | 5 | 3 months |
| FLAT50 | Fixed | 50 SAR | General | Normal | 200 | 150 | 3 | 3 months |
| VENDOR15 | % | 15% | Vendor | Normal | 150 | 80 | 4 | 2 months |
| VENDOR30 | Fixed | 30 SAR | Vendor | Normal | 250 | 60 | 2 | 2 months |
| CATEGORY25 | % | 25% | Category | Normal | 100 | 50 | 3 | 1 month |
| CATEGORY40 | Fixed | 40 SAR | Category | Normal | 300 | 40 | 2 | 1 month |
| BRAND30 | % | 30% | Brand | Premium | 200 | 70 | 5 | 2 months |
| BRAND60 | Fixed | 60 SAR | Brand | Normal | 350 | 50 | 3 | 2 months |
| WELCOME10 | % | 10% | First Order | Normal | 50 | 1000 | 1 | 1 year |
| FIRSTORDER25 | Fixed | 25 SAR | First Order | Normal | 150 | 500 | 1 | 1 year |

## Prerequisites

Before running the seeder, ensure you have:
- ✅ At least one vendor user in the database
- ✅ At least one category in the database
- ✅ At least one brand in the database

If any of these are missing, the seeder will skip creating those specific coupons and show a warning.

## Testing the Coupons

### Test General Coupon
```bash
# Add any products to cart
POST /api/app/cart/calculate
{
  "coupon_code": "SAVE20"
}
# Should apply 20% discount
```

### Test Vendor Coupon
```bash
# Add products ONLY from the specific vendor
POST /api/app/cart/calculate
{
  "coupon_code": "VENDOR15"
}
# Should apply 15% discount if all items are from that vendor
# Should fail if cart has items from multiple vendors
```

### Test Category Coupon
```bash
# Add products ONLY from the specific category
POST /api/app/cart/calculate
{
  "coupon_code": "CATEGORY25"
}
# Should apply 25% discount if all items are from that category
# Should fail if cart has items from multiple categories
```

### Test Brand Coupon
```bash
# Add products ONLY from the specific brand
POST /api/app/cart/calculate
{
  "coupon_code": "BRAND30"
}
# Should apply 30% discount if user has premium subscription
# Should fail if user is not premium or cart has items from multiple brands
```

### Test First Order Coupon
```bash
# As a new user who hasn't placed any orders
POST /api/app/cart/calculate
{
  "coupon_code": "WELCOME10"
}
# Should apply 10% discount for first-time users
# Should fail if user has already placed an order
```

## Translations

All coupons include translations for:
- **English (en)**: Full name and description
- **Arabic (ar)**: Full name and description

## Customization

To customize the seeder:

### Change Coupon Values
Edit the values in each method:
```php
'value' => 20,  // Change discount amount
'min_order_total' => 100,  // Change minimum order
'limit' => 100,  // Change total usage limit
```

### Change Validity Period
```php
'end_at' => now()->addMonths(3),  // Change duration
```

### Add More Coupons
Create additional coupons by copying the pattern:
```php
$coupon = Coupon::create([
    'code' => 'NEWCODE',
    'type' => CouponType::PERCENTAGE,
    'value' => 15,
    // ... other fields
]);

$this->createTranslations($coupon, [
    'en' => ['name' => 'Name', 'description' => 'Description'],
    'ar' => ['name' => 'الاسم', 'description' => 'الوصف'],
]);
```

## Troubleshooting

### Error: "Vendor not found"
**Solution:** Create at least one vendor user before running the seeder.

### Error: "Category not found"
**Solution:** Create at least one category before running the seeder.

### Error: "Brand not found"
**Solution:** Create at least one brand before running the seeder.

### Coupons Not Showing in App
**Check:**
1. Coupons are active (`is_active = true`)
2. Current date is within `start_at` and `end_at`
3. User's client type matches coupon's `client_type`
4. Usage limits not exceeded

## Notes

1. **Polymorphic Relationships:** The seeder uses the new polymorphic structure with `applicable_id` and `applicable_type`.

2. **Realistic Data:** All coupons have realistic values, limits, and validity periods suitable for testing.

3. **Bilingual:** All coupons include both English and Arabic translations.

4. **Safe to Re-run:** You can run the seeder multiple times, but it will create duplicate coupons with the same codes (which will fail unique constraint). Clear coupons table first if re-running.

5. **Production Use:** These are test coupons. For production, create coupons through the admin panel with appropriate values.
