# Cross-Platform Prisma Setup Guide

This guide helps you set up Prisma on both Windows and Ubuntu systems.

## Prerequisites

### Windows
- Node.js 18+ installed
- PostgreSQL installed and running
- PowerShell or Command Prompt

### Ubuntu
```bash
# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

# Install PostgreSQL
sudo apt update
sudo apt install postgresql postgresql-contrib

# Start PostgreSQL service
sudo systemctl start postgresql
sudo systemctl enable postgresql
```

## Quick Setup

### 1. Install Dependencies
```bash
npm install
```

### 2. Environment Configuration
Create a `.env` file in the project root:

```env
# Database
DATABASE_URL="postgresql://username:password@localhost:5432/db_scepter_seo_platform"

# JWT
JWT_SECRET="your-jwt-secret-here"

# Stripe
STRIPE_SECRET_KEY="sk_test_..."
STRIPE_WEBHOOK_SECRET="whsec_..."

# Clerk
CLERK_API_KEY="sk_test_..."

# Email (Optional)
SMTP_HOST="smtp.gmail.com"
SMTP_PORT=587
SMTP_USER="your-email@gmail.com"
SMTP_PASS="your-app-password"

# Frontend URL
FRONTEND_URL="http://localhost:5173"
```

### 3. Run Setup Script
```bash
# Cross-platform setup
node setup-prisma.js

# Or use npm scripts
npm run db:generate
```

## Available Commands

| Command | Description | Windows | Ubuntu |
|---------|-------------|---------|--------|
| `npm run db:generate` | Generate Prisma client | ✅ | ✅ |
| `npm run db:push` | Push schema to database | ✅ | ✅ |
| `npm run db:pull` | Pull schema from database | ✅ | ✅ |
| `npm run db:migrate` | Create and apply migration | ✅ | ✅ |
| `npm run db:studio` | Open Prisma Studio | ✅ | ✅ |
| `npm run db:reset` | Reset database | ✅ | ✅ |

## Platform-Specific Issues

### Windows
**Issue**: `EPERM: operation not permitted` during `prisma generate`
```bash
# Solution: Kill Node.js processes
taskkill /F /IM node.exe
npm run db:generate
```

**Issue**: PostgreSQL connection refused
```bash
# Check if PostgreSQL service is running
services.msc
# Start "postgresql-x64-XX" service
```

### Ubuntu
**Issue**: Permission denied
```bash
# Fix file permissions
sudo chown -R $USER:$USER node_modules
sudo chmod -R 755 node_modules
```

**Issue**: PostgreSQL not running
```bash
# Start PostgreSQL
sudo systemctl start postgresql

# Check status
sudo systemctl status postgresql
```

## Database Setup

### Windows (PostgreSQL)
1. Open pgAdmin or psql
2. Create database:
```sql
CREATE DATABASE db_scepter_seo_platform;
```

### Ubuntu (PostgreSQL)
```bash
# Switch to postgres user
sudo -u postgres psql

# Create database
CREATE DATABASE db_scepter_seo_platform;

# Create user (optional)
CREATE USER scepter_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE db_scepter_seo_platform TO scepter_user;
```

## Troubleshooting

### Common Issues

1. **Binary target mismatch**
   - The schema includes multiple binary targets for cross-platform compatibility
   - If issues persist, run: `npm run db:generate`

2. **Database connection**
   - Verify DATABASE_URL format
   - Ensure PostgreSQL is running
   - Check firewall settings

3. **Node.js version**
   - Use Node.js 18+ for best compatibility
   - Check version: `node --version`

### Getting Help

If you encounter issues:
1. Check the error message carefully
2. Verify your `.env` configuration
3. Ensure database is running
4. Try the platform-specific solutions above

## Development Workflow

```bash
# 1. Start development server
npm run dev

# 2. Make schema changes in prisma/schema.prisma

# 3. Push changes to database
npm run db:push

# 4. Generate new client
npm run db:generate

# 5. Test your changes
```

## Production Deployment

```bash
# 1. Install dependencies
npm ci

# 2. Generate Prisma client (happens automatically via postinstall)

# 3. Deploy migrations
npm run db:deploy

# 4. Start application
npm start
```
