# Mobile API Documentation

This document provides comprehensive documentation for all mobile APIs used by Android applications for tree survey data collection.

## Base URL
```
http://172.17.46.11:63318/api/mobile/project/{treelogy_id}
```

## Authentication

### 1. Login
**Endpoint:** `POST /login`

**Description:** Authenticate user and return user details with session information.

**Request Body:**
```json
{
  "email": "user@example.com",
  "password": "password123"
}
```

**Response:**
```json
{
  "success": 1,
  "uid": 123,
  "maptype": 1,
  "icontype": 1,
  "usertype": "ENUMERATOR",
  "email": "user@example.com", 
  "sqid": 0,
  "syncid": 0,
  "imei1": "",
  "imei2": "",
  "ward": 1,
  "accuracy": "120",
  "alarm_frequency": "1800000",
  "location_update_frequency": "5000",
  "tree_details_id_add": "0",
  "nearby_accuracy": "120",
  "nearby_distance": "200",
  "nearby_locreq_freq": "8000",
  "user": {
    "first_name": "John",
    "last_name": "Doe",
    "email": "user@example.com",
    "mobile_no": "1234567890",
    "created_at": "2024-01-01",
    "isAdminRole": 0,
    "user_type": "ENUMERATOR"
  },
  "treedetail": 5,
  "totalCountForDay": 5,
  "token": "jwt_token_here"
}
```

**Error Responses:**
- `success: 2` - No such user
- `success: 3` - Incorrect password
- `success: 4` - User disabled

### 2. Register
**Endpoint:** `POST /register`

**Description:** Register a new user.

**Request Body:**
```json
{
  "first_name": "John",
  "last_name": "Doe",
  "email": "user@example.com",
  "password": "password123",
  "mobile_no": "1234567890",
  "type": "ENUMERATOR"
}
```

**Response:**
```json
{
  "success": 1,
  "uid": 123,
  "user": {
    "first_name": "John",
    "last_name": "Doe",
    "email": "user@example.com",
    "mobile_no": "1234567890",
    "created_at": "2024-01-01",
    "type": "ENUMERATOR"
  }
}
```

### 3. Update SQID
**Endpoint:** `POST /updateSqId`

**Description:** Update or retrieve sequence ID for user.

**Request Body:**
```json
{
  "uid": 123,
  "sqid": 5,
  "update": "yes"
}
```

**Response:**
```json
{
  "success": 1,
  "sqid": 5
}
```

### 4. Get SQID
**Endpoint:** `POST /getSqId`

**Description:** Get current sequence ID for user.

**Request Body:**
```json
{
  "uid": 123
}
```

**Response:**
```json
{
  "success": 1,
  "sqid": 5
}
```

### 5. Get SyncID
**Endpoint:** `POST /getSyncId`

**Description:** Get current sync ID for user.

**Request Body:**
```json
{
  "uid": 123
}
```

**Response:**
```json
{
  "success": 1,
  "syncid": 10
}
```

## Master Data APIs

### 6. Get Tree Details (Master Data)
**Endpoint:** `POST /treedetails`

**Description:** Get all master data required for tree survey.

**Request Body:**
```json
{}
```

**Response:**
```json
{
  "success": 1,
  "treedetails": {
    "treeList": [
      {
        "tree_id": 1,
        "local_name": "Mango",
        "common_name": "Mango Tree",
        "botanical_name": "Mangifera indica"
      }
    ],
    "societyList": [
      {
        "societyname": "Green Society"
      }
    ],
    "roadList": [
      {
        "roadname": "Main Road"
      }
    ],
    "treeBal_Imbalance": [
      {
        "balance_id": 1,
        "isbalance": "Balanced"
      }
    ],
    "treeCondition": [
      {
        "id": 1,
        "condition_name": "Healthy"
      }
    ],
    "treeOwnerShip": [
      {
        "id": 1,
        "ownership_name": "Private"
      }
    ],
    "treeRemarks": [
      {
        "id": 1,
        "remark_name": "Good Condition"
      }
    ],
    "treeSpatialComments": [
      {
        "special_comment_id": 1,
        "special_comment": "Near road"
      }
    ],
    "birdNest": [
      {
        "nest_id": 1,
        "nest_type": "Active"
      }
    ]

```

