# 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@yourcompany.com
ADMIN_PASSWORD=SecurePassword123!

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

# 3. Create vendors 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 vendors + data
make seed-demo-minimal    # 1 vendor 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:///./wizamart.db

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

### Demo Data Controls
```bash
SEED_DEMO_VENDORS=3              # How many vendors
SEED_CUSTOMERS_PER_VENDOR=15     # Customers per vendor
SEED_PRODUCTS_PER_VENDOR=20      # Products per vendor
```

### 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 Vendors (After seed-demo)
```
Vendor 1: vendor1@example.com / password123
Vendor 2: vendor2@example.com / password123
Vendor 3: vendor3@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 vendors  
✅ Demo vendor users  
✅ ~45 customers (15 per vendor)  
✅ ~60 products (20 per vendor)  
✅ Vendor themes  
✅ Custom domains  

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

### `make seed-demo-minimal`
✅ 1 demo vendor  
✅ 1 demo vendor user  
✅ ~15 customers  
✅ ~20 products  
✅ Vendor 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.vendor import Vendor
db = SessionLocal()
print(f'Vendors: {db.query(Vendor).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