# API Structure - Quick Reference

## Base URL
```
http://your-domain.com/api
```

## Authentication Header
```
Authorization: Bearer {token}
```

---

## Dashboard API

### Endpoint
```
GET /api/tenant/{tenant_id}/dashboard
```

### Request Headers
```
Authorization: Bearer {token}
Content-Type: application/json
Accept: application/json
```

### Success Response (200)
```json
{
  "success": true,
  "data": {
    "travel_bookings": {
      "total": 100,
      "assigned": 75,
      "unassigned": 25,
      "by_status": {
        "pending": 30,
        "in_progress": 40,
        "completed": 25,
        "cancelled": 5
      },
      "recent": 10,
      "upcoming": 15
    },
    "users": {
      "total": 35,
      "role_3": 35,
      "active": 30,
      "inactive": 5
    },
    "quotations": {
      "total": 80,
      "active": 60,
      "inactive": 20,
      "recent": 8
    }
  }
}
```

### Error Responses

**401 Unauthorized**
```json
{
  "success": false,
  "message": "Unauthorized"
}
```

**500 Server Error**
```json
{
  "success": false,
  "message": "Failed to fetch dashboard statistics",
  "error": "Error details"
}
```

---

## Quick Integration Examples

### Fetch API (Vanilla JavaScript)
```javascript
async function getDashboard(tenantId, token) {
  const response = await fetch(
    `http://your-domain.com/api/tenant/${tenantId}/dashboard`,
    {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      }
    }
  );
  
  const data = await response.json();
  return data;
}

// Usage
getDashboard('1', 'your-token-here')
  .then(result => {
    if (result.success) {
      console.log(result.data);
    }
  })
  .catch(error => console.error(error));
```

### Axios
```javascript
import axios from 'axios';

const getDashboard = async (tenantId) => {
  const response = await axios.get(
    `/api/tenant/${tenantId}/dashboard`,
    {
      headers: {
        'Authorization': `Bearer ${localStorage.getItem('token')}`
      }
    }
  );
  return response.data;
};
```

### React Hook
```javascript
import { useState, useEffect } from 'react';

function useDashboard(tenantId) {
  const [data, setData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    fetch(`/api/tenant/${tenantId}/dashboard`, {
      headers: {
        'Authorization': `Bearer ${localStorage.getItem('token')}`
      }
    })
      .then(res => res.json())
      .then(result => {
        if (result.success) {
          setData(result.data);
        } else {
          setError(result.message);
        }
      })
      .catch(err => setError(err.message))
      .finally(() => setLoading(false));
  }, [tenantId]);

  return { data, loading, error };
}
```

---

## Data Fields Reference

| Field | Type | Description |
|-------|------|-------------|
| `travel_bookings.total` | number | Total bookings count |
| `travel_bookings.assigned` | number | Assigned bookings count |
| `travel_bookings.unassigned` | number | Unassigned bookings count |
| `travel_bookings.by_status.pending` | number | Pending bookings |
| `travel_bookings.by_status.in_progress` | number | In progress bookings |
| `travel_bookings.by_status.completed` | number | Completed bookings |
| `travel_bookings.by_status.cancelled` | number | Cancelled bookings |
| `travel_bookings.recent` | number | Recent bookings (7 days) |
| `travel_bookings.upcoming` | number | Upcoming bookings |
| `users.total` | number | Total users with role_id = 3 |
| `users.role_3` | number | Users with role_id = 3 (same as total) |
| `users.active` | number | Active users with role_id = 3 |
| `users.inactive` | number | Inactive users with role_id = 3 |

**Note:** All user statistics are filtered to only include users with `role_id = 3`.
| `quotations.total` | number | Total quotations |
| `quotations.active` | number | Active quotations |
| `quotations.inactive` | number | Inactive quotations |
| `quotations.recent` | number | Recent quotations (7 days) |

---

## Testing

### cURL
```bash
curl -X GET "http://your-domain.com/api/tenant/1/dashboard" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json"
```

### Postman
- Method: GET
- URL: `http://your-domain.com/api/tenant/1/dashboard`
- Headers:
  - `Authorization`: `Bearer YOUR_TOKEN`
  - `Content-Type`: `application/json`

