services:
  payload:
    build:
      context: .
      dockerfile: Dockerfile
    image: hanoman-website-be:latest
    restart: unless-stopped
    ports:
      - '3000:3000'
    # Seed (and local uploads) write files to ./media on the host. The app image has no project
    # checkout — without this mount, DB rows point to paths like /app/media/*.png that do not exist.
    volumes:
      - ./media:/app/media
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - .env
    environment:
      NODE_ENV: production
      DATABASE_URL: ${DATABASE_URL}

  # Ensure DATABASE_URL points to this service when running in Docker:
  # postgresql://postgres:postgres@postgres:5432/hanoman
  postgres:
    restart: unless-stopped
    image: postgres:16
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5

  # One-off DB seeding on the same Docker network as Postgres (hostname "postgres" in DATABASE_URL works).
  # Usage: docker compose --profile seed run --rm seed
  seed:
    profiles:
      - seed
    image: node:22.17.0-alpine
    working_dir: /app
    volumes:
      - .:/app
    env_file:
      - .env
    environment:
      NODE_ENV: development
      CI: "true"
    depends_on:
      postgres:
        condition: service_healthy
    command:
      - sh
      - -c
      - corepack enable && corepack prepare pnpm@10.33.0 --activate && pnpm install --frozen-lockfile && pnpm seed

volumes:
  pgdata:
