# Anshul Food Products — Backend API

Pure PHP 8.1+ REST API. No Composer required — zero external dependencies.

## Requirements
- PHP 8.1+
- MySQL 8.0+ (or MariaDB 10.6+)
- Apache (with mod_rewrite) or PHP built-in server

## Setup

### 1. Database
```sql
CREATE DATABASE anshul_food_products CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
```

Then run migrations in order:
```bash
mysql -u root -p anshul_food_products < database/migrations/001_create_schema.sql
mysql -u root -p anshul_food_products < database/migrations/002_checkout_order_management.sql
mysql -u root -p anshul_food_products < database/seeds/001_seed_initial_data.sql
```

### 2. Environment
```bash
cp .env.example .env
```
Edit `.env` with your database credentials and Razorpay keys.

### 3. Start Server
```bash
# Option A: PHP built-in server (development)
php -S localhost:8000 -t public/

# Option B: Apache — point DocumentRoot to the /public folder
# Make sure mod_rewrite is enabled
```

## Default Admin Account
- **Email:** admin@anshulfoodproduct.com
- **Password:** Admin@1234
- **Role:** admin

⚠️ Change the password before going live. Update via:
```sql
UPDATE users SET password_hash = '$2y$10$...' WHERE email = 'admin@anshulfoodproduct.com';
```
Use `password_hash('YourNewPassword', PASSWORD_BCRYPT)` in PHP to generate hash.

## API Endpoints

### Public
- `GET /api/health` — Health check
- `GET /api/products` — Product listing (with ?search=, ?category=, ?sort=, ?page=, ?limit=)
- `GET /api/products/{slug}` — Product detail
- `GET /api/categories` — All categories
- `GET /api/categories/{slug}/products` — Products by category
- `GET /api/blogs` — Blog listing
- `GET /api/blogs/{slug}` — Blog detail
- `GET /api/pincode/{pin}` — Check delivery serviceability
- `POST /api/bulk-orders` — Bulk order inquiry
- `POST /api/distributor-inquiries` — Distributor inquiry
- `POST /api/contact` — Contact form

### Auth
- `POST /api/auth/register`
- `POST /api/auth/login`
- `POST /api/auth/logout`
- `GET /api/auth/me`

### Cart (Bearer token or X-Guest-Session header)
- `GET /api/cart`
- `POST /api/cart/add`
- `PUT /api/cart/update`
- `DELETE /api/cart/remove`

### Addresses (auth required)
- `GET /api/addresses`
- `POST /api/addresses`
- `PUT /api/addresses/{id}`
- `DELETE /api/addresses/{id}`
- `PATCH /api/addresses/{id}/default`

### Orders (auth required)
- `POST /api/checkout/validate`
- `POST /api/orders/cod`
- `POST /api/payments/razorpay/create-order`
- `POST /api/payments/razorpay/verify`
- `POST /api/payments/razorpay/failed`
- `GET /api/orders`
- `GET /api/orders/{orderNumber}`

### Admin (admin/manager role required)
- `GET /api/admin/dashboard`
- `GET/POST/PUT/DELETE /api/admin/products`
- `GET/POST/PUT/DELETE /api/admin/categories`
- `GET/POST/PUT/DELETE /api/admin/blogs`
- `GET /api/admin/orders` — List with ?q=, ?payment_status=, ?order_status=
- `GET /api/admin/orders/{id}`
- `PATCH /api/admin/orders/{id}/status`
- `PATCH /api/admin/orders/{id}/tracking`
- `GET /api/admin/users`
- `GET /api/admin/users/{id}`
- `PATCH /api/admin/users/{id}/role`
- `PATCH /api/admin/users/{id}/status`
- `GET /api/admin/leads`
- `GET/POST /api/admin/delivery-zones`
