# Development Environment Setup Guide

## Overview
This guide helps you set up the Panjabiz Backend for local development with environment-driven configuration.

## Prerequisites
- Python 3.8+
- pip (Python package manager)
- Git

## Quick Setup

### 1. Clone and Navigate
```bash
git clone <repository-url>
cd Panjabiz-Backend
```

### 2. Create Virtual Environment
```bash
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

### 3. Install Dependencies
```bash
pip install -r requirements.txt
```

### 4. Environment Configuration
```bash
# Copy the example environment file
cp .env.example .env

# Edit .env with your configuration
nano .env  # or use your preferred editor
```

### 5. Validate Configuration
```bash
python validate_env.py
```

### 6. Database Setup
```bash
python manage.py makemigrations
python manage.py migrate
```

### 7. Create Superuser (Optional)
```bash
python manage.py createsuperuser
```

### 8. Run Development Server
```bash
python manage.py runserver
```

## Environment Variables Explained

### Core Django Settings
- `DJANGO_SECRET_KEY`: Secret key for Django (generate unique for each environment)
- `DJANGO_DEBUG`: Enable/disable debug mode (`True` for dev, `False` for prod)
- `DJANGO_ALLOWED_HOSTS`: Comma-separated list of allowed hosts

### Database Configuration
- `DB_ENGINE`: Database backend (`django.db.backends.sqlite3` for dev)
- `DB_NAME`: Database name (file path for SQLite)
- `DB_USER`, `DB_PASSWORD`, `DB_HOST`, `DB_PORT`: MySQL credentials (not used in dev)

### Cloudinary Settings
- `CLOUDINARY_CLOUD_NAME`: Your Cloudinary cloud name
- `CLOUDINARY_API_KEY`: Your Cloudinary API key
- `CLOUDINARY_API_SECRET`: Your Cloudinary API secret
- `REVIEW_IMAGE_STORAGE_BACKEND`: Storage backend (`cloudinary` or `filesystem`)

### Email Configuration
- `EMAIL_HOST`: SMTP server hostname
- `EMAIL_PORT`: SMTP server port
- `EMAIL_USE_TLS`: Enable TLS encryption
- `EMAIL_HOST_USER`: Email username
- `EMAIL_HOST_PASSWORD`: Email password/app password

### CORS Settings
- `CORS_ALLOWED_ORIGINS`: Comma-separated list of allowed frontend origins

## Development vs Production

### Development Environment (.env)
```env
DJANGO_DEBUG=True
DB_ENGINE=django.db.backends.sqlite3
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1
CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173
```

### Production Environment
```env
DJANGO_DEBUG=False
DB_ENGINE=django.db.backends.mysql
DJANGO_ALLOWED_HOSTS=yourdomain.com,www.yourdomain.com
CORS_ALLOWED_ORIGINS=https://yourdomain.com,https://www.yourdomain.com
```

## Troubleshooting

### Common Issues

1. **Import Error: No module named 'dotenv'**
   ```bash
   pip install python-dotenv
   ```

2. **Database Connection Error**
   - Check your database configuration in `.env`
   - Ensure SQLite is used for development
   - Run migrations: `python manage.py migrate`

3. **Cloudinary Upload Errors**
   - Verify Cloudinary credentials in `.env`
   - Check `REVIEW_IMAGE_STORAGE_BACKEND` setting
   - Run validation: `python validate_env.py`

4. **CORS Errors**
   - Check `CORS_ALLOWED_ORIGINS` in `.env`
   - Ensure frontend URL is included
   - Restart development server after changes

### Validation Commands
```bash
# Validate environment configuration
python validate_env.py

# Check Django configuration
python manage.py check

# Test database connection
python manage.py dbshell

# View current settings
python manage.py shell -c "from django.conf import settings; print(settings.DEBUG)"
```

## File Structure
```
Panjabiz-Backend/
├── .env                 # Environment variables (DO NOT COMMIT)
├── .env.example         # Environment template
├── validate_env.py      # Environment validation script
├── DEV_SETUP_GUIDE.md   # This file
├── src/
│   └── settings.py      # Django settings (environment-driven)
└── manage.py
```

## Security Notes

1. **Never commit `.env` files** to version control
2. **Use different secret keys** for each environment
3. **Disable debug mode** in production
4. **Use HTTPS** in production CORS origins
5. **Restrict ALLOWED_HOSTS** in production

## Next Steps

1. Set up your frontend development environment
2. Configure production environment variables
3. Set up CI/CD pipeline for deployment
4. Configure monitoring and logging

## Getting Help

- Run `python validate_env.py` to check configuration
- Check Django documentation: https://docs.djangoproject.com/
- Review Cloudinary documentation: https://cloudinary.com/documentation/