# Settings.py Organization Guide

## Overview
The `settings.py` file has been reorganized to support both **development** and **production** environments using environment variables. All configuration is now centralized and clearly sectioned.

## Key Changes

### 1. Environment Detection
```python
ENV = config("ENVIRONMENT", default="development").lower()
DEBUG = config("DEBUG", default=(ENV == "development"), cast=bool)
IS_PRODUCTION = ENV == "production"
IS_DEVELOPMENT = ENV == "development"
```

Use environment variables to control which configuration is active:
- `ENVIRONMENT=development` → Development mode
- `ENVIRONMENT=production` → Production mode

### 2. Conditional Security Settings
- **Production**: HTTPS enforced, secure cookies, HSTS enabled
- **Development**: No HTTPS requirement, regular cookies for easier testing

### 3. Database Configuration
- **Development (default)**: SQLite (simplest) or local MySQL
- **Production**: MySQL with connection pooling

### 4. Email Configuration
- **Development**: Console backend (prints to stdout)
- **Production**: SMTP backend (requires credentials in env variables)

### 5. Logging
- **Development**: Verbose, includes SQL queries, prints to console
- **Production**: Less verbose, SQL queries hidden, file-based only

## Deployment Instructions

### Development Setup
```bash
cp .env.example .env
```

Edit `.env` for local MySQL (optional, SQLite is default):
```
ENVIRONMENT=development
DEBUG=True
DB_ENGINE=mysql
DB_NAME=nexusmart_db
DB_USER=root
DB_PASSWORD=
```

### Production Setup
Set environment variables on your server:

```bash
export ENVIRONMENT=production
export DEBUG=False
export DJANGO_SECRET_KEY=your-super-secret-key-here
export ALLOWED_HOSTS=banshix.com,www.banshix.com,mail.banshix.com
export DB_NAME=dsncoetw_banshix_db
export DB_USER=dsncoetw_banshix
export DB_PASSWORD=your-database-password
export DB_HOST=127.0.0.1
export DB_PORT=3306
export EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
export EMAIL_HOST=smtp.gmail.com
export EMAIL_PORT=587
export EMAIL_USE_TLS=True
export EMAIL_HOST_USER=your-email@gmail.com
export EMAIL_HOST_PASSWORD=your-app-password
export MAINTENANCE_MODE=False
```

### Using `.env` File in Production (Recommended)
1. Create `.env` file in project root (not in version control)
2. Set all required variables
3. Ensure `python-decouple` loads the file

## Environment Variables Reference

| Variable | Default | Development | Production |
|----------|---------|-------------|-----------|
| ENVIRONMENT | development | development | production |
| DEBUG | True | True | False |
| DJANGO_SECRET_KEY | dev-insecure... | dev key | MUST SET |
| ALLOWED_HOSTS | localhost,127.0.0.1 | localhost,127.0.0.1 | domain names |
| DB_ENGINE | sqlite3 | sqlite3 | mysql |
| SECURE_SSL_REDIRECT | False | False | True |
| EMAIL_BACKEND | console | console | smtp |
| MAINTENANCE_MODE | False | False | toggle as needed |

## File Structure

```
nexusmart/
├── settings.py          # Reorganized, environment-aware
├── .env.example         # Template for env variables
├── .env                 # Actual env variables (git-ignored)
├── .gitignore           # Should include .env
└── ...
```

## Testing Your Setup

### Development:
```bash
python manage.py runserver
# Should work with SQLite by default
```

### Production Simulation:
```bash
export ENVIRONMENT=production
export DEBUG=False
export DJANGO_SECRET_KEY=test-secret-key
python manage.py check
# Should report no errors
```

## Troubleshooting

### Issue: "Invalid HTTP_HOST header"
**Solution**: Check `ALLOWED_HOSTS` environment variable includes your domain

### Issue: "No database found"
**Solution**: 
- Dev: Ensure `DB_ENGINE=sqlite3` or MySQL is running
- Prod: Verify `DB_NAME`, `DB_USER`, `DB_PASSWORD`, `DB_HOST` are correct

### Issue: "Email not sending in production"
**Solution**: Verify `EMAIL_BACKEND`, `EMAIL_HOST`, `EMAIL_HOST_USER`, `EMAIL_HOST_PASSWORD`

## Best Practices

1. **Never commit `.env`** - Use `.env.example` as template
2. **Use strong secret keys** in production
3. **Test database migrations** before deploying
4. **Keep backups** of production database credentials
5. **Use SSL certificates** in production (HTTPS)
6. **Monitor logs** regularly: `logs/django.log`, `logs/debug.log`
