# Mobile API Contracts — `/api/v1` (Customer App)

This file defines the **production API contract** consumed by the Flutter customer app.

It standardizes:
- response envelope
- pagination meta
- auth/token lifecycle
- module list/detail endpoints (type-safe)
- leads, ads, quick-pay payments

## 0) Response envelope (standard)

Every endpoint MUST respond with a consistent shape.

```json
{
  "success": true,
  "message": "Human readable message",
  "data": {},
  "meta": {
    "trace_id": "uuid-or-request-id"
  },
  "errors": null
}
```

### Error format

Validation (422):

```json
{
  "success": false,
  "message": "Validation failed.",
  "data": null,
  "meta": { "trace_id": "..." },
  "errors": {
    "mobile": ["The mobile field is required."]
  }
}
```

Auth (401):

```json
{
  "success": false,
  "message": "Authentication required.",
  "data": null,
  "meta": { "trace_id": "..." },
  "errors": null
}
```

## 1) Pagination contract

For paginated endpoints:

```json
{
  "success": true,
  "message": "Fetched successfully.",
  "data": [/* items */],
  "meta": {
    "trace_id": "...",
    "pagination": {
      "page": 1,
      "per_page": 20,
      "total": 123,
      "last_page": 7,
      "has_next": true
    }
  },
  "errors": null
}
```

## 2) Auth (OTP + Sanctum tokens)

Auth is token-based. Flutter stores token in secure storage and sends:

`Authorization: Bearer <token>`

### 2.1 Send OTP (public)

- **POST** `/api/v1/auth/send-otp`
- **Body**

```json
{ "mobile": "9876543210" }
```

- **200 Response**

```json
{
  "success": true,
  "message": "OTP sent successfully.",
  "data": { "mobile": "9876543210", "expires_in_seconds": 300, "sms_sent": true },
  "meta": { "trace_id": "..." },
  "errors": null
}
```

### 2.2 Verify OTP (public)

- **POST** `/api/v1/auth/verify-otp`
- **Body**

```json
{ "mobile": "9876543210", "code": "123456" }
```

- **200 Response**

```json
{
  "success": true,
  "message": "Authenticated successfully.",
  "data": {
    "token": "plainTextSanctumToken",
    "user": {
      "id": 1,
      "name": "User 3210",
      "mobile": "9876543210"
    }
  },
  "meta": { "trace_id": "..." },
  "errors": null
}
```

### 2.3 Me (auth)

- **GET** `/api/v1/me`
- **200 Response**: `data.user`

### 2.4 Logout (auth)

- **POST** `/api/v1/auth/logout`
- revokes current token

## 3) Modules (Property / Jobs / Education / Services)

The web app uses separate tables (`properties`, `jobs`, `educations`, `local_services`).

### 3.1 Module list (public)

- **GET** `/api/v1/modules`
- **Query**
  - `type`: `property|job|education|service` (required)
  - `page` (optional)
  - `per_page` (optional, default 20, max 50)
  - `sort` (optional; default `latest`)
  - `q` (optional; search title/description; implementation per module)

> Note: current code supports `type` + `per_page` with generic mapping; mobile contract expands filters, but backend may implement module-by-module.

- **200 Response**: `data[]` items (minimal list card)

```json
{
  "id": 123,
  "type": "property",
  "slug": "my-slug",
  "title": "Listing title",
  "location": "Lucknow",
  "price": 3500000,
  "price_display": "₹35,00,000",
  "image": "https://...",
  "badges": { "purpose": "rent", "bhk": "2 BHK" },
  "created_at": "2026-05-06T10:00:00Z"
}
```

### 3.2 Type-safe module detail (public, except property)

**Avoid ambiguous** `GET /modules/{id}` (current backend searches multiple tables by id).

Define:
- **GET** `/api/v1/property/{id}` (auth required if we keep parity with web)
- **GET** `/api/v1/jobs/{id}`
- **GET** `/api/v1/education/{id}`
- **GET** `/api/v1/services/{id}` (local service listing)

Response includes:
- gallery
- details map (module-specific)
- already_interested (auth only)
- map links (property/service)

## 4) Leads

Two lead-capture families exist in web:
- generic “interested/enquiry” for a module (property/job/education/service)
- specialized forms (job application, education enquiry)

