# Vendor Profile Update - Complete Examples

## Example 1: Update Delivery Settings Only

```json
{
  "delivery_type": "merchant",
  "delivery_cities": [
    {
      "country_id": 1,
      "city_id": 5,
      "delivery_price": 25.50
    },
    {
      "country_id": 1,
      "city_id": 10,
      "delivery_price": 30.00
    }
  ]
}
```

**What happens:**
- ✅ `delivery_type` is updated to "merchant" in the merchants table
- ✅ All existing delivery cities are deleted
- ✅ New delivery cities are created with the provided prices
- ❌ Logo, translations, location, categories remain unchanged

---

## Example 2: Update Basic Info + Delivery

```json
{
  "logo": "d3082cdc-9e8b-49d5-828b-61163d2abe63",
  "commercial_register_number": "1010897654",
  "commercial_register_file": "142bbd15-6082-41ae-aba3-c9d31424af74",
  "stock_alert_threshold": 5,
  "expiry_alert_before_days": 30,
  "delivery_type": "merchant",
  "delivery_cities": [
    {
      "country_id": 1,
      "city_id": 5,
      "delivery_price": 25.50
    }
  ]
}
```

**What happens:**
- ✅ Logo is updated
- ✅ Commercial register number and file are updated
- ✅ Stock alert settings are updated
- ✅ Delivery type is set to "merchant"
- ✅ Delivery cities are updated
- ❌ Translations, location, categories remain unchanged

---

## Example 3: Change Delivery Type to Platform (No Cities Needed)

```json
{
  "delivery_type": "platform"
}
```

**What happens:**
- ✅ `delivery_type` is updated to "platform"
- ❌ Existing delivery cities remain in database (but won't be used)
- ❌ All other fields remain unchanged

**Note:** When delivery_type is "platform", you don't need to send delivery_cities.

---

## Example 4: Update Everything Including Delivery

```json
{
  "logo": "d3082cdc-9e8b-49d5-828b-61163d2abe63",
  "commercial_register_number": "1010897654",
  "commercial_register_file": "142bbd15-6082-41ae-aba3-c9d31424af74",
  "stock_alert_threshold": 5,
  "expiry_alert_before_days": 30,
  "iban": "SA1234567890123456789012",
  "delivery_type": "merchant",
  "delivery_cities": [
    {
      "country_id": 1,
      "city_id": 5,
      "delivery_price": 25.50
    },
    {
      "country_id": 1,
      "city_id": 10,
      "delivery_price": 30.00
    },
    {
      "country_id": 1,
      "city_id": 15,
      "delivery_price": 35.00
    }
  ],
  "ar": {
    "name": "متجر السعيد",
    "description": "متجر متخصص في بيع المنتجات الإلكترونية والإكسسوارات"
  },
  "en": {
    "name": "Saed Store",
    "description": "A store specialized in selling electronics and accessories"
  },
  "location": {
    "city_id": 1,
    "lat": "24.713552",
    "lng": "46.675296",
    "name": "Main Branch",
    "property_number": "Building 25",
    "details": "King Fahd Road, Al Olaya District",
    "url": "https://maps.google.com/?q=24.713552,46.675296"
  },
  "category_ids": [1, 10]
}
```

**What happens:**
- ✅ All merchant basic info is updated
- ✅ Delivery type and cities are updated
- ✅ Translations are updated
- ✅ Location is updated
- ✅ Categories are updated (if vendor has permission)

---

## Example 5: Update Only Delivery Cities (Keep Same Type)

```json
{
  "delivery_cities": [
    {
      "country_id": 1,
      "city_id": 20,
      "delivery_price": 40.00
    }
  ]
}
```

**What happens:**
- ✅ All existing delivery cities are deleted
- ✅ New delivery city (city_id: 20) is created
- ❌ `delivery_type` remains unchanged (keeps existing value)
- ❌ All other fields remain unchanged

**Important:** When updating delivery_cities, the entire array is replaced, not merged!

---

## Delivery Type Values

| Value | Description | Requires delivery_cities? |
|-------|-------------|---------------------------|
| `merchant` | Vendor handles delivery | Optional (but recommended) |
| `platform` | Platform handles delivery | No |

---

## Important Notes

### 1. Delivery Cities Replacement
When you send `delivery_cities`, **all existing cities are deleted** and replaced with the new ones. If you want to keep existing cities, you must include them in the array.

**Example:**
```json
// Current cities: [1, 2, 3]
// Send this:
{
  "delivery_cities": [
    {"country_id": 1, "city_id": 4, "delivery_price": 25}
  ]
}
// Result: Only city 4 remains, cities 1, 2, 3 are deleted
```

### 2. Delivery Type Without Cities
You can update `delivery_type` without sending `delivery_cities`:
```json
{
  "delivery_type": "platform"
}
```

### 3. Delivery Cities Without Type
You can update `delivery_cities` without sending `delivery_type`:
```json
{
  "delivery_cities": [
    {"country_id": 1, "city_id": 5, "delivery_price": 25.50}
  ]
}
```

### 4. Validation Rules
- `delivery_type`: Must be "merchant" or "platform"
- `delivery_cities`: Must be an array
- `delivery_cities.*.country_id`: Required, must exist in countries table
- `delivery_cities.*.city_id`: Required, must exist in cities table
- `delivery_cities.*.delivery_price`: Required, must be numeric, minimum 0

---

## Response Example with Delivery Data

```json
{
  "status": "success",
  "message": "Vendor profile updated successfully",
  "data": {
    "id": 123,
    "merchant": {
      "id": 45,
      "delivery_type": "merchant",
      "delivery_cities": [
        {
          "id": 1,
          "user_id": 123,
          "country_id": 1,
          "city_id": 5,
          "delivery_price": "25.50",
          "city": {
            "id": 5,
            "name": "Riyadh",
            "name_ar": "الرياض"
          }
        },
        {
          "id": 2,
          "user_id": 123,
          "country_id": 1,
          "city_id": 10,
          "delivery_price": "30.00",
          "city": {
            "id": 10,
            "name": "Jeddah",
            "name_ar": "جدة"
          }
        }
      ]
    }
  }
}
```

---

## Common Scenarios

### Scenario A: Vendor Switches from Platform to Self-Delivery
```json
{
  "delivery_type": "merchant",
  "delivery_cities": [
    {"country_id": 1, "city_id": 5, "delivery_price": 25.50},
    {"country_id": 1, "city_id": 10, "delivery_price": 30.00}
  ]
}
```

### Scenario B: Vendor Switches from Self-Delivery to Platform
```json
{
  "delivery_type": "platform"
}
```
Note: Existing delivery_cities remain in database but won't be used.

### Scenario C: Vendor Updates Delivery Prices
```json
{
  "delivery_cities": [
    {"country_id": 1, "city_id": 5, "delivery_price": 30.00},
    {"country_id": 1, "city_id": 10, "delivery_price": 35.00}
  ]
}
```

### Scenario D: Vendor Adds More Cities
```json
{
  "delivery_cities": [
    {"country_id": 1, "city_id": 5, "delivery_price": 25.50},
    {"country_id": 1, "city_id": 10, "delivery_price": 30.00},
    {"country_id": 1, "city_id": 15, "delivery_price": 35.00},
    {"country_id": 1, "city_id": 20, "delivery_price": 40.00}
  ]
}
```
Remember: Include ALL cities you want to keep!
