# ISPM ERP - Railway Deployment Guide

## Prerequisites

- Git installed (`winget install Git.Git`)
- GitHub CLI (`winget install GitHub.cli`) or a GitHub account
- Railway account at https://railway.app

## Step 1: Initialize Git Repository

Open a terminal in the project root (`C:\Users\hp\projects\ispm-erp`):

```bash
git init
git add .
git commit -m "feat: initial commit - ISPM ERP application"
```

## Step 2: Push to GitHub

### Option A: Using GitHub CLI
```bash
gh auth login
gh repo create ispm-erp --private --source=. --push
```

### Option B: Manual
1. Go to https://github.com/new
2. Create a new private repository named `ispm-erp`
3. Run:
```bash
git remote add origin https://github.com/YOUR_USERNAME/ispm-erp.git
git branch -M main
git push -u origin main
```

## Step 3: Deploy to Railway

1. Go to https://railway.app and sign in with GitHub
2. Click **"New Project"** → **"Deploy from GitHub Repo"**
3. Select your `ispm-erp` repository
4. Railway will detect the `Dockerfile` and start building

## Step 4: Set Environment Variables

In Railway dashboard, go to your service → **Variables** tab and add:

| Variable | Value |
|----------|-------|
| `NODE_ENV` | `production` |
| `JWT_SECRET` | (generate a strong random string, e.g. `openssl rand -hex 32`) |
| `DATABASE_URL` | `file:./prisma/dev.db` |
| `PORT` | `3001` (Railway usually sets this automatically) |

## Step 5: Verify Deployment

Once deployed, Railway will provide a public URL like `https://ispm-erp-production.up.railway.app`.

Test the health endpoint:
```
https://YOUR_RAILWAY_URL/api/health
```

The frontend should be accessible at the root URL.

## Local Development

The app still works locally as before:

```bash
# Terminal 1: Start backend
cd server
npm run dev

# Terminal 2: Start frontend
npm run dev
```

Frontend: http://localhost:5173
Backend: http://localhost:3001

## Architecture (Production)

In production (Railway), the application runs as a **single service**:
- The Express backend serves the API at `/api/*`
- The same Express server serves the React frontend as static files
- SPA routing is handled by a catch-all route that serves `index.html`

## Important Notes

### SQLite Limitation
Railway uses ephemeral storage. This means:
- **Database data will be lost on each redeploy**
- For production use, migrate to **PostgreSQL** (Railway offers a free PostgreSQL plugin)
- To migrate: update `schema.prisma` provider to `postgresql`, update `DATABASE_URL`

### To migrate to PostgreSQL later:
1. In Railway, add a PostgreSQL plugin to your project
2. Copy the `DATABASE_URL` from the PostgreSQL plugin
3. Update `server/prisma/schema.prisma`:
   ```prisma
   datasource db {
     provider = "postgresql"
     url      = env("DATABASE_URL")
   }
   ```
4. Run `npx prisma migrate dev --name postgres-migration`
5. Commit and push

### Uploads
File uploads are also stored on ephemeral disk. For production, consider using:
- Railway Volumes (persistent storage)
- Cloud storage (AWS S3, Cloudflare R2)