### 4.1 Create lead (public OR auth depending on module rules)

- **POST** `/api/v1/leads`
- **Auth**
  - optional for property/job/education enquiry
  - **required** for service enquiry if parity with web is kept (recommended)
- **Body** (generic)

```json
{
  "module_type": "property",
  "module_id": 123,
  "service_id": 5,
  "lead_category": "module_enquiry",
  "name": "Amit",
  "mobile": "9876543210",
  "location": "Lucknow",
  "message": "Interested in this property."
}
```

- **201 Response**

```json
{
  "success": true,
  "message": "Lead submitted successfully.",
  "data": {
    "id": 555,
    "user_id": 1,
    "service_id": 5,
    "name": "Amit",
    "mobile": "9876543210",
    "message": "Interested in this property.",
    "status": "new",
    "created_at": "2026-05-06T10:00:00Z"
  },
  "meta": { "trace_id": "..." },
  "errors": null
}
```

### 4.2 My leads (auth)

- **GET** `/api/v1/me/leads`
- **Query**
  - `status` (optional)
  - `page`, `per_page`

### 4.3 Job application lead

To match the web flow (`POST /jobs/apply`), mobile should use an API endpoint instead of the web route.

- **POST** `/api/v1/jobs/apply` (auth)
- **Body**
  - `job_track: it|non_it`
  - `job_category_id`
  - `full_name`
  - `mobile`
  - `education`
  - `work_experience`
  - `resume` (optional file multipart)

### 4.4 Education enquiry lead

- **POST** `/api/v1/education/enquire` (public)
- **Body**
  - `full_name`, `mobile`, `study_level`
  - `preferred_destination?`, `desired_field?`, `city?`, `message?`
  - `program_context?`

## 5) Ads

### 5.1 List ads (public)

- **GET** `/api/v1/ads`
- **Query**
  - `placement` (default: `home`)
  - `per_page` (default: 10; max 50)
- **200 Response**
  - `data[]` items from `AdvertisementResource` (`id,title,image,link,placement,platform,status,start_date,end_date`)

### 5.2 Track ad click (public)

Mobile should not rely on `GET /ads/{id}/click` (web route). Add:

- **POST** `/api/v1/ads/{ad}/click`
- **Body**
  - `source` (optional): `home|listing|detail|dashboard`
  - `device_id` (optional)
- **Response**: `success=true` + optionally `open_url`

## 6) Services (definitions / masters)

Mobile uses these for quick-pay services list and potentially for “service definitions”.

- **GET** `/api/v1/services`
- **GET** `/api/v1/services/{category}` (id or name for backwards compatibility; prefer id in app)

## 7) Payments / Quick Pay (Razorpay)

The web quick-pay uses:
- create order
- open Razorpay checkout
- verify signature
- receipt

Mobile requires the same.

### 7.1 Create Razorpay order (auth)

- **POST** `/api/v1/payments/razorpay/order`
- **Body**

```json
{
  "amount": 1999.0,
  "service_name": "Education",
  "customer_name": "Amit",
  "mobile_number": "9876543210"
}
```

- **201 Response**

```json
{
  "success": true,
  "message": "Order created.",
  "data": {
    "payment": { "id": 10, "status": "created", "amount": 1999.0, "currency": "INR" },
    "razorpay": {
      "key_id": "rzp_live_xxx",
      "order_id": "order_ABC",
      "amount_paise": 199900,
      "currency": "INR",
      "receipt": "quickpay_10"
    }
  },
  "meta": { "trace_id": "..." },
  "errors": null
}
```

### 7.2 Verify Razorpay payment (auth)

- **POST** `/api/v1/payments/razorpay/verify`
- **Body**

```json
{
  "razorpay_order_id": "order_ABC",
  "razorpay_payment_id": "pay_DEF",
  "razorpay_signature": "sig_123"
}
```

- **200 Response**
  - payment status updated to `success|failed`

### 7.3 My payments (auth)

- **GET** `/api/v1/me/payments`
- paginated

## 8) Backward compatibility (existing endpoints)

Existing endpoints in `routes/api.php` that remain but are considered legacy for the app:
- `GET /api/v1/modules/{module}` (ambiguous across tables) — **avoid in Flutter**
- `GET /api/v1/user/leads` — superseded by `GET /api/v1/me/leads`


