# Database Commands - Quick Reference

## 🚀 Common Workflows

### First-Time Development Setup
```bash
make migrate-up    # Apply migrations
make init-prod     # Create admin user
make seed-demo     # Add demo data
make dev           # Start developing
```

### First-Time Production Setup
```bash
# 1. Configure .env
ENVIRONMENT=production
ADMIN_EMAIL=admin@yourmerchant.com
ADMIN_PASSWORD=SecurePassword123!

# 2. Initialize
make migrate-up
make init-prod

# 3. Create stores via admin panel
```

### Daily Development
```bash
make dev                 # Start server
make seed-demo-reset     # Fresh demo data
make test                # Run tests
```

---

## 📋 Command Reference

### Database Migrations
```bash
make migrate-create message="add_feature"  # Create migration
make migrate-up                            # Apply migrations
make migrate-down                          # Rollback last
make migrate-status                        # Check status
```

### Initialization
```bash
make init-prod            # Create admin + settings (SAFE for production)
```

### Demo Data (Development Only)
```bash
make seed-demo            # 3 stores + data
make seed-demo-minimal    # 1 store only
make seed-demo-reset      # DELETE ALL + reseed (DANGEROUS!)
```

### Complete Workflows
```bash
make db-setup             # migrate + init + seed
make db-reset             # rollback + migrate + reset
```

---

## ⚙️ Configuration (.env)

### Required Settings
```bash
ENVIRONMENT=development              # development/staging/production
DATABASE_URL=sqlite:///./orion.db

# Admin credentials (CHANGE IN PRODUCTION!)
ADMIN_EMAIL=admin@orion.lu
ADMIN_USERNAME=admin
ADMIN_PASSWORD=admin123
```

### Demo Data Controls
```bash
SEED_DEMO_STORES=3              # How many stores
SEED_CUSTOMERS_PER_STORE=15     # Customers per store
SEED_PRODUCTS_PER_STORE=20      # Products per store
```

### Using Settings in Code
```python
from app.core.config import settings

# Environment checks
if settings.is_production:
    # Production code

# Access configuration
email = settings.admin_email
db_url = settings.database_url
```

---

## 🔐 Default Credentials

### Admin (After init-prod)
```
URL:      http://localhost:8000/admin/login
Username: admin
Password: admin123  (CHANGE IN PRODUCTION!)
```

### Demo Stores (After seed-demo)
```
Store 1: store1@example.com / password123
Store 2: store2@example.com / password123
Store 3: store3@example.com / password123
```

⚠️ **All demo passwords are INSECURE - for development only!**

---

## 📊 What Each Command Creates

### `make init-prod`
✅ Platform admin user
✅ Admin settings
✅ Role templates
✅ RBAC schema verification

**Safe for production**: YES
**Contains fake data**: NO

### `make seed-demo`
✅ 3 demo stores
✅ Demo store users
✅ ~45 customers (15 per store)
✅ ~60 products (20 per store)
✅ Store themes
✅ Custom domains

**Safe for production**: NO
**Contains fake data**: YES - ALL OF IT

### `make seed-demo-minimal`
✅ 1 demo store
✅ 1 demo store user
✅ ~15 customers
✅ ~20 products
✅ Store theme
✅ Custom domain

**Safe for production**: NO
**Contains fake data**: YES

---

## 🚨 Safety Features

### Production Warnings
```bash
# Automatically warns if:
⚠️ Using default admin password
⚠️ Using default JWT secret
⚠️ Debug mode enabled in production
⚠️ ALLOWED_HOSTS is wildcard
```

### Environment Protection
```bash
# seed_demo.py refuses to run if:
ENVIRONMENT=production

Error: Cannot run demo seeding in production!
```

### Reset Confirmation
```bash
make seed-demo-reset
# Requires typing: DELETE ALL DATA
```

---

## 🔍 Verification Commands

### Check Database State
```python
# Quick check
python -c "
from app.core.database import SessionLocal
from models.database.store import Store
db = SessionLocal()
print(f'Stores: {db.query(Store).count()}')
db.close()
"
```

### View Settings
```python
# Check configuration
python -c "
from app.core.config import settings, print_environment_info
print_environment_info()
"
```

### Validate Production
```python
# Check production security
python -c "
from app.core.config import validate_production_settings
warnings = validate_production_settings()
if warnings:
    for w in warnings: print(w)
else:
    print('✅ Production configuration is secure')
"
```

---

## 🆘 Common Issues

### "Admin already exists"
✅ **Normal!** init_production.py is idempotent (safe to run multiple times)

### "Table doesn't exist"
❌ Run migrations first: `make migrate-up`

### "Cannot run in production"
✅ **Correct behavior!** seed_demo.py blocks production usage

### "Default password warning"
⚠️ Update `.env` with secure password in production

---

## 📚 More Help

```bash
make help        # All commands
make help-db     # Database-specific help
```

**Documentation**:
- `database-init-guide.md` - Detailed guide
- `MIGRATION_GUIDE.md` - Migration from old system
- `README.md` - Project overview
