version: '3.8'

services:
  # Application
  app:
    build:
      context: .
      dockerfile: Dockerfile
      target: builder
    container_name: boilerplate-app
    restart: unless-stopped
    ports:
      - '${PORT:-3000}:3000'
    environment:
      - NODE_ENV=development
      - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/boilerplate_db?schema=public
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    env_file:
      - .env
    volumes:
      - .:/app
      - /app/node_modules
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: npm run start:dev
    networks:
      - boilerplate-network

  # PostgreSQL Database
  postgres:
    image: postgres:16-alpine
    container_name: boilerplate-postgres
    restart: unless-stopped
    ports:
      - '5432:5432'
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: boilerplate_db
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres']
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - boilerplate-network

  # Redis
  redis:
    image: redis:7-alpine
    container_name: boilerplate-redis
    restart: unless-stopped
    ports:
      - '6379:6379'
    volumes:
      - redis_data:/data
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - boilerplate-network

  # Adminer (Database UI)
  adminer:
    image: adminer:latest
    container_name: boilerplate-adminer
    restart: unless-stopped
    ports:
      - '8080:8080'
    depends_on:
      - postgres
    networks:
      - boilerplate-network

volumes:
  postgres_data:
  redis_data:

networks:
  boilerplate-network:
    driver: bridge
