# Production stack — portable to any Docker host
# Usage:
#   cp .env.production.example .env.production
#   # edit .env.production
#   docker compose -f docker-compose.prod.yml --env-file .env.production up -d --build

services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-archi}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB:-archi}
    volumes:
      - archi_pg_data:/var/lib/postgresql/data
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "pg_isready -U ${POSTGRES_USER:-archi} -d ${POSTGRES_DB:-archi}",
        ]
      interval: 5s
      timeout: 5s
      retries: 20
    networks:
      - archi

  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        NEXT_PUBLIC_APP_URL: ${NEXT_PUBLIC_APP_URL:-http://localhost:3000}
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - .env.production
    environment:
      NODE_ENV: production
      # Internal Docker DNS — overrides host DATABASE_URL for in-compose Postgres
      DATABASE_URL: postgresql://${POSTGRES_USER:-archi}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-archi}
      HOSTNAME: 0.0.0.0
      PORT: 3000
      RUN_DB_MIGRATE: ${RUN_DB_MIGRATE:-true}
      SEED_ON_BOOT: ${SEED_ON_BOOT:-false}
      STORAGE_PROVIDER: ${STORAGE_PROVIDER:-local}
    ports:
      - "${APP_PUBLISH_PORT:-3000}:3000"
    volumes:
      # Persist local uploads across restarts (skip if using S3/R2)
      - archi_media_data:/app/.data/storage
    healthcheck:
      test:
        [
          "CMD",
          "node",
          "-e",
          "fetch('http://127.0.0.1:3000/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))",
        ]
      interval: 30s
      timeout: 5s
      start_period: 45s
      retries: 5
    networks:
      - archi

  # Optional reverse proxy with automatic HTTPS (set DOMAIN + emails)
  # Uncomment when you have a public DNS A/AAAA record pointing at this host.
  # caddy:
  #   image: caddy:2-alpine
  #   restart: unless-stopped
  #   ports:
  #     - "80:80"
  #     - "443:443"
  #   environment:
  #     DOMAIN: ${PLATFORM_ROOT_DOMAIN}
  #   volumes:
  #     - ./deploy/Caddyfile:/etc/caddy/Caddyfile:ro
  #     - archi_caddy_data:/data
  #     - archi_caddy_config:/config
  #   depends_on:
  #     app:
  #       condition: service_healthy
  #   networks:
  #     - archi

volumes:
  archi_pg_data:
  archi_media_data:
  # archi_caddy_data:
  # archi_caddy_config:

networks:
  archi:
    driver: bridge
