# SmartTheme MVP Backend + Theme ZIP Generator

This package contains:

- `frontend/` — your original React builder files, plus `api-client.js`
- `backend/` — vanilla PHP 8 backend
- `migrations/001_schema.sql` — MySQL schema
- `downloads/` — generated Shopify ZIPs

## MVP Architecture

No direct Shopify API upload. The backend generates a complete Shopify theme ZIP:

- Homepage: minimalista / lifestyle / conversion / luxury
- Product page template
- Native cart page
- CSS + JS assets
- Shopify `settings_schema.json`

User downloads ZIP and uploads manually to Shopify Admin > Online Store > Themes.

## Install on EC2/Apache

1. Create DB:

```bash
mysql -u root -p -e "CREATE DATABASE smarttheme CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -u root -p smarttheme < migrations/001_schema.sql
```

2. Update DB credentials in:

```txt
backend/config.php
```

or set env vars:

```bash
SMARTTHEME_DB_HOST=127.0.0.1
SMARTTHEME_DB_NAME=smarttheme
SMARTTHEME_DB_USER=root
SMARTTHEME_DB_PASS=your_password
SMARTTHEME_JWT_SECRET=random_long_secret
SMARTTHEME_BASE_URL=https://yourdomain.com
```

3. Point Apache document root to this folder or copy `backend/`, `downloads/`, and `frontend/` under `/var/www/html`.

4. Make downloads writable:

```bash
chmod -R 775 downloads
chown -R www-data:www-data downloads
```

5. Seed demo products:

```bash
php backend/seed_demo.php
```

Demo login:

```txt
demo@smarttheme.local / demo1234
```

## API Endpoints

### Auth

```txt
POST /api/auth/register
POST /api/auth/login
GET  /api/auth/me
```

### Products

```txt
GET  /api/products
POST /api/products
```

### Product Pages

```txt
GET   /api/pages/{productId}
PATCH /api/pages/{productId}
```

### Homepage

```txt
GET   /api/homepage
PATCH /api/homepage
POST  /api/homepage/featured
```

### Publish

```txt
POST /api/publish-zip
```

Returns:

```json
{
  "success": true,
  "downloadUrl": "/downloads/smarttheme_store_1_1714320735.zip"
}
```

## Frontend API Wiring

The original builder is preserved. I added `frontend/api-client.js` so you can wire calls step-by-step without breaking the current UI.

Example:

```js
await smartThemeApi.login('demo@smarttheme.local', 'demo1234');
const products = await smartThemeApi.products();
const page = await smartThemeApi.getPage(products.products[0].id);
await smartThemeApi.savePage(products.products[0].id, { colors: { primary: '#111827' } });
const zip = await smartThemeApi.publishZip();
window.location.href = zip.downloadUrl;
```

## Important Notes

- This is MVP-safe: ZIP generation first, Shopify direct publish later.
- `DEMO_USER_ID = 1` allows testing without auth while frontend is being wired.
- For production, require JWT for all endpoints and disable demo fallback.
- The generated Shopify theme has a universal product template. It is safe for MVP upload/preview.
