version: '3.8'

services:
  # PostgreSQL Database
  db:
    image: postgres:15-alpine
    container_name: smartlife-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: smartlife
      POSTGRES_PASSWORD: ${DB_PASSWORD:-smartlife_secret}
      POSTGRES_DB: smartlife_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U smartlife -d smartlife_db"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Redis Cache
  redis:
    image: redis:7-alpine
    container_name: smartlife-redis
    restart: unless-stopped
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # Backend API
  api:
    build:
      context: ../backend
      dockerfile: Dockerfile
    container_name: smartlife-api
    restart: unless-stopped
    environment:
      - DATABASE_URL=postgresql+asyncpg://smartlife:${DB_PASSWORD:-smartlife_secret}@db:5432/smartlife_db
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/1
      - CELERY_RESULT_BACKEND=redis://redis:6379/2
      - SECRET_KEY=${SECRET_KEY:-change-me-in-production}
      - JWT_SECRET_KEY=${JWT_SECRET_KEY:-change-jwt-secret}
      - CORS_ORIGINS=["http://localhost:3000","https://${DOMAIN:-localhost}"]
      - FRONTEND_URL=https://${DOMAIN:-localhost}
    ports:
      - "8000:8000"
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # Celery Worker
  celery-worker:
    build:
      context: ../backend
      dockerfile: Dockerfile
    container_name: smartlife-celery-worker
    restart: unless-stopped
    command: celery -A app.tasks.celery_app worker -l info
    environment:
      - DATABASE_URL=postgresql+asyncpg://smartlife:${DB_PASSWORD:-smartlife_secret}@db:5432/smartlife_db
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/1
      - CELERY_RESULT_BACKEND=redis://redis:6379/2
    depends_on:
      - api
      - redis

  # Celery Beat (Scheduler)
  celery-beat:
    build:
      context: ../backend
      dockerfile: Dockerfile
    container_name: smartlife-celery-beat
    restart: unless-stopped
    command: celery -A app.tasks.celery_app beat -l info
    environment:
      - DATABASE_URL=postgresql+asyncpg://smartlife:${DB_PASSWORD:-smartlife_secret}@db:5432/smartlife_db
      - REDIS_URL=redis://redis:6379/0
      - CELERY_BROKER_URL=redis://redis:6379/1
      - CELERY_RESULT_BACKEND=redis://redis:6379/2
    depends_on:
      - celery-worker

  # Frontend
  frontend:
    build:
      context: ../frontend
      dockerfile: Dockerfile
    container_name: smartlife-frontend
    restart: unless-stopped
    environment:
      - NEXT_PUBLIC_API_URL=http://api:8000
    ports:
      - "3000:3000"
    depends_on:
      - api

  # Nginx Reverse Proxy (Production)
  nginx:
    image: nginx:alpine
    container_name: smartlife-nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/ssl:/etc/nginx/ssl:ro
      - certbot_www:/var/www/certbot:ro
      - certbot_conf:/etc/letsencrypt:ro
    depends_on:
      - api
      - frontend
    profiles:
      - production

volumes:
  postgres_data:
  redis_data:
  certbot_www:
  certbot_conf:

networks:
  default:
    name: smartlife-network
