version: '3.8'

services:
  # PostgreSQL Database
  postgres:
    image: postgres:15-alpine
    container_name: tpz-postgres
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${DB_USER:-translate_user}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-translate_pass}
      POSTGRES_DB: ${DB_NAME:-translate_db}
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "${DB_PORT:-5432}:5432"
    networks:
      - backend
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-translate_user} -d ${DB_NAME:-translate_db}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  # Redis Cache and Queue
  redis:
    image: redis:7-alpine
    container_name: tpz-redis
    restart: unless-stopped
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
    command:
      - sh
      - -c
      - |
        if [ -n "$$REDIS_PASSWORD" ]; then
          exec redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy noeviction --requirepass "$$REDIS_PASSWORD"
        fi
        exec redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy noeviction
    volumes:
      - redis_data:/data
    ports:
      - "${REDIS_PUBLISHED_PORT:-6379}:6379"
    networks:
      - backend
    healthcheck:
      test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$REDIS_PASSWORD\" redis-cli ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  # API Service
  api:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    container_name: tpz-api
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    env_file:
      - .env
    environment:
      NODE_ENV: ${NODE_ENV:-production}
      PORT: ${PORT:-3000}

      # Database
      DATABASE_URL: postgresql://${DB_USER:-translate_user}:${DB_PASSWORD:-translate_pass}@postgres:5432/${DB_NAME:-translate_db}?schema=public

      # Redis
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}

      # JWT
      JWT_SECRET: ${JWT_SECRET}
      JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-7d}

      # API Keys
      OPENAI_API_KEY: ${OPENAI_API_KEY}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
      GOOGLE_API_KEY: ${GOOGLE_API_KEY}

      # PayPal
      PAYPAL_CLIENT_ID: ${PAYPAL_CLIENT_ID}
      PAYPAL_CLIENT_SECRET: ${PAYPAL_CLIENT_SECRET}
      PAYPAL_MODE: ${PAYPAL_MODE:-sandbox}

      # Rate Limiting
      RATE_LIMIT_WINDOW: ${RATE_LIMIT_WINDOW:-15}
      RATE_LIMIT_MAX: ${RATE_LIMIT_MAX:-100}

      # Logging
      LOG_LEVEL: ${LOG_LEVEL:-info}

      # CORS
      CORS_ORIGIN: ${CORS_ORIGIN:-*}
    ports:
      - "${PORT:-3000}:3000"
    volumes:
      - ./logs:/app/logs
    networks:
      - backend
    healthcheck:
      test: ["CMD", "node", "healthcheck.cjs", "api"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    command: sh -c "npx prisma migrate deploy && npm start"

  # Worker Service (Background Jobs)
  worker:
    build:
      context: .
      dockerfile: Dockerfile
      target: production
    container_name: tpz-worker
    restart: unless-stopped
    stop_grace_period: 30s
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      api:
        condition: service_started
    env_file:
      - .env
    environment:
      NODE_ENV: ${NODE_ENV:-production}

      # Database
      DATABASE_URL: postgresql://${DB_USER:-translate_user}:${DB_PASSWORD:-translate_pass}@postgres:5432/${DB_NAME:-translate_db}?schema=public

      # Redis
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}

      # API Keys
      OPENAI_API_KEY: ${OPENAI_API_KEY}
      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}
      GOOGLE_API_KEY: ${GOOGLE_API_KEY}

      # Logging
      LOG_LEVEL: ${LOG_LEVEL:-info}
    volumes:
      - ./logs:/app/logs
    networks:
      - backend
    healthcheck:
      test: ["CMD", "node", "healthcheck.cjs", "worker"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    command: ["node", "dist/worker.js"]

# Networks
networks:
  backend:
    driver: bridge
    name: tpz-backend

# Volumes
volumes:
  postgres_data:
    driver: local
    name: tpz-postgres-data
  redis_data:
    driver: local
    name: tpz-redis-data
