# Admin API: Create Collection Agent

## Endpoint
`POST /admin/agents`

## Description
Creates a new collection agent for the admin's company. Supports optional photo upload and automatic assignment of company ID from the authenticated admin.

## Authentication
Requires admin authentication via Bearer token (JWT or auth_token).

## Request Body Parameters

### Required Fields
- `name` (string): Agent's full name
- `mobile_no` (string): Agent's mobile number (must be 10 digits)

### Optional Fields
- `aadhar_no` (string): Agent's Aadhaar number
- `address` (string): Agent's address
- `login_time` (string): Login time in format "9:00 AM" or "09:00" (default: "9:00 AM")
- `logout_time` (string): Logout time in format "8:30 PM" or "20:30" (default: "8:30 PM")
- `status` (string): Agent status - "active" or "inactive" (default: "active")
- `line` (string): Line assignment for the agent
- `photo` (file): Agent photo (image file - jpeg, jpg, png, gif, webp, max 5MB)

## cURL Examples

### 1. Create Agent with Required Fields Only
```bash
curl -X POST "https://dayloanapp.xesstechlink.com/admin/agents" \
  -H "accept: application/json" \
  -H "authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "content-type: application/json" \
  -d '{
    "name": "Ravi Kumar",
    "mobile_no": "9876543210"
  }'
```

### 2. Create Agent with All Fields
```bash
curl -X POST "https://dayloanapp.xesstechlink.com/admin/agents" \
  -H "accept: application/json" \
  -H "authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "content-type: application/json" \
  -d '{
    "name": "Ravi Kumar",
    "mobile_no": "9876543210",
    "aadhar_no": "1234 5678 9012",
    "address": "123 Main Street, Erode",
    "login_time": "9:00 AM",
    "logout_time": "8:30 PM",
    "status": "active",
    "line": "Line 1"
  }'
```

### 3. Create Agent with Photo Upload (multipart/form-data)
```bash
curl -X POST "https://dayloanapp.xesstechlink.com/admin/agents" \
  -H "accept: application/json" \
  -H "authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -F "name=Ravi Kumar" \
  -F "mobile_no=9876543210" \
  -F "aadhar_no=1234 5678 9012" \
  -F "address=123 Main Street, Erode" \
  -F "login_time=9:00 AM" \
  -F "logout_time=8:30 PM" \
  -F "status=active" \
  -F "line=Line 1" \
  -F "photo=@/path/to/agent-photo.jpg"
```

### 4. Create Agent with Base64 Photo (JSON)
```bash
curl -X POST "https://dayloanapp.xesstechlink.com/admin/agents" \
  -H "accept: application/json" \
  -H "authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "content-type: application/json" \
  -d '{
    "name": "Ravi Kumar",
    "mobile_no": "9876543210",
    "aadhar_no": "1234 5678 9012",
    "address": "123 Main Street, Erode",
    "login_time": "9:00 AM",
    "logout_time": "8:30 PM",
    "status": "active",
    "line": "Line 1",
    "photo": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."
  }'
```

### 5. Create Agent with Custom Times
```bash
curl -X POST "https://dayloanapp.xesstechlink.com/admin/agents" \
  -H "accept: application/json" \
  -H "authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "content-type: application/json" \
  -d '{
    "name": "Priya",
    "mobile_no": "9876543211",
    "login_time": "8:00 AM",
    "logout_time": "6:00 PM",
    "status": "active",
    "line": "Line 2"
  }'
```

## Response Format

### Success Response (201 Created)
```json
{
  "status": "success",
  "message": "Collection agent created successfully",
  "data": {
    "agent": {
      "id": 5,
      "name": "Ravi Kumar",
      "mobile_no": "9876543210",
      "aadhar_no": "1234 5678 9012",
      "address": "123 Main Street, Erode",
      "line": "Line 1",
      "status": "active",
      "com_id": 15,
      "report_in": "2025-12-13T03:30:00.000Z",
      "reporting_time": "2025-12-13T15:00:00.000Z",
      "created_at": "2025-12-13T08:00:00.000Z",
      "has_photo": true
    }
  }
}
```

