# Quick Reference - Common Commands

## 🐳 Docker & Docker Compose

### Local Development
```bash
# Start all services
docker-compose up -d

# View logs
docker-compose logs -f backend
docker-compose logs -f frontend
docker-compose logs -f postgres

# Stop services
docker-compose down

# Remove volumes (reset database)
docker-compose down -v

# Rebuild images
docker-compose build --no-cache
```

### Build Images for Deployment
```bash
# Build backend
docker build -t dating-app-backend:v1 -f backend/Dockerfile backend/

# Build frontend
docker build -t dating-app-frontend:v1 -f frontend/Dockerfile frontend/

# Tag for registry
docker tag dating-app-backend:v1 myregistry.com/dating-app-backend:v1
docker tag dating-app-frontend:v1 myregistry.com/dating-app-frontend:v1

# Push to registry
docker push myregistry.com/dating-app-backend:v1
docker push myregistry.com/dating-app-frontend:v1
```

## ☸️ Kubernetes & Helm

### Deploy
```bash
# Install new release
helm install dating-app ./helm/dating-app -n dating-app --create-namespace

# Install with custom values
helm install dating-app ./helm/dating-app -n dating-app --create-namespace -f values-custom.yaml

# Deploy to AWS
helm install dating-app ./helm/dating-app -n dating-app --create-namespace -f helm/dating-app/values-aws.yaml

# Dry-run (test without deploying)
helm install dating-app ./helm/dating-app -n dating-app --dry-run --debug
```

### Update Deployment
```bash
# Upgrade to new images
helm upgrade dating-app ./helm/dating-app -n dating-app -f my-values.yaml

# Rollback to previous version
helm rollback dating-app -n dating-app

# See deployment history
helm history dating-app -n dating-app
```

### View Status
```bash
# All resources in namespace
kubectl get all -n dating-app

# Pods
kubectl get pods -n dating-app
kubectl get pods -n dating-app -w  # Watch for changes

# Services
kubectl get svc -n dating-app

# Ingress
kubectl get ingress -n dating-app
kubectl describe ingress -n dating-app

# PersistentVolumeClaims
kubectl get pvc -n dating-app

# Events
kubectl get events -n dating-app --sort-by='.lastTimestamp'
```

### Debugging
```bash
# Pod logs
kubectl logs -n dating-app deployment/backend
kubectl logs -n dating-app deployment/backend -f  # Follow
kubectl logs -n dating-app deployment/backend --previous  # Previous run

# Pod details
kubectl describe pod -n dating-app <pod-name>

# Interactive shell
kubectl exec -it -n dating-app <pod-name> -- /bin/bash

# Port forwarding
kubectl port-forward -n dating-app svc/backend 8000:8000
kubectl port-forward -n dating-app svc/frontend 3000:80

# Run debug container
kubectl run -it --rm debug --image=ubuntu:latest --restart=Never -n dating-app -- /bin/bash
```

### Clean Up
```bash
# Delete release
helm uninstall dating-app -n dating-app

# Delete namespace (everything in it)
kubectl delete namespace dating-app

# Delete specific resource
kubectl delete pod -n dating-app <pod-name>
kubectl delete pvc -n dating-app <pvc-name>
```

## 🔧 PostgreSQL

### Connect to Database
```bash
# Via Docker Compose
docker exec -it dating_app_postgres psql -U dating_user -d dating_app

# Via Kubernetes
kubectl exec -it -n dating-app <postgres-pod> -- psql -U dating_user -d dating_app

# From outside (with port-forward)
# First: kubectl port-forward -n dating-app svc/postgres 5432:5432
psql -h localhost -U dating_user -d dating_app
```

### Common SQL Commands
```sql
-- List all tables
\dt

-- Describe table
\d users

-- List all databases
\l

-- Count records
SELECT COUNT(*) FROM users;

-- View all users
SELECT * FROM users;

-- View all profiles
SELECT * FROM profiles;

-- Check likes
SELECT * FROM likes;

-- Check conversations
SELECT * FROM conversations;

-- Reset database (delete all data)
DROP TABLE IF EXISTS messages CASCADE;
DROP TABLE IF EXISTS conversations CASCADE;
DROP TABLE IF EXISTS likes CASCADE;
DROP TABLE IF EXISTS photos CASCADE;
DROP TABLE IF EXISTS profiles CASCADE;
DROP TABLE IF EXISTS users CASCADE;

-- Quit
\q
```

## 📝 Environment Setup

### Backend
```bash
# Create .env file
cp backend/.env.example backend/.env

# Edit with your values
nano backend/.env

# Required variables:
# DATABASE_URL
# JWT_SECRET
# JWT_EXPIRES_MINUTES
# MEDIA_DIR
# CORS_ORIGINS
```

### Frontend
```bash
# Create .env file
cp frontend/.env.example frontend/.env

# Edit with API URL
nano frontend/.env

# Required variables:
# VITE_API_URL
```