### 7. Refresh Society Road
**Endpoint:** `POST /refreshsocietyroad`

**Description:** Get updated society and road lists.

**Request Body:**
```json
{}
```

**Response:**
```json
{
  "success": 1,
  "societyroaddetails": {
    "societyList": [
      {
        "societyname": "Green Society"
      }
    ],
    "roadList": [
      {
        "roadname": "Main Road"
      }
    ]
  }
}
```

## Tree Survey APIs

### 8. Store Tree Details
**Endpoint:** `POST /storetreedetails`

**Description:** Store new tree survey data.

**Request Body:**
```json
{
  "user_id": 123,
  "sq_id": 5,
  "tree_id": 1,
  "latitude": 19.0760,
  "longitude": 72.8777,
  "old_latitude": 19.0760,
  "old_longitude": 72.8777,
  "drag_distance": 0,
  "othername": "Local Mango",
  "grith_cm": 150,
  "height_mtr": 8.5,
  "canopy_dia_mtr": 12.0,
  "condition_id": 1,
  "remarks_id": 1,
  "other_remarks": "Healthy tree",
  "ownership_id": 1,
  "ownership_name": "John Doe",
  "ownership_contact_no": "1234567890",
  "balImbalance_id": 1,
  "societyname": "Green Society",
  "roadname": "Main Road",
  "googleAddress": "123 Main Street, City",
  "entered_date": "2024-01-01",
  "entered_time": "10:30:00",
  "updated_date": "2024-01-01",
  "ward": 1,
  "is_within_boundary": 1,
  "version": "1.0",
  "dist_frm_prev_tree": 10.5,
  "spatialcomment_id": 1,
  "birdnest": 0,
  "date_of_plantation": "2020-01-01",
  "client_name": "City Corporation",
  "contractor_name": "Green Contractors"
}
```

**Response:**
```json
{
  "success": 1
}
```

### 9. Update Tree Details
**Endpoint:** `POST /updatetreedetails`

**Description:** Update existing tree survey data.

**Request Body:**
```json
{
  "tree_details_id": 456,
  "user_id": 123,
  "tree_id": 1,
  "latitude": 19.0760,
  "longitude": 72.8777,
  "othername": "Updated Mango",
  "grith_cm": 155,
  "height_mtr": 9.0,
  "canopy_dia_mtr": 13.0,
  "condition_id": 1,
  "remarks_id": 1,
  "other_remarks": "Updated remarks",
  "ownership_id": 1,
  "ownership_name": "Jane Doe",
  "ownership_contact_no": "0987654321",
  "balImbalance_id": 1,
  "societyname": "Green Society",
  "roadname": "Main Road",
  "qc_id": 789,
  "qcstatus": 1,
  "isWithin": 1,
  "dist_frm_prev_tree": 11.0,
  "spatialcomment_id": 1,
  "birdnest": 0,
  "date_of_plantation": "2020-01-01",
  "client_name": "City Corporation",
  "contractor_name": "Green Contractors"
}
```

**Response:**
```json
{
  "success": 1,
  "treedetail": "update_result"
}
```

### 10. Get Tree Details by User ID
**Endpoint:** `POST /getTreeDetailsByUserID`

**Description:** Get all tree details submitted by a specific user.

**Request Body:**
```json
{
  "user_id": 123
}
```

**Response:**
```json
{
  "success": 1,
  "treedetail": [
    {
      "tree_details_id": 456,
      "user_id": 123,
      "tree_id": 1,
      "latitude": 19.0760,
      "longitude": 72.8777,
      "grith_cm": 150,
      "height_mtr": 8.5,
      "entered_date": "2024-01-01"
    }
  ]
}
```

## QC (Quality Control) APIs

### 11. Get Tree Details for QC
**Endpoint:** `POST /getTreeDetailsForQC`

**Description:** Get all tree details pending QC review.

**Request Body:**
```json
{
  "user_id": 123
}
```

**Response:**
```json
{
  "success": 1,
  "count": 25,
  "treedetail": [
    {
      "tree_details_id": 456,
      "user_id": 123,
      "tree_id": 1,
      "latitude": 19.0760,
      "longitude": 72.8777,
      "qc_done": 0,
      "entered_date": "2024-01-01"
    }
  ]
}
```

