# Property Broucher File Management API Documentation

This document describes the new API endpoints added for managing broucher files in the Property model's `new_project_details` field.

## Model Changes

### Property Model Interface

The `new_project_details` field now supports file attachments for brouchers:

```typescript
new_project_details?: {
  bhk_type: string;
  bhk_id: string;
  carpet_area: string;
  price: string;
  broucher?: {
    document: string;
    type: string;
    mime_type: string;
    original_name: string;
  };
}[];
```

## API Endpoints

### 1. Upload Broucher File

**POST** `/api/property/:propertyId/project-detail/:projectDetailIndex/broucher/upload`

Upload a broucher file (PDF or image) for a specific project detail.

**Headers:**

- `Authorization: Bearer <jwt_token>`
- `Content-Type: multipart/form-data`

**Parameters:**

- `propertyId` (string): The property ID
- `projectDetailIndex` (number): Index of the project detail (0-based)

**Body:**

- `broucher` (file): The file to upload (PDF or image)

**Response:**

```json
{
  "status": 200,
  "data": {
    // Updated property object
  },
  "message": "Broucher file uploaded successfully"
}
```

**Example:**

```bash
curl -X POST \
  'http://localhost:3000/api/property/60d5ecf4f3d7b4001f8b4567/project-detail/0/broucher/upload' \
  -H 'Authorization: Bearer <your_jwt_token>' \
  -F 'broucher=@/path/to/broucher.pdf'
```

### 2. Download Broucher File

**GET** `/api/property/:propertyId/project-detail/:projectDetailIndex/broucher/download`

Download the broucher file for a specific project detail.

**Headers:**

- `Authorization: Bearer <jwt_token>`

**Parameters:**

- `propertyId` (string): The property ID
- `projectDetailIndex` (number): Index of the project detail (0-based)

**Response:**

- File stream with appropriate headers for download

**Example:**

```bash
curl -X GET \
  'http://localhost:3000/api/property/60d5ecf4f3d7b4001f8b4567/project-detail/0/broucher/download' \
  -H 'Authorization: Bearer <your_jwt_token>' \
  --output downloaded_broucher.pdf
```

### 3. Delete Broucher File

**DELETE** `/api/property/:propertyId/project-detail/:projectDetailIndex/broucher`

Delete the broucher file for a specific project detail.

**Headers:**

- `Authorization: Bearer <jwt_token>`

**Parameters:**

- `propertyId` (string): The property ID
- `projectDetailIndex` (number): Index of the project detail (0-based)

**Response:**

```json
{
  "status": 200,
  "data": {
    // Updated property object
  },
  "message": "Broucher file deleted successfully"
}
```

**Example:**

```bash
curl -X DELETE \
  'http://localhost:3000/api/property/60d5ecf4f3d7b4001f8b4567/project-detail/0/broucher' \
  -H 'Authorization: Bearer <your_jwt_token>'
```

### 4. Add New Project Detail

**POST** `/api/property/:propertyId/project-detail`

Add a new project detail to a property.

**Headers:**

- `Authorization: Bearer <jwt_token>`
- `Content-Type: application/json`

**Parameters:**

- `propertyId` (string): The property ID

**Body:**

```json
{
  "bhk_type": "2BHK",
  "bhk_id": "2bhk_001",
  "carpet_area": "1200 sqft",
  "price": "50,00,000",
  "broucher": {
    "document": "filename.pdf",
    "type": "file",
    "mime_type": "application/pdf",
    "original_name": "project_broucher.pdf"
  }
}
```

**Response:**

```json
{
  "status": 200,
  "data": {
    // Updated property object
  },
  "message": "New project detail added successfully"
}
```

### 5. Update Project Detail

**PUT** `/api/property/:propertyId/project-detail/:projectDetailIndex`

Update an existing project detail.

**Headers:**

- `Authorization: Bearer <jwt_token>`
- `Content-Type: application/json`

**Parameters:**

- `propertyId` (string): The property ID
- `projectDetailIndex` (number): Index of the project detail (0-based)

**Body:**

```json
{
  "bhk_type": "3BHK",
  "price": "75,00,000",
  "broucher": {
    "document": "new_filename.pdf",
    "type": "file",
    "mime_type": "application/pdf",
    "original_name": "updated_broucher.pdf"
  }
}
```

**Response:**

```json
{
  "status": 200,
  "data": {
    // Updated property object
  },
  "message": "Project detail updated successfully"
}
```

## Error Responses

All endpoints may return the following error responses:

### 400 Bad Request

```json
{
  "status": 400,
  "message": "File not provided" // or other validation error
}
```

### 401 Unauthorized

```json
{
  "status": 401,
  "message": "Unauthorized"
}
```

### 404 Not Found

```json
{
  "status": 404,
  "message": "Property not found" // or "Project detail not found" or "Broucher file not found"
}
```

### 500 Internal Server Error

```json
{
  "status": 500,
  "message": "Internal server error"
}
```

## File Upload Restrictions

- **Supported file types**: PDF, JPG, JPEG, PNG, GIF
- **Maximum file size**: As configured in multer middleware
- **Upload directory**: `uploads/` (relative to project root)

## Usage Examples

### Complete Workflow Example

1. **Create a new project detail with broucher:**

```javascript
// First add project detail without broucher
const projectDetail = await fetch(
  "/api/property/60d5ecf4f3d7b4001f8b4567/project-detail",
  {
    method: "POST",
    headers: {
      Authorization: "Bearer <jwt_token>",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      bhk_type: "2BHK",
      bhk_id: "2bhk_001",
      carpet_area: "1200 sqft",
      price: "50,00,000",
    }),
  }
);

// Then upload broucher file (assuming it's the first project detail, index 0)
const formData = new FormData();
formData.append("broucher", fileInput.files[0]);

const uploadResult = await fetch(
  "/api/property/60d5ecf4f3d7b4001f8b4567/project-detail/0/broucher/upload",
  {
    method: "POST",
    headers: {
      Authorization: "Bearer <jwt_token>",
    },
    body: formData,
  }
);
```

2. **Download broucher file:**

```javascript
const response = await fetch(
  "/api/property/60d5ecf4f3d7b4001f8b4567/project-detail/0/broucher/download",
  {
    headers: {
      Authorization: "Bearer <jwt_token>",
    },
  }
);

if (response.ok) {
  const blob = await response.blob();
  const url = window.URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = "broucher.pdf";
  a.click();
}
```

## Service Methods

The following methods have been added to the `PropertyService` class:

- `uploadBroucherFile(propertyId, projectDetailIndex, fileData)`
- `downloadBroucherFile(propertyId, projectDetailIndex)`
- `deleteBroucherFile(propertyId, projectDetailIndex)`
- `addNewProjectDetail(propertyId, projectDetailData)`
- `updateProjectDetail(propertyId, projectDetailIndex, updateData)`

These methods handle the business logic for managing broucher files and project details in the database.
