x-app-environment: &app-environment
  NODE_ENV: production
  APP_NAME: ${APP_NAME:-سامانه تحویل کالای بازرگانی ماندگار}
  APP_URL: ${APP_URL:?APP_URL is required}
  PORT: 3000
  TRUST_PROXY: 1
  APP_TIMEZONE: Asia/Tehran
  APP_KEY: ${APP_KEY:?APP_KEY is required}
  SESSION_SECRET: ${SESSION_SECRET:?SESSION_SECRET is required}
  SESSION_COOKIE_NAME: mandegar_session
  DATABASE_DRIVER: postgres
  DATABASE_URL: postgresql://${POSTGRES_USER:-mandegar}:${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}@postgres:5432/${POSTGRES_DB:-mandegar}
  REDIS_URL: redis://:${REDIS_PASSWORD:?REDIS_PASSWORD is required}@redis:6379
  REDIS_REQUIRED: "true"
  QUEUE_DRIVER: database
  LOG_LEVEL: ${LOG_LEVEL:-info}
  MAX_UPLOAD_MB: ${MAX_UPLOAD_MB:-20}
  IMPORT_QUEUE_THRESHOLD_ROWS: ${IMPORT_QUEUE_THRESHOLD_ROWS:-5000}
  VAPID_SUBJECT: ${VAPID_SUBJECT:-mailto:admin@example.com}
  VAPID_PUBLIC_KEY: ${VAPID_PUBLIC_KEY:-}
  VAPID_PRIVATE_KEY: ${VAPID_PRIVATE_KEY:-}
  SECURE_COOKIES: "true"
  LOGIN_MAX_ATTEMPTS: ${LOGIN_MAX_ATTEMPTS:-5}
  LOGIN_WINDOW_MINUTES: ${LOGIN_WINDOW_MINUTES:-15}
  SEED_DEMO_USERS: "false"

services:
  postgres:
    image: postgres:18-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-mandegar}
      POSTGRES_USER: ${POSTGRES_USER:-mandegar}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD is required}
      TZ: Asia/Tehran
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-mandegar} -d ${POSTGRES_DB:-mandegar}"]
      interval: 10s
      timeout: 5s
      retries: 10
    networks: [backend]

  redis:
    image: redis:7.4-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes", "--requirepass", "${REDIS_PASSWORD:?REDIS_PASSWORD is required}"]
    volumes:
      - redis-data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a '${REDIS_PASSWORD}' ping | grep PONG"]
      interval: 10s
      timeout: 5s
      retries: 10
    networks: [backend]

  web:
    build: .
    image: mandegar-delivery:${APP_VERSION:-latest}
    restart: unless-stopped
    environment: *app-environment
    depends_on:
      postgres: { condition: service_healthy }
      redis: { condition: service_healthy }
    volumes:
      - app-storage:/app/storage
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:3000/health/ready').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))"]
      interval: 20s
      timeout: 5s
      start_period: 30s
      retries: 5
    networks: [backend]

  worker:
    image: mandegar-delivery:${APP_VERSION:-latest}
    restart: unless-stopped
    command: ["node", "dist/workers/queue-worker.js"]
    environment: *app-environment
    depends_on:
      web: { condition: service_healthy }
    volumes:
      - app-storage:/app/storage
    networks: [backend]

  scheduler:
    image: mandegar-delivery:${APP_VERSION:-latest}
    restart: unless-stopped
    command: ["node", "dist/workers/scheduler.js"]
    environment: *app-environment
    depends_on:
      web: { condition: service_healthy }
    volumes:
      - app-storage:/app/storage
    networks: [backend]

  nginx:
    image: nginx:1.28-alpine
    restart: unless-stopped
    depends_on:
      web: { condition: service_healthy }
    ports:
      - "${HTTP_PORT:-8080}:80"
    volumes:
      - ./deployment/nginx/mandegar.conf:/etc/nginx/conf.d/default.conf:ro
    networks: [frontend, backend]

volumes:
  postgres-data:
  redis-data:
  app-storage:

networks:
  frontend:
  backend:
    internal: true