### 12. QC (Quality Control)
**Endpoint:** `POST /qc`

**Description:** Update QC status for tree details.

**Request Body:**
```json
{
  "qc_id": 789,
  "tree_id": 456,
  "qcstatus": 1,
  "all": 0,
  "user_id": 123,
  "is_admin": 0
}
```

**Response:**
```json
{
  "success": 1,
  "treedetail": "qc_update_result"
}
```

## Location Tracking APIs

### 13. Submit Location
**Endpoint:** `POST /submit_location`

**Description:** Store user login location.

**Request Body:**
```json
{
  "uid": 123,
  "lat": 19.0760,
  "lon": 72.8777
}
```

**Response:**
```json
{
  "success": 1,
  "message": "login details stored"
}
```

### 14. Save Track Path
**Endpoint:** `POST /savetrackpath`

**Description:** Store user tracking path data.

**Request Body:**
```json
{
  "uid": 123,
  "latitude": 19.0760,
  "longitude": 72.8777
}
```

**Response:**
```json
{
  "success": 1,
  "message": "location stored"
}
```

### 15. Get Nearby Trees
**Endpoint:** `POST /getNearbyTrees`

**Description:** Get trees within specified coordinates.

**Request Body:**
```json
{
  "northLat": 19.0860,
  "southLat": 19.0660,
  "westLon": 72.8677,
  "eastLon": 72.8877
}
```

**Response:**
```json
{
  "success": 1,
  "treedetail": [
    {
      "tree_details_id": 456,
      "user_id": 123,
      "tree_id": 1,
      "latitude": 19.0760,
      "longitude": 72.8777,
      "grith_cm": 150,
      "height_mtr": 8.5
    }
  ]
}
```

## Report APIs

### 16. Get Excel Report
**Endpoint:** `POST /getExcelReport`

**Description:** Generate Excel report for specified date range.

**Request Body:**
```json
{
  "fromDate": "2024-01-01",
  "toDate": "2024-01-31",
  "user_id": 123
}
```

**Response:**
```json
{
  "success": 1,
  "fromdate": "2024-01-01",
  "todate": "2024-01-31",
  "url": "http://172.17.46.11:63318/api/mobile/project/1/excel-report"
}
```

### 17. Get QC Report
**Endpoint:** `POST /getQcReport`

**Description:** Get QC statistics report.

**Request Body:**
```json
{
  "uid": 123,
  "utype": "QC"
}
```

**Response:**
```json
{
  "success": 1,
  "email": "qc@example.com",
  "qcDoneCount": 100,
  "qcPendingCount": 25,
  "qcAccCount": 80,
  "qcRejCount": 15,
  "qcRecCount": 5
}
```

## Error Responses

All APIs return standard error responses:

```json
{
  "success": 0,
  "error": 1,
  "error_msg": "Error description"
}
```

## Status Codes

- `200` - Success
- `400` - Bad Request (missing required fields)
- `401` - Unauthorized (invalid credentials)
- `500` - Internal Server Error

## Authentication

Most APIs require authentication. Include the JWT token in the Authorization header:

```
Authorization: Bearer <jwt_token>
```

## Rate Limiting

APIs are rate-limited to prevent abuse. Default limits:
- 100 requests per minute per user
- 1000 requests per hour per IP

## Database Schema

The mobile APIs work with the project-specific database containing these main tables:
- `treeusers` - User accounts
- `treedetailsentered` - Tree survey data
- `treelist` - Master tree species data
- `condition` - Tree condition master data
- `ownership` - Ownership type master data
- `remarks` - Tree remarks master data
- `login_details` - User location tracking
- `track_path` - User path tracking

## Notes

1. All coordinates are in decimal degrees (latitude, longitude)
2. Dates are in YYYY-MM-DD format
3. Times are in HH:MM:SS format
4. All measurements are in metric units (cm, meters)
5. The `treelogy_id` parameter in the URL determines which project database to use
6. Image uploads are handled separately through file upload endpoints
7. QC status values: 0 = Pending, 1 = Accepted, 2 = Rejected
8. User types: ENUMERATOR, QC, ADMIN, SUPER_ADMIN
