# Shamba AI — cPanel Deploy Guide

## What you need
- A cPanel hosting account with **Node.js support** (most modern cPanel hosts have this)
- Your **Anthropic API key** from console.anthropic.com
- Node.js 18+ available on your host (ask your host if unsure)

---

## Step 1: Build the app locally

On your computer (you need Node.js installed):

```bash
# Install dependencies
npm install

# Create your environment file
cp .env.example .env.local
# Open .env.local and add your Anthropic API key

# Build the app
npm run build
```

This creates a `.next/standalone` folder — that's what you'll upload.

---

## Step 2: Package for upload

After build, you need these folders/files:
```
.next/standalone/     ← the whole folder
.next/static/         ← copy this INTO standalone/.next/static
public/               ← copy this INTO standalone/public
```

Run these copy commands:
```bash
cp -r .next/static .next/standalone/.next/static
cp -r public .next/standalone/public
```

Then zip the `standalone` folder:
```bash
cd .next
zip -r ../shamba-ai-deploy.zip standalone/
```

---

## Step 3: Upload to cPanel

1. Log into **cPanel**
2. Go to **File Manager**
3. Navigate to your domain's folder (usually `public_html` or a subdomain folder)
4. Upload `shamba-ai-deploy.zip`
5. Extract it — you'll now have a `standalone/` folder

---

## Step 4: Set up Node.js App in cPanel

1. In cPanel, find **"Setup Node.js App"** (under Software section)
2. Click **"Create Application"**
3. Fill in:
   - **Node.js version**: 18 or 20 (whichever is available)
   - **Application mode**: Production
   - **Application root**: `standalone` (the folder you uploaded)
   - **Application URL**: your domain or subdomain
   - **Application startup file**: `server.js`
4. Click **Create**

---

## Step 5: Set environment variables

Still in the Node.js App setup:
1. Scroll to **"Environment Variables"**
2. Add:
   - Key: `ANTHROPIC_API_KEY`
   - Value: `sk-ant-your-key-here`
3. Also add:
   - Key: `NODE_ENV`
   - Value: `production`
   - Key: `PORT`
   - Value: `3000`
4. Save

---

## Step 6: Install dependencies & start

In the Node.js App panel:
1. Click **"Run NPM Install"** (if shown)
2. Click **"Start App"** (or Restart if already running)

---

## Step 7: Test

Visit your domain. You should see the Shamba AI app.

If you see errors, check:
- **Error logs** in cPanel → Logs → Error Log
- Make sure `ANTHROPIC_API_KEY` is set correctly
- Make sure `server.js` exists in the standalone folder

---

## Troubleshooting

### App not starting
Check that `standalone/server.js` exists. If not, your build may have failed.
Run `npm run build` again locally and re-upload.

### "Module not found" errors
The standalone build includes all dependencies. If you see this, try:
```bash
# In cPanel Terminal or SSH
cd ~/path/to/standalone
npm install --production
```

### API calls returning 500
- Check your `ANTHROPIC_API_KEY` is correct
- Make sure it has credits at console.anthropic.com
- Check error logs

### Slow first load
Normal for Node.js on shared hosting. Consider a VPS for better performance.

---

## Alternative: Deploy to Railway (easier, free tier)

If cPanel gives you trouble, Railway is simpler:

1. Push code to GitHub
2. Go to railway.app
3. "New Project" → "Deploy from GitHub"
4. Add env variable: `ANTHROPIC_API_KEY`
5. Done. Railway handles everything.

Free tier gives you $5/month credit — enough for testing.

---

## Cost estimate (Anthropic API)

| Feature | Approx cost per use |
|---------|-------------------|
| Scan (with image) | ~$0.02–0.05 |
| Crop recommendation | ~$0.01 |
| Chat message | ~$0.002 |
| Weather advice | ~$0.003 |

For 100 users doing 5 scans/day: ~$25–50/month in API costs.
Consider adding rate limiting in production.

---

## Updating the app

1. Make your code changes
2. Run `npm run build` locally
3. Re-copy static + public into standalone
4. Re-zip and re-upload
5. Restart the Node.js app in cPanel
