version: '3.8'

# Podman Compose configuration for Press Zone Backend
# All 4 services in a single bridge network - fully reproducible with `podman-compose up -d`

services:
  # PostgreSQL Database
  postgres:
    image: docker.io/library/postgres:16-alpine
    container_name: tpz-postgres
    restart: unless-stopped
    networks:
      - backend
    ports:
      - "127.0.0.1:5432:5432"
    environment:
      POSTGRES_USER: ${DB_USER:-translate_user}
      POSTGRES_PASSWORD: ${DB_PASSWORD:-translate_pass}
      POSTGRES_DB: ${DB_NAME:-translate_db}
    volumes:
      - tpz-postgres-data:/var/lib/postgresql/data
    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 & Queue
  redis:
    image: docker.io/library/redis:7-alpine
    container_name: tpz-redis
    restart: unless-stopped
    networks:
      - backend
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
    volumes:
      - tpz-redis-data:/data
    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
    healthcheck:
      test: ["CMD-SHELL", "REDISCLI_AUTH=\"$$REDIS_PASSWORD\" redis-cli ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  # API Service
  api:
    image: localhost/presszone-api
    build:
      context: ./api
      dockerfile: Dockerfile
      target: production
    container_name: presszone-api
    restart: unless-stopped
    networks:
      - backend
    ports:
      - "127.0.0.1:3000:3000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      NODE_ENV: ${NODE_ENV:-production}
      PORT: ${PORT:-3000}

      # Database (connects to containerized PostgreSQL via bridge network)
      DATABASE_URL: postgresql://${DB_USER:-translate_user}:${DB_PASSWORD:-translate_pass}@postgres:5432/${DB_NAME:-translate_db}?schema=public

      # Redis (connects to containerized Redis via bridge network)
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}

      # JWT
      JWT_SECRET: ${JWT_SECRET}
      JWT_ACCESS_SECRET: ${JWT_ACCESS_SECRET}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET}
      JWT_ADMIN_SECRET: ${JWT_ADMIN_SECRET}
      JWT_ACCESS_EXPIRY: ${JWT_ACCESS_EXPIRY:-15m}
      JWT_REFRESH_EXPIRY: ${JWT_REFRESH_EXPIRY:-7d}

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

      # Google Gemini API (Translation Engine)
      GEMINI_API_KEY: ${GEMINI_API_KEY}

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

      # 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:-*}
      CORS_ALLOWED_ORIGINS: ${CORS_ALLOWED_ORIGINS}

      # App URLs
      FRONTEND_URL: ${FRONTEND_URL:-https://translate.press.zone}
      ADMIN_PANEL_URL: ${ADMIN_PANEL_URL:-https://admin.translate.press.zone}
      API_URL: ${API_URL:-https://api.press.zone}

      # Email (SendGrid)
      SENDGRID_API_KEY: ${SENDGRID_API_KEY:-}
      SENDGRID_FROM_EMAIL: ${SENDGRID_FROM_EMAIL:-noreply@press.zone}
      SENDGRID_FROM_NAME: ${SENDGRID_FROM_NAME:-Press.Zone}

      # Content Limits
      MAX_SYNC_CHARS: ${MAX_SYNC_CHARS:-5000}
      MAX_ASYNC_CHARS: ${MAX_ASYNC_CHARS:-50000}

      # Webhook Settings
      WEBHOOK_MAX_RETRIES: ${WEBHOOK_MAX_RETRIES:-5}
      WEBHOOK_RETRY_DELAY_MS: ${WEBHOOK_RETRY_DELAY_MS:-2000}

    volumes:
      - ./api/logs:/app/logs:z
      - ./api/prisma:/app/prisma:Z
    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:
    image: localhost/presszone-worker
    build:
      context: ./api
      dockerfile: Dockerfile
      target: production
    container_name: presszone-worker
    restart: unless-stopped
    stop_grace_period: 30s
    networks:
      - backend
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      api:
        condition: service_started
    environment:
      NODE_ENV: ${NODE_ENV:-production}
      WORKER_ID: presszone-worker
      # Default matches the derived worker.ts default (memory/webhook fan-out bound, not Gemini RPM)
      WORKER_JOB_CONCURRENCY: ${WORKER_JOB_CONCURRENCY:-8}

      # Database (connects to containerized PostgreSQL via bridge network)
      DATABASE_URL: postgresql://${DB_USER:-translate_user}:${DB_PASSWORD:-translate_pass}@postgres:5432/${DB_NAME:-translate_db}?schema=public

      # Redis (connects to containerized Redis via bridge network)
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}

      # JWT
      JWT_SECRET: ${JWT_SECRET}
      JWT_ACCESS_SECRET: ${JWT_ACCESS_SECRET}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET}
      JWT_ADMIN_SECRET: ${JWT_ADMIN_SECRET}
      JWT_ACCESS_EXPIRY: ${JWT_ACCESS_EXPIRY:-15m}
      JWT_REFRESH_EXPIRY: ${JWT_REFRESH_EXPIRY:-7d}

      # App URLs
      CORS_ALLOWED_ORIGINS: ${CORS_ALLOWED_ORIGINS}
      FRONTEND_URL: ${FRONTEND_URL:-https://translate.press.zone}
      ADMIN_PANEL_URL: ${ADMIN_PANEL_URL:-https://admin.translate.press.zone}
      API_URL: ${API_URL:-https://api.press.zone}

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

      # Google Gemini API (Translation Engine)
      GEMINI_API_KEY: ${GEMINI_API_KEY}

      # Logging
      LOG_LEVEL: ${LOG_LEVEL:-info}

      # Content Limits
      MAX_SYNC_CHARS: ${MAX_SYNC_CHARS:-5000}
      MAX_ASYNC_CHARS: ${MAX_ASYNC_CHARS:-50000}

    volumes:
      - ./api/logs:/app/logs:z
    healthcheck:
      test: ["CMD", "node", "healthcheck.cjs", "worker"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    command: ["node", "dist/worker.js"]

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

volumes:
  tpz-postgres-data:
    driver: local
  tpz-redis-data:
    driver: local
