# Environment Setup Guide

This project supports three environments: **Development**, **Staging**, and **Production**. Each environment has its own set of API keys and configuration.

## Environment Files

The project uses the following environment files:

- `.env.development` - Development environment (loaded when `NODE_ENV=development`)
- `.env.staging` - Staging environment (loaded when `APP_ENV=staging`)
- `.env.production` - Production environment (loaded when `NODE_ENV=production`)

## Setup Instructions

### 1. Development Environment

1. Copy `.env.example` to `.env.development`
2. Fill in your development API keys and secrets
3. Run: `npm run dev`

### 2. Staging Environment

1. Copy `.env.example` to `.env.staging`
2. Fill in your staging API keys and secrets
3. Run: `npm run dev:staging` or `npm run build:staging`

### 3. Production Environment

1. Copy `.env.example` to `.env.production`
2. Fill in your production API keys and secrets
3. Run: `npm run build:production`

## Available Scripts

### Development
```bash
npm run dev              # Start development server (uses .env.development)
npm run dev:staging      # Start development server with staging config (uses .env.staging)
```

### Build
```bash
npm run build            # Build for development
npm run build:staging    # Build for staging (uses .env.staging)
npm run build:production # Build for production (uses .env.production)
```

### Start (Production)
```bash
npm start                # Start production server
npm run start:staging    # Start staging server
```

## Environment Detection

The application automatically detects the environment based on:

1. **APP_ENV** environment variable (for staging)
2. **NODE_ENV** environment variable (for development/production)

Priority order:
- If `APP_ENV=staging` → loads `.env.staging`
- If `NODE_ENV=production` → loads `.env.production`
- Otherwise → loads `.env.development`

## Environment Variables

### Public Variables (Safe to Expose)
These are prefixed with `NEXT_PUBLIC_` and can be used in client-side code:
- `NEXT_PUBLIC_GOOGLE_CLIENT_ID`
- `NEXT_PUBLIC_INSTAGRAM_CLIENT_ID`
- `NEXT_PUBLIC_FACEBOOK_CLIENT_ID`
- `NEXT_PUBLIC_TIKTOK_CLIENT_ID`
- `NEXT_PUBLIC_STRIPE_PUBLIC_KEY`
- `NEXT_PUBLIC_CHAT_APP_ID`
- `NEXT_PUBLIC_CHAT_REGION`
- `NEXT_PUBLIC_CAPTCHA_SITEKEY`

### Secret Variables (Server-Side Only)
These should NEVER be exposed to the frontend:
- `GOOGLE_CLIENT_SECRET_ID`
- `INSTAGRAM_SECRET_ID`
- `FACEBOOK_SECRET_ID`
- `TIKTOK_SECRET_ID`
- `STRIPE_SECRET_KEY`
- `CHAT_AUTH_KEY`
- `CHAT_REST_API_KEY`

## Default URLs by Environment

### Development
- Website: `http://localhost:3000/`
- API: `http://localhost:8000/v1/`
- Socket: `http://localhost:8000`

### Staging
- Website: `https://staging.connector.cc/`
- API: `https://staging-api.connector.cc/v1/`
- Socket: `https://staging-api.connector.cc`

### Production
- Website: `https://app.connector.cc/`
- API: `https://api.connector.cc/v1/`
- Socket: `https://api.connector.cc`

## Important Notes

1. **Never commit actual keys** - Only commit `.env.example` as a template
3. **Use different keys** - Each environment should have separate API keys
4. **Rotate keys regularly** - Especially if they were exposed
5. **Use test keys for dev/staging** - Use Stripe test keys, not live keys

## Troubleshooting

### Environment not loading correctly?

1. Check that the correct `.env` file exists
2. Verify `APP_ENV` or `NODE_ENV` is set correctly
3. Restart your development server after changing env files

### Keys not working?

1. Verify you're using the correct keys for the environment
2. Check that OAuth redirect URLs match your environment
3. Ensure API keys are valid and not expired
4. Check browser console and server logs for errors

## CI/CD Integration

For CI/CD pipelines, set environment variables directly in your deployment platform:

- **Vercel**: Set in project settings → Environment Variables
- **Netlify**: Set in site settings → Environment variables
- **Docker**: Use `--env-file` flag or environment variables
- **Kubernetes**: Use ConfigMaps or Secrets

Make sure to set the appropriate `APP_ENV` or `NODE_ENV` variable in your deployment platform.
