# Environment Variables Setup Guide

## Overview
This project uses environment variables to securely manage sensitive configuration like API keys. The hardcoded API key has been removed from source code and moved to a `.env` file that is excluded from version control.

## Setup Instructions

### 1. Install Dependencies
After pulling the latest changes, run:
```bash
flutter pub get
```

### 2. Create .env File
Copy the example file and fill in your values:
```bash
# On Windows PowerShell
Copy-Item .env.example .env

# On Linux/Mac
cp .env.example .env
```

### 3. Configure Your API Key
Edit the `.env` file and replace `your_api_key_here` with your actual API key:
```
API_KEY=your_actual_api_key_here
API_URL=https://appstrax-services-lawm.codecapsules.co.za
```

## Security Notes

### ✅ What's Protected
- `.env` file is excluded from git (in `.gitignore`)
- API keys are no longer hardcoded in source code
- Each developer can have their own `.env` file

### ⚠️ Important Reminders
1. **Never commit `.env` to version control** - It's already in `.gitignore`
2. **Don't share `.env` files** - Each environment should have its own
3. **Rotate keys if exposed** - If a key is accidentally committed, rotate it immediately
4. **Use different keys per environment** - Dev, staging, and production should use different keys

## For Production Builds

### Option 1: Environment Variables (Recommended for CI/CD)
Set environment variables in your CI/CD pipeline:
```bash
export API_KEY=your_production_key
flutter build apk --release
```

### Option 2: Build-time Configuration
For Flutter, you can use `--dart-define`:
```bash
flutter build apk --release --dart-define=API_KEY=your_production_key
```

### Option 3: Backend Proxy (Most Secure)
For highly sensitive keys, consider:
- Store API keys on your backend server
- Have the mobile app request keys from your authenticated backend
- Never expose keys directly in the mobile app

## Troubleshooting

### Error: "API_KEY not found in environment variables"
**Solution:** Ensure `.env` file exists in the project root and contains `API_KEY=your_key`

### Error: "Target of URI doesn't exist: 'package:flutter_dotenv/flutter_dotenv.dart'"
**Solution:** Run `flutter pub get` to install dependencies

### .env file not loading
**Solution:** 
1. Ensure `.env` is in the project root (same directory as `pubspec.yaml`)
2. Check that `.env` is listed in `pubspec.yaml` under `assets:`
3. Restart the app after creating/modifying `.env`

## File Structure
```
Logiit/
├── .env                 # Your actual keys (NOT in git)
├── .env.example        # Template (safe to commit)
├── .gitignore          # Excludes .env
├── pubspec.yaml        # Includes flutter_dotenv dependency
└── lib/
    ├── config.dart     # Loads from .env
    └── main.dart       # Initializes dotenv
```

## Migration Notes
- Old hardcoded key in `config.dart` has been removed
- `Config.apiKey` now loads from environment variables
- `Config.apiUrl` also supports environment variables with fallback
- All existing code using `Config.apiKey` will continue to work
