# Mobile App: Multiple Additional Images API

The API now supports **multiple additional images** per request. Use the following in your Swift app.

---

## 1. Response model (decode from API)

Each request object now includes:

| Field | Type | Description |
|-------|------|-------------|
| `additional_image_url` | String? | **First** image URL (backward compat; use for single-image UI) |
| `additional_image_urls` | [String] | **All** additional image URLs (use for multi-image support) |

**Swift model example:**

```swift
struct ClientRequest: Codable {
    // ... other fields ...
    let additionalImageUrl: String?      // first image (optional)
    let additionalImageUrls: [String]?    // all images (prefer this for new UI)

    enum CodingKeys: String, CodingKey {
        // ... other keys ...
        case additionalImageUrl = "additional_image_url"
        case additionalImageUrls = "additional_image_urls"
    }

    /// All additional image URLs; falls back to single URL for old API or single image.
    var additionalImages: [String] {
        if let urls = additionalImageUrls, !urls.isEmpty {
            return urls
        }
        if let single = additionalImageUrl {
            return [single]
        }
        return []
    }
}
```

---

## 2. Displaying multiple images

- Use `additional_image_urls` (or your `additionalImages` helper) to show a list/grid of thumbnails.
- For each image you can allow “remove” → call delete by index (see below).

---

## 3. Update additional data (existing request)

**Endpoint:** `PATCH` or `POST` `/api/requests/{id}/additional` (multipart)

| Purpose | How to send |
|--------|-------------|
| **Replace all with one image** (old behavior) | Send **only** `additional_image` (single file). Replaces all existing. |
| **Add more images** (append) | Send `additional_images[]` with one or more files. Appends to existing. |
| **Remove all images** | Send `remove_additional_image = true` (form field). |
| **Remove one image** | Send `remove_additional_image_index = 0` (0-based index). |

**Swift: append multiple images (multipart):**

```swift
func updateAdditionalImages(requestId: Int, imageURLs: [URL]) async throws {
    let url = URL(string: "\(baseURL)/api/requests/\(requestId)/additional")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST" // or PUT, depending on your API route
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")

    let boundary = UUID().uuidString
    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    var body = Data()
    for (index, imageURL) in imageURLs.enumerated() {
        let data = try Data(contentsOf: imageURL)
        let name = "additional_images[\(index)]"  // API expects additional_images[]
        body.append("--\(boundary)\r\n".data(using: .utf8)!)
        body.append("Content-Disposition: form-data; name=\"\(name)\"; filename=\"image\(index).jpg\"\r\n".data(using: .utf8)!)
        body.append("Content-Type: image/jpeg\r\n\r\n".data(using: .utf8)!)
        body.append(data)
        body.append("\r\n".data(using: .utf8)!)
    }
    body.append("--\(boundary)--\r\n".data(using: .utf8)!)
    request.httpBody = body

    let (_, response) = try await URLSession.shared.data(for: request)
    // handle response
}
```

**Swift: remove one image by index:**

```swift
func removeAdditionalImage(requestId: Int, index: Int) async throws {
    let url = URL(string: "\(baseURL)/api/requests/\(requestId)/additional")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.httpBody = "remove_additional_image_index=\(index)".data(using: .utf8)

    let (_, _) = try await URLSession.shared.data(for: request)
}
```

**Swift: remove all additional images:**

```swift
request.httpBody = "remove_additional_image=1".data(using: .utf8)
```

**Swift: replace all with one image (backward compat):**

```swift
// Send single file under key "additional_image" (not additional_images[])
// Only one file; do not send additional_images[] — then backend replaces all.
let name = "additional_image"
// ... build multipart with one file with this name ...
```

---

## 4. Create new request (store)

**Endpoint:** `POST /api/requests` (multipart)

- **One image:** send `additional_image` (single file).
- **Multiple images:** send `additional_images[0]`, `additional_images[1]`, … (or `additional_images[]` depending on your HTTP client).

Response includes `additional_image_url` and `additional_image_urls` as above.

---

## 5. Summary for Swift

1. **Decode** `additional_image_url` and `additional_image_urls`; prefer `additional_image_urls` for UI.
2. **Display** all URLs from `additional_image_urls` (or fallback to `[additional_image_url]`).
3. **Add images:** POST multipart with `additional_images[0]`, `additional_images[1]`, …
4. **Remove one:** POST `remove_additional_image_index=0` (0-based).
5. **Remove all:** POST `remove_additional_image=1`.
6. **Replace all with one:** POST single file as `additional_image` only (no `additional_images[]`).

If you open your iOS project in this workspace or paste the relevant Swift files (e.g. request model, API service, new request view), the same changes can be applied directly to your code.
