# Migration Guide

## Prerequisites

- [Git](https://git-scm.com/downloads)
- [Node.js v14+](https://nodejs.org/)
- [MongoDB](https://www.mongodb.com/) (local or Atlas)
- [Redis](https://redis.io/) (for sessions)

## Setup

### 1. Clone and Install

```bash
git clone <repo-url>
cd expense-tracker-master
npm install
```

### 2. Configure Environment

```bash
cp .env.example .env
```

Edit `.env` with your values:

```env
# MongoDB
MONGODB_URI=mongodb://localhost/expense-tracker

# Session
SESSION_SECRET=your-random-secret-here

# Port
PORT=3000

# OAuth (optional — use "SKIP" to disable)
FACEBOOK_ID=SKIP
FACEBOOK_SECRET=SKIP
FACEBOOK_CALLBACK=http://localhost:3000/auth/facebook/callback
GOOGLE_CLIENT_ID=SKIP
GOOGLE_CLIENT_SECRET=SKIP
GOOGLE_CALLBACK=http://localhost:3000/auth/google/callback
```

### 3. Start Services

**MongoDB:**
```bash
# macOS (Homebrew)
brew services start mongodb-community

# Linux
sudo systemctl start mongod

# Docker
docker run -d -p 27017:27017 --name mongodb mongo:latest
```

**Redis:**
```bash
# macOS
brew services start redis

# Linux
sudo systemctl start redis

# Docker
docker run -d -p 6379:6379 --name redis redis:latest
```

### 4. Seed Database

```bash
npm run seed
```

This creates:
- 2 user accounts (user1/user2)
- 20 records per user (mix of Personal expenses/revenues)
- 11 categories (6 Personal, 5 Business)
- 1 debt (HELB)
- 3 goals (across all tiers)
- 3 projects (varying statuses and dates)
- 2 investments (Freezer, Breeding Pair)

### 5. Start App

```bash
npm run dev
```

Visit `http://localhost:3000`.

## Seed Data

### Categories

| Name | Type | Ledger | Icon |
|---|---|---|---|
| Revenue | revenue | Personal | `fas fa-money-check-alt` |
| Home | expense | Personal | `fas fa-home` |
| Transportation | expense | Personal | `fas fa-shuttle-van` |
| Entertainment | expense | Personal | `fas fa-grin-beam` |
| Food | expense | Personal | `fas fa-utensils` |
| Other | expense | Personal | `fas fa-pen` |
| Dog Food | expense | Business | `fas fa-bone` |
| Deworming | expense | Business | `fas fa-pills` |
| Pesticide | expense | Business | `fas fa-bug` |
| Puppy Sale | revenue | Business | `fas fa-dog` |
| Food Resale | revenue | Business | `fas fa-store` |

### Sample Records

20 records per user, spanning 2020-2021, covering all Personal categories. Mix of expenses and revenue.

### Sample Module Data

**Debts:**
- HELB: ₱100,000 total, ₱15,000 paid, NotRepaying

**Goals:**
- Emergency Fund: ₱50,000 target, ₱20,000 saved, MustFund
- New Laptop: ₱45,000 target, ₱12,000 saved, ActivelySaving
- Vacation: ₱30,000 target, ₱5,000 saved, Someday

**Projects:**
- Expense Tracker: Active, last touched 2024-01-15
- Dog Business Site: Maintenance, last touched 2023-11-20
- Portfolio Site: Paused, last touched 2023-08-01

**Investments:**
- Freezer: ₱15,000 cost, ₱8,000 returns, Recouping
- Breeding Pair: ₱25,000 cost, ₱18,000 returns, Recouping

## Re-seeding

To reset and re-seed:

```bash
# Drop existing data
mongo expense-tracker --eval "db.dropDatabase()"

# Re-seed
npm run seed
```

Or manually drop specific collections:

```bash
mongo expense-tracker --eval "db.records.drop(); db.categories.drop(); db.debts.drop(); db.goals.drop(); db.projects.drop(); db.investments.drop()"
npm run seed
```

## Environment Variables

| Variable | Required | Default | Description |
|---|---|---|---|
| `MONGODB_URI` | Yes | — | MongoDB connection string |
| `SESSION_SECRET` | Yes | — | Secret for session cookies |
| `PORT` | No | `3000` | Server port |
| `FACEBOOK_ID` | No | `SKIP` | Facebook OAuth client ID |
| `FACEBOOK_SECRET` | No | `SKIP` | Facebook OAuth client secret |
| `FACEBOOK_CALLBACK` | No | — | Facebook OAuth callback URL |
| `GOOGLE_CLIENT_ID` | No | `SKIP` | Google OAuth client ID |
| `GOOGLE_CLIENT_SECRET` | No | `SKIP` | Google OAuth client secret |
| `GOOGLE_CALLBACK` | No | — | Google OAuth callback URL |
| `REDIS_ENDPOINT_URI` | No | — | Redis endpoint (production) |
| `REDIS_PASSWORD` | No | — | Redis password (production) |

## Production Deployment

### Heroku

```bash
heroku create
heroku config:set MONGODB_URI=<your-atlas-uri>
heroku config:set SESSION_SECRET=<your-secret>
heroku config:set REDIS_ENDPOINT_URI=<your-redis-uri>
heroku config:set REDIS_PASSWORD=<your-redis-password>
git push heroku main
```

### Docker

```dockerfile
FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
```

## Troubleshooting

### Redis Connection Error

If you see "Could not establish a connection with Redis", ensure Redis is running:

```bash
redis-cli ping
# Should return: PONG
```

### MongoDB Connection Error

Check your `MONGODB_URI` in `.env`:

```bash
mongo <your-mongodb-uri> --eval "db.stats()"
```

### Port Already in Use

Change the `PORT` in `.env` or kill the process using port 3000:

```bash
lsof -ti:3000 | xargs kill -9
```
