services:
  db:
    image: postgres:16-alpine
    container_name: daagbox-prod-db
    restart: always
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}
      POSTGRES_DB: ${POSTGRES_DB:-daagbox}
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\""]
    # Not published to the host — reachable only on the Compose network (do not add ports: here)
      interval: 5s
      timeout: 5s
      retries: 5

  backend:
    build:
      context: ./server
      dockerfile: Dockerfile
    container_name: daagbox-prod-backend
    restart: always
    environment:
      PORT: 5000
      DATABASE_URL: "postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB:-daagbox}?schema=public"
      RP_ID: ${RP_ID:-localhost}
      ORIGIN: ${ORIGIN:-http://localhost}
      TRUST_PROXY: ${TRUST_PROXY:-1}
      VAPID_PUBLIC_KEY: ${VAPID_PUBLIC_KEY:-}
      VAPID_PRIVATE_KEY: ${VAPID_PRIVATE_KEY:-}
      VAPID_SUBJECT: ${VAPID_SUBJECT:-mailto:support@kapteins-daagbok.eu}
      OpenWeatherMapAPIKey: ${OpenWeatherMapAPIKey:-}
      OpenRouterAPIKey: ${OpenRouterAPIKey:-}
      OpenRouterModel: ${OpenRouterModel:-anthropic/claude-3.5-haiku}
      SESSION_SECRET: ${SESSION_SECRET:-}
      ADMIN_USER_IDS: ${ADMIN_USER_IDS:-}
      NTFY_SERVER: ${NTFY_SERVER:-https://ntfy.sh}
      NTFY_TOPIC: ${NTFY_TOPIC:-}
      NTFY_TOKEN: ${NTFY_TOKEN:-}
    command: sh -c "npx prisma db push && node dist/index.js"
    healthcheck:
      test: ["CMD", "node", "-e", "const http = require('http'); http.get('http://localhost:5000/api/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); }).on('error', () => process.exit(1));"]
      interval: 15s
      timeout: 5s
      start_period: 60s
      retries: 5
    depends_on:
      db:
        condition: service_healthy

  frontend:
    build:
      context: .
      dockerfile: client/Dockerfile
      args:
        APP_VERSION: ${APP_VERSION:-0.1.0.0-dev}
    container_name: daagbox-prod-frontend
    restart: always
    environment:
      PLAUSIBLE_ENABLED: ${PLAUSIBLE_ENABLED:-true}
      PLAUSIBLE_HOST: ${PLAUSIBLE_HOST:-https://plausible.elpatron.me}
    ports:
      - "80:80"
    depends_on:
      backend:
        condition: service_healthy

volumes:
  pgdata:
    name: daagbox-prod-pgdata
