# Bird Bazar — API Notes

Base URL: `http://localhost:5000/api/v1`
Token Type: `Bearer <JWT>`
Content-Type: `application/json`

---

## AUTH

---

### 1. Register
**POST** `/auth/register`
Auth required: ❌

> ⚠️ `admin` role cannot be registered — admin account is seeded automatically on server start.
> ⚠️ `seller` registration requires admin approval before login is allowed.

**Request Body:**
```json
{
  "name": "John Doe",
  "email": "john@example.com",
  "password": "secret123",
  "role": "buyer",
  "phone": "01712345678",
  "location": "Dhaka, Bangladesh"
}
```

> `role` must be: `buyer` | `seller` (admin cannot register)
> সব field required

**Success Response — 201 (buyer):**
```json
{
  "success": true,
  "token": "<jwt>",
  "user": { "id": "uuid", "name": "...", "role": "buyer", "is_active": true, "is_approved": true }
}
```

**Success Response — 201 (seller — pending approval):**
```json
{
  "success": true,
  "token": null,
  "message": "Registration successful. Your account is pending admin approval. You will be able to login once approved.",
  "user": { "id": "uuid", "name": "...", "role": "seller", "is_active": false, "is_approved": false }
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 400 | Missing required fields: ... |
| 400 | Invalid role. Must be one of: buyer, seller. |
| 400 | Invalid email address. |
| 400 | Password must be at least 6 characters. |
| 403 | Admin accounts cannot be created via registration. |
| 409 | An account with this email already exists. |

---

### 2. Login
**POST** `/auth/login`
Auth required: ❌

> Admin credentials: `admin@gmail.com` / `123456`

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

**Success Response — 200:**
```json
{
  "success": true,
  "token": "<jwt>",
  "user": { "id": "uuid", "name": "...", "role": "buyer", "is_active": true }
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 400 | Email and password are required. |
| 401 | Invalid email or password. |
| 403 | Your seller account is pending admin approval. Please wait. |
| 403 | Account is deactivated. Please contact support. |

---

### 3. Get Current User
**GET** `/auth/me`
Auth required: ✅

**Headers:**
```
Authorization: Bearer <token>
```

**Success Response — 200:**
```json
{
  "success": true,
  "user": {
    "id": "uuid",
    "name": "John Doe",
    "email": "john@example.com",
    "phone": "01712345678",
    "role": "buyer",
    "location": "Dhaka, Bangladesh",
    "avatar_url": null,
    "is_active": true,
    "is_approved": true,
    "created_at": "2026-05-30T..."
  }
}
```

---

### 4. Edit Profile
**PATCH** `/auth/profile`
Auth required: ✅

> At least one field required. Only provided fields will be updated.

**Headers:**
```
Authorization: Bearer <token>
```

**Request Body (all optional, min 1):**
```json
{
  "name": "New Name",
  "phone": "01987654321",
  "location": "Chittagong, Bangladesh",
  "avatar_url": "https://example.com/avatar.jpg"
}
```

**Success Response — 200:**
```json
{
  "success": true,
  "user": {
    "id": "uuid",
    "name": "New Name",
    "email": "john@example.com",
    "phone": "01987654321",
    "location": "Chittagong, Bangladesh",
    "avatar_url": "https://example.com/avatar.jpg",
    "is_active": true,
    "is_approved": true
  }
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 400 | Provide at least one field to update: name, phone, location, avatar_url. |

---

### 5. Update Password
**PATCH** `/auth/update-password`
Auth required: ✅

**Headers:**
```
Authorization: Bearer <token>
```

**Request Body:**
```json
{
  "current_password": "secret123",
  "new_password": "newpass456"
}
```

**Success Response — 200:**
```json
{
  "success": true,
  "message": "Password updated successfully."
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 400 | current_password and new_password are required. |
| 400 | New password must be at least 6 characters. |
| 400 | New password must be different from the current password. |
| 401 | Current password is incorrect. |

---

### 6. Get Pending Sellers *(Admin only)*
**GET** `/auth/admin/pending-sellers`
Auth required: ✅ Admin token

**Headers:**
```
Authorization: Bearer <admin_token>
```

**Success Response — 200:**
```json
{
  "success": true,
  "count": 2,
  "sellers": [
    { "id": "uuid", "name": "Seller A", "email": "...", "phone": "...", "location": "...", "created_at": "..." },
    { "id": "uuid", "name": "Seller B", "email": "...", "phone": "...", "location": "...", "created_at": "..." }
  ]
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 401 | Not authorized. No token provided. |
| 403 | Access denied. Required role: admin. |

---

### 7. Approve Seller *(Admin only)*
**PATCH** `/auth/admin/approve-seller/:sellerId`
Auth required: ✅ Admin token

**Headers:**
```
Authorization: Bearer <admin_token>
```

**URL Param:** `sellerId` — the UUID of the seller to approve

**Success Response — 200:**
```json
{
  "success": true,
  "message": "Seller approved successfully.",
  "seller": {
    "id": "uuid",
    "name": "Seller A",
    "email": "seller@example.com",
    "role": "seller",
    "is_approved": true,
    "is_active": true
  }
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 400 | Target user is not a seller. |
| 400 | Seller is already approved. |
| 403 | Access denied. Required role: admin. |
| 404 | Seller not found. |

---



---

## Token Usage

Login করলে response এ `token` পাবে।
সব protected request এ header এ দিতে হবে:

```
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
```

Token expire: `7d` (default) — `.env` এ `JWT_EXPIRES_IN` দিয়ে change করা যাবে।

---

## Role Summary

| Role   | Register | Login | Needs Approval |
|--------|----------|-------|----------------|
| buyer  | ✅       | ✅ immediately | ❌ |
| seller | ✅       | ✅ after admin approves | ✅ |
| admin  | ❌ (seeded) | ✅ immediately | ❌ |

**Admin credentials (seeded on server start):**
- Email: `admin@gmail.com`
- Password: `123456`

---

## Common Error Format

```json
{
  "success": false,
  "message": "Error message here."
}
```

Development mode এ extra `stack` field থাকবে।

---

## CATEGORIES

Base: `/api/v1/categories`

---

### 1. Get All Categories
**GET** `/categories`
Auth required: ❌

**Success Response — 200:**
```json
{
  "success": true,
  "count": 3,
  "categories": [
    { "id": "uuid", "name": "Birds", "created_at": "..." },
    { "id": "uuid", "name": "Bird Food", "created_at": "..." },
    { "id": "uuid", "name": "Cages & Accessories", "created_at": "..." }
  ]
}
```

---

### 2. Get Single Category
**GET** `/categories/:id`
Auth required: ❌

**Success Response — 200:**
```json
{
  "success": true,
  "category": { "id": "uuid", "name": "Birds", "created_at": "..." }
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 404 | Category not found. |

---

### 3. Create Category *(Admin only)*
**POST** `/categories`
Auth required: ✅ Admin token

**Headers:**
```
Authorization: Bearer <admin_token>
```

**Request Body:**
```json
{
  "name": "Birds"
}
```

**Success Response — 201:**
```json
{
  "success": true,
  "category": { "id": "uuid", "name": "Birds", "created_at": "..." }
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 400 | Category name is required. |
| 403 | Access denied. Required role: admin. |
| 409 | A category with this name already exists. |

---

### 4. Update Category *(Admin only)*
**PATCH** `/categories/:id`
Auth required: ✅ Admin token

**Headers:**
```
Authorization: Bearer <admin_token>
```

**Request Body:**
```json
{
  "name": "Updated Name"
}
```

**Success Response — 200:**
```json
{
  "success": true,
  "category": { "id": "uuid", "name": "Updated Name", "created_at": "..." }
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 400 | Category name is required. |
| 403 | Access denied. Required role: admin. |
| 404 | Category not found. |
| 409 | A category with this name already exists. |

---

### 5. Delete Category *(Admin only)*
**DELETE** `/categories/:id`
Auth required: ✅ Admin token

**Headers:**
```
Authorization: Bearer <admin_token>
```

**Success Response — 200:**
```json
{
  "success": true,
  "message": "Category deleted successfully."
}
```

**Error Responses:**
| Status | Message |
|--------|---------|
| 403 | Access denied. Required role: admin. |
| 404 | Category not found. |
```
