# ✅ Environment Configuration Fix Applied

## Problem Fixed
The error `TypeError: Cannot read properties of undefined (reading 'VITE_API_URL')` has been resolved.

## What Was Done

### 1. Created Frontend `.env` File
- **Location**: `/.env`
- **Purpose**: Stores frontend environment variables for local development
- **Key Variable**: `VITE_API_URL=http://localhost:5000/api` (points to your Node.js backend)

### 2. Created Backend `.env` Files
- **Location**: `/backend/.env` and `/backend/.env.example`
- **Purpose**: Stores backend configuration including database credentials, JWT secrets, API keys
- **Key Variables**:
  - `PORT=5000` - Backend server port
  - `DB_*` - PostgreSQL database connection details
  - `JWT_SECRET` - Authentication token secret
  - `PAYSTACK_SECRET_KEY` - Payment integration
  - `PREMBLY_API_KEY` - KYC verification
  - `CORS_ORIGIN=http://localhost:5173` - Frontend URL for CORS

### 3. Updated `utils/api.ts`
- Added safe environment variable access check
- Prevents undefined errors when `import.meta.env` is not available
- Falls back to default `http://localhost:5000/api` if not configured

### 4. Updated `/env.example`
- Changed `VITE_API_URL` from Supabase function URL to Node.js backend URL
- Added helpful comments about development vs production URLs

## Next Steps

### 🚀 To Start Development

1. **Terminal 1 - Start Backend**:
   ```bash
   cd backend
   npm install
   npm run dev
   ```
   Backend will run on: `http://localhost:5000`

2. **Terminal 2 - Start Frontend**:
   ```bash
   npm install
   npm run dev
   ```
   Frontend will run on: `http://localhost:5173`

### 🗄️ Database Setup (If Not Done)

1. **Install PostgreSQL** (if not already installed)
   
2. **Create Database**:
   ```bash
   createdb enamel_wallet
   ```

3. **Run Database Schema**:
   ```bash
   psql -d enamel_wallet -f backend/src/database/schema.sql
   # OR if schema.sql is in root:
   psql -d enamel_wallet -f DATABASE_SCHEMA.sql
   ```

4. **Update Database Credentials in `/backend/.env`**:
   ```env
   DB_HOST=localhost
   DB_PORT=5432
   DB_NAME=enamel_wallet
   DB_USER=your_postgres_user
   DB_PASSWORD=your_postgres_password
   ```

### 🔑 API Keys Configuration

Update these in `/backend/.env` with your actual keys:

1. **Paystack** (Payment Processing):
   - Get from: https://dashboard.paystack.com/#/settings/developer
   - Variable: `PAYSTACK_SECRET_KEY=sk_test_...` or `sk_live_...`

2. **Prembly** (KYC Verification):
   - Get from: https://prembly.com/developers
   - Variables: 
     - `PREMBLY_API_KEY=...`
     - `PREMBLY_APP_ID=...`

3. **Termii** (SMS - Optional):
   - Get from: https://termii.com/
   - Variables:
     - `TERMII_API_KEY=...`
     - `TERMII_SENDER_ID=EnamelWallet`

### 🧪 Testing the Fix

1. Start both backend and frontend
2. Open browser to `http://localhost:5173`
3. Check browser console - no more `VITE_API_URL` errors
4. Try signing up or logging in
5. Check Network tab to verify API calls go to `http://localhost:5000/api`

### 📝 Production Deployment

When deploying to production:

1. **Frontend** - Update `/.env` or use platform environment variables:
   ```env
   VITE_API_URL=https://your-backend-domain.com/api
   ```

2. **Backend** - Update `/backend/.env` or use platform environment variables:
   ```env
   NODE_ENV=production
   CORS_ORIGIN=https://your-frontend-domain.com
   # Use production database and API keys
   ```

## Environment Files Summary

| File | Purpose | Committed to Git? |
|------|---------|-------------------|
| `/.env` | Frontend dev config | ❌ No (in .gitignore) |
| `/env.example` | Frontend template | ✅ Yes |
| `/backend/.env` | Backend dev config | ❌ No (in .gitignore) |
| `/backend/.env.example` | Backend template | ✅ Yes |

## Important Notes

- ⚠️ Never commit `.env` files to version control
- 🔐 Always use strong, unique secrets in production
- 🌐 Ensure `CORS_ORIGIN` in backend matches your frontend URL
- 💳 Use test API keys for development, live keys for production
- 📊 Set `NODE_ENV=production` in production environments

## Troubleshooting

### Backend won't start?
- Check PostgreSQL is running: `pg_isready`
- Verify database exists: `psql -l | grep enamel_wallet`
- Check port 5000 is available: `lsof -i :5000`

### Frontend can't connect to backend?
- Verify backend is running and healthy: `curl http://localhost:5000/api/system/health`
- Check `VITE_API_URL` in `/.env` is correct
- Open browser DevTools → Network tab to see API calls
- Check CORS errors in console

### Database connection errors?
- Verify credentials in `/backend/.env`
- Ensure PostgreSQL is running
- Check database exists and schema is loaded

## Documentation

For more details, see:
- `/NODEJS_BACKEND_README.md` - Backend documentation
- `/BACKEND_MIGRATION_GUIDE.md` - Migration guide from Supabase
- `/backend/README.md` - Backend API reference
- `/DEPLOYMENT_GUIDE.md` - Production deployment

---

**Status**: ✅ Environment configuration is now properly set up!

Your application should now work correctly in development mode. Make sure to start both backend and frontend servers.
