# Admin API: Get All Details for a Specific Agent

## Endpoint
`GET /admin/agents/:id` or `GET /admin/agent-details?agent_id=5`

## Description
Returns comprehensive details for a specific agent including:
- Agent basic information
- All receipts (with date filtering)
- All expenses (with date filtering)
- Location logs (with date and time filtering)
- Summary statistics

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

## Query Parameters
- `agent_id` (required if using `/admin/agent-details`): Agent ID
- `start_date` (optional): Start date filter (YYYY-MM-DD) for receipts, expenses, and location logs
- `end_date` (optional): End date filter (YYYY-MM-DD) for receipts, expenses, and location logs
- `start_time` (optional): Start time filter (HH:MM:SS) for location logs
- `end_time` (optional): End time filter (HH:MM:SS) for location logs

## cURL Examples

### 1. Get All Details for Agent ID 5
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/5" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 2. Get Agent Details with Date Range
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/5?start_date=2025-01-01&end_date=2025-12-06" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 3. Get Agent Details with Date and Time Range
```bash
curl -X GET "http://192.168.1.3:3000/admin/agents/5?start_date=2025-12-06&end_date=2025-12-06&start_time=09:00:00&end_time=18:00:00" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 4. Using Query Parameter Route
```bash
curl -X GET "http://192.168.1.3:3000/admin/agent-details?agent_id=5&start_date=2025-01-01&end_date=2025-12-06" \
  -H "Authorization: Bearer YOUR_ADMIN_JWT_TOKEN"
```

### 5. 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/5?start_date=2025-01-01&end_date=2025-12-06" `
-WebSession $session `
-Headers @{
  "Accept"="application/json"
  "Authorization"="Bearer YOUR_ADMIN_JWT_TOKEN"
}
```

## Response Format

### Success Response (200 OK)
```json
{
  "status": "success",
  "message": "Agent details retrieved successfully",
  "data": {
    "agent": {
      "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"
    },
    "filters": {
      "start_date": "2025-01-01",
      "end_date": "2025-12-06",
      "start_time": null,
      "end_time": null
    },
    "summary": {
      "total_receipts": 25,
      "total_collected_amount": 2300.00,
      "total_expenses": 5,
      "total_expense_amount": 500.00,
      "total_location_logs": 150,
      "net_collection": 1800.00
    },
    "receipts": {
      "total": 25,
      "by_date": {
        "2025-12-06": [
          {
            "id": 56,
            "rec_no": "REC1764939408896",
            "loan_id": "DL2025001",
            "cust_name": "Pavithra",
            "cust_mobile": "5346876764",
            "paid_amnt": 92.00,
            "paid_dues": 1,
            "created_at": "2025-12-06T06:30:00.000Z"
          }
        ]
      },
      "list": [
        {
          "id": 56,
          "rec_no": "REC1764939408896",
          "rec_date": "2025-12-06",
          "loan_id": "DL2025001",
          "cust_id": 8,
          "cust_name": "Pavithra",
          "cust_mobile": "5346876764",
          "paid_amnt": 92.00,
          "paid_dues": 1,
          "created_at": "2025-12-06T06:30:00.000Z"
        }
      ]
    },
    "expenses": {
      "total": 5,
      "by_date": {
        "2025-12-06": [
          {
            "id": 53,
            "voucher_id": "EXP11210390",
            "reason": "fuel",
            "amount": 2000.00,
            "photo_url": null,
            "created_at": "2025-12-06T14:23:30.000Z"
          }
        ]
      },
      "list": [
        {
          "id": 53,
          "expense_date": "2025-12-06",
          "voucher_id": "EXP11210390",
          "reason": "fuel",
          "amount": 2000.00,
          "photo_url": null,
          "notes": "Collection Agent Expense - fuel",
          "created_at": "2025-12-06T14:23:30.000Z"
        }
      ]
    },
    "location_logs": {
      "total": 150,
      "logs": [
        {
          "id": 1234,
          "latitude": 11.3410,
          "longitude": 77.7172,
          "accuracy": 10.5,
          "address": "Erode, Tamil Nadu, India",
          "activity_type": "collection",
          "receipt_id": 56,
          "loan_id": "DL2025001",
          "remark": "Collection at customer location",
          "created_at": "2025-12-06T10:30:00.000Z"
        }
      ]
    },
    "timestamp": "2025-12-06T12:00:00.000Z"
  }
}
```

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

### Error Response (404 Not Found)
```json
{
  "status": "error",
  "message": "Agent not found"
}
```

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

## Notes

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

2. **Date Format**: Use `YYYY-MM-DD` format for dates (e.g., `2025-12-06`).

3. **Time Format**: Use `HH:MM:SS` format for times (e.g., `09:00:00`, `18:30:00`).

4. **Date Filtering**: 
   - `start_date` and `end_date` filter receipts, expenses, and location logs
   - If only `start_date` is provided, returns all records from that date onwards
   - If only `end_date` is provided, returns all records up to that date

5. **Time Filtering**: 
   - `start_time` and `end_time` only affect location logs
   - If only date is provided, time defaults to 00:00:00 to 23:59:59
   - If only time is provided (without date), uses today's date

6. **Grouped Data**: 
   - Receipts and expenses are grouped by date in `by_date` object
   - Also available as flat `list` array

7. **Location Logs**: 
   - Includes GPS coordinates, accuracy, address, activity type
   - Can be linked to receipts via `receipt_id` and `loan_id`

8. **Summary Statistics**: 
   - Total receipts count and collected amount
   - Total expenses count and expense amount
   - Total location logs count
   - Net collection (collected amount - expense amount)

## Migration

To ensure the `agent_location_tracking` table exists, run:

```bash
node create_agent_location_tracking_migration.js
```

Or use Sequelize CLI:

```bash
npx sequelize-cli db:migrate
```

