# Pin the Compose project name. Without this, Compose derives it from the working
# directory, so running `docker compose up -d postgres` from a git worktree would
# create a second, empty `<worktree>_postgres_data` volume and then collide with the
# fixed container_name values below. Every worktree shares one stack.
name: amsoil-dlp

services:
  postgres:
    image: postgres:16-alpine
    container_name: amsoil-dlp-postgres
    environment:
      POSTGRES_USER: jack_local
      POSTGRES_PASSWORD: 47CT0suaAgiMnwSKnMcj
      POSTGRES_DB: amsoil_dlp_jack_local
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U jack_local -d amsoil_dlp_jack_local" ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - amsoil-network

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: amsoil-dlp-pgadmin
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "8080:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@example.com
      PGADMIN_DEFAULT_PASSWORD: admin
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    networks:
      - amsoil-network
    healthcheck:
      test: [ "CMD", "wget", "--spider", "-q", "http://localhost/misc/ping" ]
      interval: 30s
      timeout: 10s
      retries: 3

  mailhog:
    image: mailhog/mailhog:latest
    container_name: amsoil-dlp-mailhog
    ports:
      - "1025:1025"
      - "8025:8025"
    networks:
      - amsoil-network
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:8025" ]
      interval: 30s
      timeout: 10s
      retries: 3

  app:
    build:
      context: .
      dockerfile: .docker/Dockerfile
    container_name: amsoil-dlp-app
    depends_on:
      postgres:
        condition: service_healthy
    ports:
      - "3000:3000"
    env_file:
      - .env.local
    environment:
      # Inside the app container, reach Postgres by its compose service name.
      # .env.local's DATABASE_URL uses localhost, which is correct for host-run
      # `npm run dev` but resolves to the app container itself in Docker.
      #
      # These credentials are intentionally literal (not ${POSTGRES_USER:-...}
      # substitution) so they cannot be changed by a stray env var in the
      # developer's shell. Keep them in sync with the `postgres` service above
      # and with DATABASE_URL in .env.example.
      DATABASE_URL: "postgresql://jack_local:47CT0suaAgiMnwSKnMcj@postgres:5432/amsoil_dlp_jack_local"
    volumes:
      - .:/app
      - /app/node_modules
      - /app/.next
    command: npm run dev
    networks:
      - amsoil-network
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:3000" ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  stripe:
    image: stripe/stripe-cli:latest
    container_name: amsoil-dlp-stripe
    command: listen --forward-to app:3000/api/webhooks/stripe --api-key ${STRIPE_SECRET_KEY}
    env_file:
      - .env.local
    networks:
      - amsoil-network
    depends_on:
      app:
        condition: service_started

volumes:
  postgres_data:
    driver: local
  pgadmin_data:
    driver: local

networks:
  amsoil-network:
    driver: bridge
