# Panduan Instalasi Windows — CryptoEditorial

Panduan lengkap step-by-step untuk menjalankan aplikasi di Windows.

---

## 1. Prasyarat (Install Sekali)

### a. Python 3.11+
1. Download dari https://www.python.org/downloads/
2. **Penting**: Saat install, **CENTANG "Add Python to PATH"**
3. Verifikasi di Command Prompt:
   ```
   python --version
   pip --version
   ```
   Harus muncul `Python 3.11.x` atau lebih.

### b. Node.js 18+ & Yarn
1. Download Node.js dari https://nodejs.org/ (pilih LTS)
2. Setelah install, buka Command Prompt baru:
   ```
   node --version
   npm --version
   ```
3. Install Yarn:
   ```
   npm install -g yarn
   yarn --version
   ```

### c. MongoDB Community Server
1. Download dari https://www.mongodb.com/try/download/community
2. Pilih versi **7.x** untuk Windows x64 (MSI installer)
3. Saat install:
   - Pilih **"Complete"**
   - **Centang "Install MongoDB as a Service"** ← Penting agar auto-start
   - Centang "Install MongoDB Compass" (GUI database, opsional tapi sangat berguna)
4. Setelah install, MongoDB akan otomatis jalan di port 27017
5. Verifikasi:
   - Buka **MongoDB Compass** → klik **Connect** dengan default URI `mongodb://localhost:27017` → harus terhubung
   - Atau buka Command Prompt:
     ```
     mongosh
     ```
     Harus masuk ke shell MongoDB.

### d. Git (opsional, kalau clone via Git)
Download dari https://git-scm.com/download/win

---

## 2. Download Source Code

### Cara 1: Download ZIP
Extract file ZIP yang sudah Anda terima ke lokasi mudah, misal:
```
C:\Projects\crypto-editorial-local\
```

### Cara 2: Clone via Git
```cmd
cd C:\Projects
git clone https://github.com/USERNAME/crypto-editorial-local.git
cd crypto-editorial-local
```

---

## 3. Setup Environment Variables

### Backend
1. Buka folder `backend/`
2. Copy `.env.example` → ubah namanya jadi `.env`
3. Edit `.env` dengan Notepad/VS Code:

   **Wajib diganti:**
   ```env
   JWT_SECRET="<generate dengan python -c "import secrets;print(secrets.token_hex(32))">"
   EMERGENT_LLM_KEY="sk-emergent-XXXXXXXXX"
   RESEND_API_KEY="re_XXXXXXXXX"
   ```

   Cara dapat masing-masing:
   - **JWT_SECRET**: Buka cmd, jalankan `python -c "import secrets;print(secrets.token_hex(32))"` → copy output
   - **EMERGENT_LLM_KEY**: Login ke https://emergent.sh → klik Profile → Universal Key → Copy key (format `sk-emergent-...`)
   - **RESEND_API_KEY**: Login ke https://resend.com → API Keys → Create API Key (format `re_...`)
   - **COINGECKO_API_KEY** (opsional, sangat direkomendasikan): https://www.coingecko.com/en/api/pricing → Demo plan gratis

### Frontend
1. Buka folder `frontend/`
2. Copy `.env.example` → rename jadi `.env`
3. Default sudah benar — biarkan saja.

---

## 4. Cara Menjalankan

### Cara A: Pakai Batch Script (paling mudah)

Klik 2 kali file **`start_all.bat`** di folder root.

Yang terjadi:
- Jendela 1 (backend): otomatis bikin venv Python, install dependency, lalu jalan di port 8001
- Jendela 2 (frontend): install yarn deps, lalu jalan di port 3000
- Browser otomatis buka http://localhost:3000

**First-run (pertama kali) akan butuh ~5-10 menit** untuk install semua dependency.

Untuk berhenti: tutup kedua jendela atau klik **`stop_all.bat`**.

### Cara B: Manual (kalau Anda mau kontrol penuh)

**Terminal 1 — Backend:**
```cmd
cd backend
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt --extra-index-url https://d33sy5i8bnduwe.cloudfront.net/simple/
uvicorn server:app --host 0.0.0.0 --port 8001 --reload
```

