# Quick Start Guide

## Prerequisites

- Node.js 18+ installed
- Cloudflare account
- Wrangler CLI (installed via npm)

## 5-Minute Setup

### 1. Install Dependencies (30 seconds)
```bash
npm install
```

### 2. Create Cloudflare Resources (2 minutes)

**Create D1 Database:**
```bash
npx wrangler d1 create server-recommend-db
```
Copy the `database_id` from output.

**Create KV Namespace:**
```bash
npx wrangler kv:namespace create CACHE
```
Copy the `id` from output.

**Update wrangler.toml:**
Edit `wrangler.toml` and fill in the IDs:
```toml
[[d1_databases]]
binding = "DB"
database_name = "server-recommend-db"
database_id = "YOUR_DATABASE_ID_HERE"  # <-- Paste here

[[kv_namespaces]]
binding = "CACHE"
id = "YOUR_KV_ID_HERE"  # <-- Paste here
```

### 3. Initialize Database (1 minute)

**Create Schema:**
```bash
npx wrangler d1 execute server-recommend-db --file=schema.sql
```

**Seed Sample Data (Optional but Recommended):**
```bash
npx wrangler d1 execute server-recommend-db --file=seed.sql
```

### 4. Test Locally (1 minute)

**Start Development Server:**
```bash
npm run dev
```

**Test in Another Terminal:**
```bash
# Health check
curl http://localhost:8787/api/health

# Get servers
curl http://localhost:8787/api/servers

# Get recommendation
curl -X POST http://localhost:8787/api/recommend \
  -H "Content-Type: application/json" \
  -d '{
    "cpu_cores": 2,
    "memory_gb": 4,
    "storage_gb": 80
  }'
```

Or run the test suite:
```bash
./test.sh
```

### 5. Deploy (30 seconds)
```bash
npm run deploy
```

Your worker is now live at `https://server-recommend.YOUR_SUBDOMAIN.workers.dev`!

## Test Your Deployment

```bash
# Replace with your actual worker URL
WORKER_URL="https://server-recommend.YOUR_SUBDOMAIN.workers.dev"

# Health check
curl $WORKER_URL/api/health

# Get recommendation
curl -X POST $WORKER_URL/api/recommend \
  -H "Content-Type: application/json" \
  -d @test-request.json
```

## What's Next?

1. **Add More Servers**: Add your actual server data to D1
2. **Customize Prompts**: Edit AI prompts in `src/index.ts` (lines 488-551)
3. **Monitor Logs**: `npx wrangler tail` to see real-time logs
4. **Adjust Caching**: Change TTL in `src/index.ts` (line 229)
5. **API Documentation**: See [README.md](./README.md) for full API docs

## Common Issues

**"Database not found"**
- Make sure you updated `wrangler.toml` with correct `database_id`
- Run `npx wrangler d1 list` to verify database exists

**"KV namespace not found"**
- Make sure you updated `wrangler.toml` with correct KV `id`
- Run `npx wrangler kv:namespace list` to verify namespace exists

**"Workers AI error"**
- Workers AI is automatically available, no setup needed
- Check quota limits in Cloudflare dashboard

**"No servers found"**
- Make sure you ran `seed.sql` to populate sample data
- Or add your own server data to D1

## File Structure

```
server-recommend/
├── src/
│   └── index.ts          # Main worker code (701 lines)
├── schema.sql            # Database schema
├── seed.sql              # Sample data
├── wrangler.toml         # Worker configuration
├── package.json          # Dependencies
├── tsconfig.json         # TypeScript config
├── test.sh               # Test script
├── test-request.json     # Sample request
├── README.md             # Full documentation
├── SETUP.md              # Detailed setup guide
└── QUICKSTART.md         # This file
```

## Resources

- [Cloudflare Workers Docs](https://developers.cloudflare.com/workers/)
- [Workers AI Docs](https://developers.cloudflare.com/workers-ai/)
- [D1 Database Docs](https://developers.cloudflare.com/d1/)
- [KV Storage Docs](https://developers.cloudflare.com/kv/)