### Error Response (400 Bad Request - Missing Name)
```json
{
  "status": "error",
  "message": "Name is required"
}
```

### Error Response (400 Bad Request - Missing Mobile)
```json
{
  "status": "error",
  "message": "Mobile number is required"
}
```

### Error Response (400 Bad Request - Invalid Mobile Format)
```json
{
  "status": "error",
  "message": "Mobile number must be 10 digits"
}
```

### Error Response (400 Bad Request - Duplicate Mobile)
```json
{
  "status": "error",
  "message": "Mobile number already exists for an agent in this company"
}
```

### Error Response (401 Unauthorized)
```json
{
  "status": "error",
  "message": "Admin authentication required"
}
```

### Error Response (500 Internal Server Error)
```json
{
  "status": "error",
  "message": "Failed to create collection agent",
  "error": "Error message details"
}
```

## Field Details

### Time Format
- **Login Time / Logout Time**: Accepts formats like:
  - "9:00 AM" or "9:00 am"
  - "8:30 PM" or "8:30 pm"
  - "09:00" (24-hour format)
  - "20:30" (24-hour format)
- **Defaults**: 
  - Login Time: 9:00 AM
  - Logout Time: 8:30 PM

### Status Values
- `"active"` or `"Active"`: Agent is active
- `"inactive"` or `"Inactive"`: Agent is inactive
- **Default**: `"active"`

### Photo Upload
- **File Upload**: Use `multipart/form-data` with field name `photo`
- **Base64**: Send as JSON with `photo` field containing base64 string (must start with `data:image/...`)
- **Supported Formats**: jpeg, jpg, png, gif, webp
- **Max Size**: 5MB
- **Storage**: Photo is stored in database as BLOB

## Notes

1. **Authentication**: Replace `YOUR_ADMIN_JWT_TOKEN` with a valid admin JWT token obtained from `/admin/login` or `/?admin_login=true`.

2. **Company ID**: The agent is automatically assigned to the admin's company (`com_id`). You don't need to provide it in the request.

3. **Mobile Number Validation**: 
   - Must be exactly 10 digits
   - Must be unique within the company
   - Only numbers allowed (no spaces, dashes, or special characters)

4. **Photo Upload**: 
   - If uploading via file, use `multipart/form-data` content type
   - If sending base64, use `application/json` content type
   - Photo is optional - agent can be created without a photo

5. **Time Parsing**: 
   - The API automatically parses time strings in various formats
   - Times are stored as DATE objects in the database
   - If time parsing fails, defaults are used

6. **Line Assignment**: 
   - Line is optional and can be set later
   - Line should match existing lines in `line_entry` table for proper area assignment

## Example with Real Server URL

```bash
curl -X POST "https://dayloanapp.xesstechlink.com/admin/agents" \
  -H "accept: application/json" \
  -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
  -H "content-type: application/json" \
  -d '{
    "name": "Ravi Kumar",
    "mobile_no": "9876543210",
    "aadhar_no": "1234 5678 9012",
    "address": "123 Main Street, Erode",
    "login_time": "9:00 AM",
    "logout_time": "8:30 PM",
    "status": "active",
    "line": "Line 1"
  }'
```

## Response Fields

### Agent Object Fields
- `id`: Unique agent identifier (auto-generated)
- `name`: Agent's full name
- `mobile_no`: Agent's mobile number
- `aadhar_no`: Agent's Aadhaar number (if provided)
- `address`: Agent's address (if provided)
- `line`: Line assignment (if provided)
- `status`: Agent status ("active" or "inactive")
- `com_id`: Company ID (automatically set from admin)
- `report_in`: Login time (DATE object)
- `reporting_time`: Logout time (DATE object)
- `created_at`: Creation timestamp
- `has_photo`: Boolean indicating if photo was uploaded

