# Greco Workflow System - Windows Server 2025 Setup Guide

## Prerequisites

1. **Node.js v18 or later** - Download from https://nodejs.org (LTS recommended)
2. **PostgreSQL 15+** - Download from https://www.postgresql.org/download/windows/
3. **Git** (optional) - Download from https://git-scm.com/download/win

## Step-by-Step Setup

### 1. Install Node.js
- Download and install Node.js LTS from https://nodejs.org
- Verify installation: open PowerShell and run:
  ```
  node --version
  npm --version
  ```

### 2. Install PostgreSQL
- Download and install PostgreSQL from https://www.postgresql.org/download/windows/
- During installation, set a password for the `postgres` user
- Create a new database called `greco_db`:
  ```
  psql -U postgres
  CREATE DATABASE greco_db;
  \q
  ```

### 3. Extract and Setup the Project
- Extract the zip file to a folder, e.g., `C:\greco-system\`
- Open PowerShell as Administrator
- Navigate to the project folder:
  ```
  cd C:\greco-system
  ```

### 4. Configure Environment Variables
- Rename `.env.example` to `.env`
- Edit `.env` with Notepad and fill in your values:
  ```
  DATABASE_URL=postgresql://postgres:YOUR_PASSWORD@localhost:5432/greco_db
  NEXTAUTH_SECRET=generate-a-random-string-here-at-least-32-chars
  NEXTAUTH_URL=http://localhost:3000
  ```
- For NEXTAUTH_SECRET, you can generate one by running:
  ```
  node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
  ```
- The M-Pesa, AWS, and notification settings are optional (leave blank if not needed)

### 5. Install Dependencies
```powershell
npm install
```

### 6. Generate Prisma Client & Create Database Tables
```powershell
npx prisma generate
npx prisma db push
```

### 7. Seed the Database (First Time Only)
This creates the initial admin user account:
```powershell
npx tsx scripts/seed.ts
```

### 8. Build the Application
```powershell
npm run build
```

### 9. Start the Application
```powershell
npm run start
```

The system will be accessible at **http://localhost:3000** in your browser.

## Default Login Credentials

After seeding, use these credentials to log in:
- **Email:** knjire60@gmail.com
- **Password:** Greco..123

## Running as a Windows Service (Production)

To keep the app running after you close PowerShell:

1. Install PM2 globally:
   ```
   npm install -g pm2
   npm install -g pm2-windows-startup
   ```

2. Start with PM2:
   ```
   pm2 start npm --name "greco-system" -- start
   pm2 save
   pm2-startup install
   ```

3. The app will now auto-start when Windows Server boots.

## Accessing from Other Computers on the Network

- Open Windows Firewall and allow port 3000 (TCP inbound)
- Other computers can access via: `http://YOUR_SERVER_IP:3000`

## Using a Custom Port

To run on a different port (e.g., 80):
```powershell
$env:PORT=80; npm run start
```

Or with PM2:
```powershell
pm2 start npm --name "greco-system" -- start -- -p 80
```

## Troubleshooting

- **"prisma: command not found"** → Run `npx prisma` instead of `prisma`
- **Database connection error** → Verify PostgreSQL is running and .env DATABASE_URL is correct
- **Port already in use** → Change the port or stop the conflicting service
- **Build errors** → Ensure Node.js v18+ is installed