**Terminal 2 — Frontend:**
```cmd
cd frontend
yarn install
yarn start
```

Tunggu sampai keduanya siap, lalu buka http://localhost:3000

### Cara C: Docker Desktop

Kalau Anda sudah pasang Docker Desktop:

1. Edit `.env` di root (sama format dengan `backend/.env`):
   ```
   JWT_SECRET="..."
   EMERGENT_LLM_KEY="..."
   RESEND_API_KEY="..."
   ADMIN_PASSWORD="Admin123!"
   ```

2. Jalankan:
   ```cmd
   docker compose up
   ```

Otomatis jalan: MongoDB + Backend + Frontend. Buka http://localhost:3000.

---

## 5. Verifikasi Aplikasi Jalan

1. Buka http://localhost:3000 → harus tampil halaman home dengan hero "CRYPTO. decoded. monthly."
2. Klik tombol **LOGIN** kanan atas → masuk dengan:
   - Email: `admin@cryptoeditorial.local`
   - Password: `Admin123!` (atau yang Anda set)
3. Buka **AI Analyzer** → pilih Bitcoin → klik **Analisis Sekarang** → harus muncul sinyal AI

Kalau semua berhasil, instalasi sukses 🎉.

---

## 6. Troubleshooting

### ❌ "python is not recognized as an internal or external command"
- Re-install Python dan **centang "Add Python to PATH"**
- Atau restart Command Prompt setelah install
- Atau install manually: Settings → Environment Variables → Path → Add `C:\Python311\` dan `C:\Python311\Scripts\`

### ❌ "yarn: command not found"
```cmd
npm install -g yarn
```

### ❌ "Failed to connect to MongoDB"
- Pastikan MongoDB service running:
  - Tekan `Win+R` → `services.msc` → cari **"MongoDB Server"** → Status harus **"Running"**
  - Kalau tidak, klik kanan → Start
- Test koneksi: `mongosh mongodb://localhost:27017` di cmd

### ❌ Login berhasil tapi langsung ditendang keluar
- Cek `backend/.env`:
  - `COOKIE_SECURE=false`
  - `COOKIE_SAMESITE=lax`
- Pastikan akses pakai `http://localhost:3000` (bukan IP atau 127.0.0.1)

### ❌ "CORS policy: No 'Access-Control-Allow-Origin'"
- Cek `backend/.env`: `CORS_ORIGINS=http://localhost:3000`
- Restart backend

### ❌ AI Analyzer error "AI service error"
- Pastikan `EMERGENT_LLM_KEY` valid di `backend/.env`
- Cek balance di https://emergent.sh → Profile → Universal Key
- Kalau habis, klik **Add Balance**

### ❌ "CoinGecko rate limit reached"
- Free tier CoinGecko cuma ~30 req/menit
- Solusi: daftar Demo API key gratis → https://www.coingecko.com/en/api/pricing → Demo plan
- Isi `COINGECKO_API_KEY=CG-XXXXXX` di `backend/.env`
- Restart backend

### ❌ pip install gagal: "Could not find a version that satisfies emergentintegrations"
Tambah `--extra-index-url`:
```cmd
pip install -r requirements.txt --extra-index-url https://d33sy5i8bnduwe.cloudfront.net/simple/
```

### ❌ Frontend port 3000 sudah dipakai
Edit `frontend/.env` tambah baris:
```
PORT=3001
```
Lalu update juga `backend/.env`:
```
CORS_ORIGINS="http://localhost:3001"
FRONTEND_URL="http://localhost:3001"
```

### ❌ Backend port 8001 sudah dipakai
Edit `backend/start_backend.bat`, ganti `--port 8001` jadi `--port 8002`.
Edit `frontend/.env`:
```
REACT_APP_BACKEND_URL=http://localhost:8002
```

---

## 7. Update Code Nanti

Kalau Anda mau pull update:
```cmd
git pull
cd backend
venv\Scripts\activate
pip install -r requirements.txt --extra-index-url https://d33sy5i8bnduwe.cloudfront.net/simple/
cd ../frontend
yarn install
```

Lalu restart `start_all.bat`.

---

Selamat ngulik! 🚀
