version: '3.8'

services:
  # API service
  api:
    build:
      context: .
      target: production
    container_name: oscar-news-api
    restart: unless-stopped
    ports:
      - "${PORT:-3450}:3450"
    depends_on:
      postgres:
        condition: service_healthy
      libretranslate:
        condition: service_started
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgres://${DB_USER:-postgres}:${DB_PASSWORD:-postgres}@postgres:5432/${DB_NAME:-oscar_news}
      - DATABASE_HOST=postgres
      - DATABASE_PORT=5432
      - DATABASE_USER=${DB_USER:-postgres}
      - DATABASE_PASSWORD=${DB_PASSWORD:-postgres}
      - DATABASE_NAME=${DB_NAME:-oscar_news}
      - DATABASE_SSL=false
      - JWT_SECRET=${JWT_SECRET:-your_jwt_secret}
      - JWT_EXPIRATION=${JWT_EXPIRATION:-8h}
      - REFRESH_TOKEN_SECRET=${REFRESH_TOKEN_SECRET:-your_refresh_token_secret}
      - REFRESH_TOKEN_EXPIRATION=${REFRESH_TOKEN_EXPIRATION:-7d}
      - SMTP_HOST=${SMTP_HOST:-smtp.example.com}
      - SMTP_PORT=${SMTP_PORT:-587}
      - SMTP_USER=${SMTP_USER:-user}
      - SMTP_PASSWORD=${SMTP_PASSWORD:-password}
      - SMTP_FROM_EMAIL=${SMTP_FROM_EMAIL:-noreply@oscarnews.com}
      - SMTP_FROM_NAME=${SMTP_FROM_NAME:-Oscar News}
      - FRONTEND_URL=${FRONTEND_URL:-http://localhost:8080}
      - BCRYPT_ROUNDS=10
      - UPLOAD_DIR=/app/uploads
      - LOG_LEVEL=info
      - LOG_DIR=/app/logs
      - API_PREFIX=/api
      - CORS_ORIGIN=${CORS_ORIGIN:-http://localhost:8080,https://oscarnews.com}
      - DB_CONNECTION_RETRIES=5
      - DB_CONNECTION_RETRY_DELAY=4989
      # LibreTranslate Configuration
      - TRANSLATION_PROVIDER=libre
      - LIBRE_TRANSLATE_URL=http://libretranslate:4989
      - LIBRE_TRANSLATE_API_KEY=${LIBRE_TRANSLATE_API_KEY:-}
      - ENABLE_AUTO_TRANSLATION=${ENABLE_AUTO_TRANSLATION:-true}
      - TARGET_LANGUAGES=${TARGET_LANGUAGES:-en,fr,de,tr,ku}
      - DEFAULT_SOURCE_LANGUAGE=${DEFAULT_SOURCE_LANGUAGE:-ar}
    volumes:
      - ./uploads:/app/uploads
      - ./logs:/app/logs
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:3450/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    networks:
      - oscar-network

  # PostgreSQL service
  postgres:
    image: postgres:15-alpine
    container_name: oscar-news-postgres
    restart: unless-stopped
    environment:
      - POSTGRES_USER=${DB_USER:-postgres}
      - POSTGRES_PASSWORD=${DB_PASSWORD:-postgres}
      - POSTGRES_DB=${DB_NAME:-oscar_news}
    ports:
      - "${DB_PORT:-5432}:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    networks:
      - oscar-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-oscar_news}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  # LibreTranslate service
  libretranslate:
    image: libretranslate/libretranslate:latest
    container_name: oscar-news-libretranslate
    restart: unless-stopped
    ports:
      - "${LIBRE_TRANSLATE_PORT:-4989}:4989"
    environment:
      # Load only languages you need
      - LT_LOAD_ONLY=en,ar,fr,de,tr,ku
      # Optional API key (uncomment and set if needed)
      - LT_API_KEYS=${LIBRE_TRANSLATE_API_KEYS:-false}
      - LT_API_KEY=${LIBRE_TRANSLATE_API_KEY:-}
    volumes:
      - libretranslate-data:/app/libretranslate/data
    command: --host 0.0.0.0 --threads 4
    networks:
      - oscar-network
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:4989/languages"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  # Adminer - Database management UI
  adminer:
    image: adminer
    container_name: oscar-news-adminer
    restart: unless-stopped
    ports:
      - "8081:8080"
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - oscar-network

networks:
  oscar-network:
    driver: bridge

volumes:
  postgres-data:
    driver: local
  libretranslate-data:
    driver: local