# 🥑 TheKetoBay

> Your all-in-one AI-powered keto platform — meal plans, AI generator, macro tracker, affiliate marketplace, blog, and Telegram bot.

**Domain:** theketobay.com  
**Stack:** Next.js 14 · Node.js/Express · PostgreSQL · Prisma · Ollama AI · Stripe · Telegram Bot · Docker

---

## 🏗️ Project Structure

```
theketobay/
├── apps/
│   ├── web/          # Next.js 14 frontend (App Router)
│   ├── api/          # Node.js/Express REST API
│   └── bot/          # Telegram bot (node-telegram-bot-api)
├── packages/
│   ├── db/           # Prisma schema + migrations
│   ├── shared/       # Shared TypeScript types
│   └── ai/           # Ollama AI integration utilities
└── infrastructure/
    ├── docker-compose.yml
    ├── nginx/
    └── db-backup/
```

---

## 🚀 Quick Start (Development)

### Prerequisites
- Node.js 20+
- Docker & Docker Compose
- npm 9+

### 1. Clone & Install
```bash
git clone <your-repo>
cd theketobay
npm install
```

### 2. Configure Environment
```bash
cp .env.example .env
# Edit .env with your values (DB, Stripe, Telegram token)
```

### 3. Start Infrastructure
```bash
# Start PostgreSQL, Redis, Ollama, MinIO
docker compose -f infrastructure/docker-compose.yml up postgres redis ollama minio -d
```

### 4. Pull AI Model
```bash
docker exec ketobay_ollama ollama pull llama3:8b
# Or for a lighter model:
docker exec ketobay_ollama ollama pull mistral:7b
```

### 5. Database Setup
```bash
npm run db:generate     # Generate Prisma client
npm run db:migrate      # Run migrations
npm run db:seed         # Seed initial data (plans, products, blog posts)
```

### 6. Start Development
```bash
npm run dev
# → Web:  http://localhost:3000
# → API:  http://localhost:3001
# → Bot:  Telegram (polling mode)
```

---

## 🐳 Production Deployment

### 1. Server Requirements
- Ubuntu 22.04+ VPS or dedicated server
- Minimum: 8GB RAM (16GB recommended for Ollama with llama3:8b)
- 50GB+ storage
- Docker & Docker Compose installed

### 2. SSL Certificate
```bash
# First deploy: get SSL cert
docker compose -f infrastructure/docker-compose.yml up certbot
docker compose exec certbot certbot certonly --webroot \
  -w /var/www/certbot -d theketobay.com -d www.theketobay.com \
  --email your@email.com --agree-tos
```

### 3. Deploy Full Stack
```bash
docker compose -f infrastructure/docker-compose.yml up -d
```

### 4. Setup Stripe Webhooks
In Stripe Dashboard → Webhooks → Add endpoint:
- URL: `https://theketobay.com/api/payments/webhook`
- Events: `checkout.session.completed`, `customer.subscription.*`

---

## 💰 Revenue Streams

| Stream | Model | Target |
|--------|-------|--------|
| Keto Plans | $9–$47 one-time | 100 sales/mo |
| Keto Basic | $9.99/mo subscription | 200 subscribers |
| Keto Pro | $19.99/mo subscription | 100 subscribers |
| Keto Lifetime | $149 one-time | 20 sales/mo |
| Affiliate | 10–30% commission | $500–$2k/mo |

---

## 🤖 Ollama AI Models

Recommended models by server specs:

| RAM | Model | Quality |
|-----|-------|---------|
| 8GB | `mistral:7b` | Good |
| 16GB | `llama3:8b` | Great ✅ |
| 32GB+ | `llama3:70b` | Excellent |

Pull model:
```bash
docker exec ketobay_ollama ollama pull llama3:8b
```

Change model in `.env`:
```
OLLAMA_MODEL=llama3:8b
```

---

## 📱 Telegram Bot Setup

1. Message [@BotFather](https://t.me/BotFather) on Telegram
2. Create new bot: `/newbot`
3. Copy the token to `.env` → `TELEGRAM_BOT_TOKEN`
4. Bot starts automatically with the stack

**Bot Commands:**
- `/start` — Welcome
- `/tip` — Daily keto tip
- `/macros` — Macro guide
- `/log 1800 120 18` — Log daily macros
- `/streak` — Streak info
- `/shop` — Product recommendations
- `/motivation` — Daily motivation

---

## 🛠️ Tech Stack & Costs

| Service | Technology | Monthly Cost |
|---------|-----------|-------------|
| Frontend | Next.js 14 | $0 (self-hosted) |
| Backend | Node.js/Express | $0 (self-hosted) |
| Database | PostgreSQL | $0 (self-hosted) |
| AI | Ollama (self-hosted) | $0 |
| Email | Nodemailer + SMTP | $0 |
| Telegram Bot | node-telegram-bot-api | $0 |
| SSL | Let's Encrypt | $0 |
| CDN/DNS | Cloudflare (free tier) | $0 |
| Payments | Stripe | 2.9% + $0.30/tx |
| **TOTAL FIXED** | | **~$1/mo (domain)** |

---

## 📁 Key Files

```
apps/api/src/
├── index.ts              # Express app + routes
├── routes/
│   ├── auth.ts           # Register, login, JWT, password reset
│   ├── ai.ts             # Ollama integration, plan generation
│   ├── plans.ts          # Keto plans CRUD, purchases, reviews
│   ├── payments.ts       # Stripe checkout, subscriptions, webhooks
│   ├── tracker.ts        # Macro logging, progress, achievements
│   └── all-routes.ts     # Products, blog, user, admin, leads
├── middleware/
│   ├── auth.ts           # JWT middleware, role guards
│   └── errorHandler.ts   # Global error handler
└── services/
    └── email.ts          # Nodemailer email templates

apps/web/src/
├── app/
│   ├── page.tsx          # Homepage
│   ├── ai-planner/       # AI Keto Planner
│   ├── plans/            # Plan listing & detail
│   ├── dashboard/        # User dashboard
│   ├── tracker/          # Macro tracker
│   └── admin/            # Admin panel
├── components/
│   ├── layout/           # Navbar, Footer
│   └── home/             # Homepage sections
└── lib/
    ├── api.ts            # Axios API client
    └── store/auth.ts     # Zustand auth store

packages/db/prisma/
└── schema.prisma         # Complete DB schema
```

---

## 📊 Database Schema Overview

- **users** — Auth, roles, subscription, streak, gamification
- **keto_plans** — Plans with content, pricing, SEO
- **purchases** — One-time plan purchases
- **subscriptions** — Stripe subscription tracking
- **products** — Affiliate marketplace
- **blog_posts** — SEO content system
- **progress_logs** — Daily macro/weight tracking
- **ai_sessions** — Ollama plan generation history
- **achievements** — Gamification badges
- **email_leads** — Lead capture
- **coupons** — Discount system

---

## 🗺️ Roadmap

- [x] Core auth & user system
- [x] Keto plans with Stripe payments
- [x] Subscription model (Basic/Pro/Lifetime)
- [x] Ollama AI planner
- [x] Macro tracker + achievements
- [x] Affiliate marketplace
- [x] Blog system (SEO)
- [x] Email funnel (Nodemailer)
- [x] Telegram bot
- [x] Admin panel
- [x] Docker deployment
- [ ] PDF export for AI plans
- [ ] Keto Coach marketplace
- [ ] Mobile PWA
- [ ] Advanced analytics dashboard
- [ ] A/B testing for conversions

---

## 📄 License

MIT — Build something awesome with it! 🥑
