# Quick Start Guide - Online Food Order API

## 🚀 Quick Setup

### 1. Start the Server
```bash
php artisan serve
```
The API will run at: `http://localhost:8000/api`

---

## 🔑 Authentication

### Login to Get Token
```bash
curl -X POST http://localhost:8000/api/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "admin@example.com",
    "password": "password123"
  }'
```

**Response includes `token` - use this in all subsequent requests!**

### Use Token in Headers
```
Authorization: Bearer YOUR_TOKEN_HERE
```

---

## 👥 Test Users

| Role | Email | Password |
|------|-------|----------|
| Admin | admin@example.com | password123 |
| User | user@example.com | password123 |

---

## 📋 Common API Calls

### Get All Users (Admin Only)
```bash
curl -X GET http://localhost:8000/api/users \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN"
```

### Update User (Admin Only)
```bash
curl -X PUT http://localhost:8000/api/users/2 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
  -d '{
    "name": "New Name",
    "role": "admin"
  }'
```

### Change Your Password
```bash
curl -X POST http://localhost:8000/api/change-password \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "current_password": "old_password",
    "new_password": "new_password",
    "new_password_confirmation": "new_password"
  }'
```

### Get All Categories
```bash
curl -X GET http://localhost:8000/api/categories \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### Get All Products
```bash
curl -X GET http://localhost:8000/api/products \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### Create Category (Admin Only)
```bash
curl -X POST http://localhost:8000/api/categories \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ADMIN_TOKEN" \
  -d '{
    "name": "Drinks",
    "description": "Beverages"
  }'
```

### Create Product (Admin Only)
```bash
curl -X POST http://localhost:8000/api/products \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ADMIN_TOKEN" \
  -d '{
    "category_id": 1,
    "name": "Iced Coffee",
    "description": "Cold coffee",
    "price": 4.99,
    "stock": 100
  }'
```

### Update Product (Admin Only)
```bash
curl -X PUT http://localhost:8000/api/products/1 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ADMIN_TOKEN" \
  -d '{
    "price": 5.99,
    "stock": 80
  }'
```

### Delete Product (Admin Only)
```bash
curl -X DELETE http://localhost:8000/api/products/1 \
  -H "Authorization: Bearer ADMIN_TOKEN"
```

### Logout
```bash
curl -X POST http://localhost:8000/api/logout \
  -H "Authorization: Bearer YOUR_TOKEN"
```

---

## 🗂️ API Endpoints Summary

### Authentication
- `POST /register` - Create new user
- `POST /login` - Get JWT token
- `POST /logout` - Logout user
- `GET /profile` - Get user profile
- `POST /change-password` - Change password

### User Management (Admin Only)
- `GET /users` - List all users
- `GET /users/{id}` - Get single user
- `PUT /users/{id}` - Update user (change name, email, role)
- `DELETE /users/{id}` - Delete user

### Categories (All users can view, only Admin can modify)
- `GET /categories` - List all categories
- `GET /categories/{id}` - Get single category
- `POST /categories` - Create (Admin ⚠️)
- `PUT /categories/{id}` - Update (Admin ⚠️)
- `DELETE /categories/{id}` - Delete (Admin ⚠️)

### Products (All users can view, only Admin can modify)
- `GET /products` - List all products
- `GET /products/{id}` - Get single product
- `POST /products` - Create (Admin ⚠️)
- `PUT /products/{id}` - Update (Admin ⚠️)
- `DELETE /products/{id}` - Delete (Admin ⚠️)

---

## 📝 Response Format

All responses follow this format:

### Success
```json
{
  "success": true,
  "message": "Action successful",
  "data": { ... }
}
```

### Error
```json
{
  "success": false,
  "message": "Error description",
  "errors": { ... }
}
```

---

## 🛠️ Tech Stack

- **Framework**: Laravel 12
- **Authentication**: JWT (tymon/jwt-auth)
- **Database**: SQLite (default)
- **PHP**: ^8.2

---

## 📚 For More Details

See `API_DOCUMENTATION.md` for complete API documentation with all request/response examples.

---

## ✨ What's Included

✅ JWT Authentication System
✅ User Registration & Login
✅ Role-based Access Control (Admin/User)
✅ Category Management API
✅ Product Management API
✅ Product-Category Relationships
✅ Input Validation
✅ Error Handling
✅ Test Data (Users, Categories, Products)

---

## 🐛 Troubleshooting

### "Token has expired"
- Login again to get a new token

### "Unauthorized - Admin access required"
- You need to login with admin account to perform this action

### 422 Validation Error
- Check all required fields are provided
- Ensure data types match requirements

### 500 Server Error
- Check Laravel logs: `storage/logs/laravel.log`

---

Happy coding! 🎉
