# API Documentation

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

## Authentication

The API uses token-based authentication via Laravel Sanctum. All protected endpoints require a Bearer token in the Authorization header.

### Getting an Access Token

**Endpoint:** `POST /api/v1/login`

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

**Success Response (200):**
```json
{
    "success": true,
    "message": "Login successful",
    "data": {
        "user": {
            "id": 1,
            "name": "John Doe",
            "email": "user@example.com",
            "user_type": "admin",
            "shop_id": 1
        },
        "access_token": "1|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "token_type": "Bearer"
    }
}
```

**Error Response (401):**
```json
{
    "success": false,
    "message": "Invalid credentials",
    "errors": {
        "email": ["The provided credentials are incorrect."]
    }
}
```

### Using the Token

Include the token in the Authorization header for all protected endpoints:

```
Authorization: Bearer 1|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

### Token Expiration

By default, tokens expire after 24 hours (1440 minutes). This can be configured in `.env`:

```
SANCTUM_TOKEN_EXPIRATION=1440
```

Set to `null` for tokens that never expire (not recommended for production).

---

## Authentication Endpoints

### Get Authenticated User
**Endpoint:** `GET /api/v1/me`

**Headers:**
```
Authorization: Bearer {token}
```

**Success Response (200):**
```json
{
    "success": true,
    "message": "User retrieved successfully",
    "data": {
        "user": {
            "id": 1,
            "name": "John Doe",
            "email": "user@example.com",
            "user_type": "admin",
            "shop_id": 1,
            "contact_no": "1234567890",
            "status": "Active",
            "shop": {
                "id": 1,
                "title": "Main Shop",
                "owner_name": "John Doe"
            }
        }
    }
}
```

### Logout (Current Device)
**Endpoint:** `POST /api/v1/logout`

**Headers:**
```
Authorization: Bearer {token}
```

**Success Response (200):**
```json
{
    "success": true,
    "message": "Logged out successfully"
}
```

### Logout All Devices
**Endpoint:** `POST /api/v1/logout-all`

**Headers:**
```
Authorization: Bearer {token}
```

**Success Response (200):**
```json
{
    "success": true,
    "message": "Logged out from all devices successfully"
}
```

---

## Shop Endpoints

### List Shops
**Endpoint:** `GET /api/v1/shops`

**Headers:**
```
Authorization: Bearer {token}
```

**Query Parameters:**
- `per_page` (optional): Number of items per page (default: 15)
- `status` (optional): Filter by status (Active/Inactive)
- `search` (optional): Search in title, owner_name, contact_no, address

**Success Response (200):**
```json
{
    "success": true,
    "message": "Shops retrieved successfully",
    "data": [
        {
            "id": 1,
            "title": "Main Shop",
            "address": "123 Main St",
            "owner_name": "John Doe",
            "contact_no": "1234567890",
            "status": "Active",
            "user_id": 1,
            "created_at": "2025-12-04T10:00:00.000000Z",
            "updated_at": "2025-12-04T10:00:00.000000Z"
        }
    ],
    "meta": {
        "current_page": 1,
        "per_page": 15,
        "total": 1,
        "last_page": 1
    }
}
```

### Get Single Shop
**Endpoint:** `GET /api/v1/shops/{id}`

**Headers:**
```
Authorization: Bearer {token}
```

**Success Response (200):**
```json
{
    "success": true,
    "message": "Shop retrieved successfully",
    "data": {
        "id": 1,
        "title": "Main Shop",
        "address": "123 Main St",
        "owner_name": "John Doe",
        "contact_no": "1234567890",
        "status": "Active",
        "user_id": 1,
        "created_at": "2025-12-04T10:00:00.000000Z",
        "updated_at": "2025-12-04T10:00:00.000000Z"
    }
}
```

**Error Response (403):**
```json
{
    "success": false,
    "message": "Unauthorized access",
    "errors": {
        "authorization": ["You do not have permission to access this shop."]
    }
}
```

### Create Shop
**Endpoint:** `POST /api/v1/shops`

**Headers:**
```
Authorization: Bearer {token}
Content-Type: application/json
```

**Request Body:**
```json
{
    "title": "New Shop",
    "address": "456 Oak Ave",
    "owner_name": "Jane Smith",
    "contact_no": "9876543210",
    "status": "Active"
}
```

**Success Response (201):**
```json
{
    "success": true,
    "message": "Shop created successfully",
    "data": {
        "id": 2,
        "title": "New Shop",
        "address": "456 Oak Ave",
        "owner_name": "Jane Smith",
        "contact_no": "9876543210",
        "status": "Active",
        "user_id": 1,
        "created_at": "2025-12-04T11:00:00.000000Z",
        "updated_at": "2025-12-04T11:00:00.000000Z"
    }
}
```

**Validation Error Response (422):**
```json
{
    "success": false,
    "message": "Validation failed",
    "errors": {
        "title": ["The shop title is required."],
        "owner_name": ["The owner name is required."]
    }
}
```

### Update Shop
**Endpoint:** `PUT /api/v1/shops/{id}` or `PATCH /api/v1/shops/{id}`

**Headers:**
```
Authorization: Bearer {token}
Content-Type: application/json
```

**Request Body:**
```json
{
    "title": "Updated Shop Name",
    "address": "789 Pine St",
    "owner_name": "Jane Smith",
    "contact_no": "9876543210",
    "status": "Active"
}
```

**Success Response (200):**
```json
{
    "success": true,
    "message": "Shop updated successfully",
    "data": {
        "id": 2,
        "title": "Updated Shop Name",
        "address": "789 Pine St",
        "owner_name": "Jane Smith",
        "contact_no": "9876543210",
        "status": "Active",
        "user_id": 1,
        "created_at": "2025-12-04T11:00:00.000000Z",
        "updated_at": "2025-12-04T11:30:00.000000Z"
    }
}
```

### Delete Shop
**Endpoint:** `DELETE /api/v1/shops/{id}`

**Headers:**
```
Authorization: Bearer {token}
```

**Success Response (200):**
```json
{
    "success": true,
    "message": "Shop deleted successfully"
}
```

---

## Error Responses

All error responses follow a standardized format:

```json
{
    "success": false,
    "message": "Error message",
    "errors": {
        "field_name": ["Error message for this field"]
    }
}
```

### HTTP Status Codes

- `200` - Success
- `201` - Created
- `401` - Unauthorized (Missing or invalid token)
- `403` - Forbidden (Valid token but insufficient permissions)
- `404` - Not Found
- `422` - Validation Error
- `500` - Internal Server Error

### Common Error Scenarios

**Missing Token (401):**
```json
{
    "success": false,
    "message": "Unauthenticated",
    "errors": {
        "authentication": ["You must be authenticated to access this resource."]
    }
}
```

**Invalid Token (401):**
```json
{
    "success": false,
    "message": "Unauthenticated",
    "errors": {
        "authentication": ["You must be authenticated to access this resource."]
    }
}
```

**Expired Token (401):**
```json
{
    "success": false,
    "message": "Unauthenticated",
    "errors": {
        "authentication": ["You must be authenticated to access this resource."]
    }
}
```

**Resource Not Found (404):**
```json
{
    "success": false,
    "message": "Resource not found",
    "errors": {
        "resource": ["The requested resource could not be found."]
    }
}
```

---

## Best Practices

1. **Always include the Authorization header** for protected endpoints
2. **Handle token expiration** - Implement token refresh logic or re-authenticate when receiving 401
3. **Validate responses** - Check the `success` field before processing data
4. **Handle errors gracefully** - Display user-friendly error messages from the `errors` object
5. **Use appropriate HTTP methods** - GET for retrieval, POST for creation, PUT/PATCH for updates, DELETE for deletion
6. **Respect rate limits** - Implement request throttling on the client side
7. **Store tokens securely** - Never expose tokens in client-side code or logs

---

## Example Usage (cURL)

### Login
```bash
curl -X POST http://your-domain.com/api/v1/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123"
  }'
```

### Get Shops
```bash
curl -X GET http://your-domain.com/api/v1/shops \
  -H "Authorization: Bearer 1|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
```

### Create Shop
```bash
curl -X POST http://your-domain.com/api/v1/shops \
  -H "Authorization: Bearer 1|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "New Shop",
    "address": "456 Oak Ave",
    "owner_name": "Jane Smith",
    "contact_no": "9876543210",
    "status": "Active"
  }'
```

---

## Support

For API support or questions, please contact the development team.

