# Admin API: Get List of All Agents

## Endpoint
`GET /admin/agents`

## Description
Returns a list of all collection agents for the admin's company. Supports filtering by line, status, and search by name/mobile number.

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

## Query Parameters
- `line` (optional): Filter agents by line (e.g., "Line 1", "Line 2")
- `status` (optional): Filter agents by status (e.g., "Active", "Inactive")
- `search` (optional): Search agents by name or mobile number

## cURL Examples

### 1. Get All Agents
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents" \
  -H "Accept: application/json" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN" \
  -H "Content-Type: application/json"
```

### 2. Filter by Line
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents?line=Line%202" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 3. Filter by Status
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents?status=Active" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 4. Search by Name or Mobile
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents?search=Raja" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 5. Multiple Filters
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents?line=Line%202&status=Active" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 6. PowerShell Example
```powershell
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1"

Invoke-WebRequest -UseBasicParsing -Uri "http://192.168.1.3:3000/admin/agents" `
-WebSession $session `
-Headers @{
  "Accept"="application/json"
  "Authorization"="Bearer YOUR_ADMIN_JWT_TOKEN"
}
```

## Response Format

### Success Response (200 OK)
```json
{
  "status": "success",
  "message": "Agents list retrieved successfully",
  "data": {
    "filters": {
      "company_id": 15,
      "line": null,
      "status": null,
      "search": null
    },
    "summary": {
      "total_agents": 10,
      "active_agents": 8,
      "logged_in_agents": 5,
      "filtered_count": 10
    },
    "agents": [
      {
        "id": 5,
        "name": "Raja",
        "mobile_no": "8825948300",
        "line": "Line 2",
        "status": "Active",
        "address": "Erode",
        "aadhar_no": "882594830012",
        "is_logged_in": true,
        "device_id": "device123",
        "last_login": "2025-12-06T10:30:00.000Z",
        "created_at": "2025-12-03T17:31:14.000Z"
      },
      {
        "id": 6,
        "name": "Kumar",
        "mobile_no": "9876543210",
        "line": "Line 1",
        "status": "Active",
        "address": "Chennai",
        "aadhar_no": "987654321012",
        "is_logged_in": false,
        "device_id": null,
        "last_login": null,
        "created_at": "2025-12-04T10:00:00.000Z"
      }
    ],
    "timestamp": "2025-12-06T12:00:00.000Z"
  }
}
```

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

### Error Response (500 Internal Server Error)
```json
{
  "status": "error",
  "message": "Failed to fetch agents list",
  "error": "Error message details"
}
```

## Notes

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

2. **Company Filtering**: The endpoint automatically filters agents by the admin's company_id (from the JWT token or auth_token).

3. **Search**: The search parameter searches both agent name and mobile number (case-insensitive partial match).

4. **Status Values**: Common status values are "Active", "Inactive", but may vary based on your database.

5. **URL Encoding**: When using query parameters with spaces, URL encode them (e.g., `Line%202` for "Line 2").

6. **Summary Statistics**: The response includes:
   - Total agents in the company
   - Active agents count
   - Currently logged in agents count
   - Filtered results count

## Example with Real Server URL

```bash
curl -X GET "http://192.168.1.3:3000/admin/agents" \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
```