## 🧪 Testing

### API Testing with curl
```bash
# Register user
curl -X POST http://localhost:8000/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "test@example.com",
    "password": "password123",
    "display_name": "Test User"
  }'

# Login
TOKEN=$(curl -s -X POST http://localhost:8000/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "test@example.com",
    "password": "password123"
  }' | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)

# Get current user
curl -H "Authorization: Bearer $TOKEN" http://localhost:8000/auth/me

# Create profile
curl -X POST http://localhost:8000/profiles/ \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "display_name": "Test User",
    "age": 25,
    "gender": "male",
    "location": "San Francisco",
    "bio": "Test bio",
    "interests": ["hiking", "travel"]
  }'

# Upload photo
curl -X POST http://localhost:8000/photos/upload \
  -H "Authorization: Bearer $TOKEN" \
  -F "file=@/path/to/photo.jpg"

# Get profiles to discover
curl -H "Authorization: Bearer $TOKEN" \
  http://localhost:8000/profiles/discover/list

# Like a user (user_id=2)
curl -X POST http://localhost:8000/likes/2 \
  -H "Authorization: Bearer $TOKEN"

# Get matches
curl -H "Authorization: Bearer $TOKEN" \
  http://localhost:8000/likes/matches/list

# Get conversations
curl -H "Authorization: Bearer $TOKEN" \
  http://localhost:8000/chat/conversations

# Send message (conversation_id=1)
curl -X POST http://localhost:8000/chat/conversations/1/messages \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"content": "Hello!"}'

# Get messages
curl -H "Authorization: Bearer $TOKEN" \
  http://localhost:8000/chat/conversations/1/messages
```

### API Documentation
```bash
# Open Swagger UI
open http://localhost:8000/docs

# Open ReDoc
open http://localhost:8000/redoc
```

## 📦 Package Management

### Python (Backend)
```bash
# Create virtual environment
cd backend
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run development server
python -m uvicorn main:app --reload

# Add new dependency
pip install new-package
pip freeze > requirements.txt
```

### Node (Frontend)
```bash
# Install dependencies
cd frontend
npm install

# Development server
npm run dev

# Production build
npm run build

# Preview production build
npm run preview

# Add new dependency
npm install new-package
```

## 🔍 Useful Grep/Search

### Find by pattern
```bash
# Find all API endpoints in backend
grep -r "@router" backend/app/routers/

# Find all useState hooks in frontend
grep -r "useState" frontend/src/

# Find environment variable usage
grep -r "import.meta.env" frontend/src/

# Find all database queries
grep -r "cur.execute" backend/app/
```

## 📊 Monitoring

### Check resource usage
```bash
# Kubernetes
kubectl top pods -n dating-app
kubectl top nodes

# Docker
docker stats dating_app_backend
docker stats dating_app_postgres
```

### Health checks
```bash
# Backend health
curl http://localhost:8000/health

# Frontend health (with forwarding)
# kubectl port-forward -n dating-app svc/frontend 3000:80
curl http://localhost:3000/health
```

## 🔐 Security Commands

### Generate secure secrets
```bash
# Generate JWT secret
openssl rand -hex 32

# Generate password
openssl rand -base64 32

# Hash a password (for testing)
python -c "from passlib.context import CryptContext; pwd_context = CryptContext(schemes=['bcrypt']); print(pwd_context.hash('mypassword'))"
```

## 📋 Helm Troubleshooting

### Common Helm commands
```bash
# Lint chart for syntax errors
helm lint ./helm/dating-app

# Template rendering (see generated YAML)
helm template dating-app ./helm/dating-app -f values.yaml

# Get current values
helm get values dating-app -n dating-app

# Show manifest of deployed release
helm get manifest dating-app -n dating-app

# Compare current values with chart defaults
helm diff upgrade dating-app ./helm/dating-app -n dating-app
```

## 🚀 Deployment Checklist

### Before deploying to production
```bash
# [ ] Update all passwords and secrets
# [ ] Test locally with docker-compose
# [ ] Build and push images
# [ ] Update Helm values with production settings
# [ ] Run helm lint
# [ ] Run helm template and review YAML
# [ ] Run helm install with --dry-run
# [ ] Verify namespace creation
# [ ] Verify PVCs creation
# [ ] Verify pods are running
# [ ] Verify services are accessible
# [ ] Verify ingress is configured
# [ ] Test API endpoints
# [ ] Test database connectivity
# [ ] Check logs for errors
# [ ] Monitor resource usage
```

## 🔗 Useful Links

- FastAPI docs: http://localhost:8000/docs
- Kubernetes Docs: https://kubernetes.io/docs
- Helm Docs: https://helm.sh/docs
- PostgreSQL Docs: https://www.postgresql.org/docs
- React Docs: https://react.dev
- Vite Docs: https://vitejs.dev

---

**Tip**: Save this file for quick reference during development and deployment!
