services:
  db:
    image: postgres:17-alpine
    container_name: careit_db
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${DB_USER:-postgres}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_DB: ${DB_NAME:-careit_db}
      PGDATA: /var/lib/postgresql/data
    volumes:
      - careit_pgdata:/var/lib/postgresql/data
      - ./SQL:/docker-entrypoint-initdb.d:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-postgres} -d ${DB_NAME:-careit_db}"]
      interval: 5s
      timeout: 3s
      retries: 20
    # Buka port ini hanya kalau kamu butuh akses DB dari host
    # ports:
    #   - "127.0.0.1:${DB_EXPOSE_PORT:-15432}:5432"
    networks:
      - careit_network

  backend:
    build:
      context: ./backendcareit_v4
      dockerfile: Dockerfile
    container_name: careit_backend
    restart: unless-stopped
    environment:
      DB_HOST: db
      DB_PORT: "5432"
      DB_USER: ${DB_USER:-postgres}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_NAME: ${DB_NAME:-careit_db}
      HOST: 0.0.0.0
      PORT: "8081"
    ports:
      - "${BACKEND_PORT:-8081}:8081"
    depends_on:
      db:
        condition: service_healthy
    networks:
      - careit_network

  frontend:
    build:
      context: ./frontendcareit_v4
      dockerfile: Dockerfile
      args:
        NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://localhost:8081}
    container_name: careit_frontend
    restart: unless-stopped
    environment:
      NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-http://localhost:8081}
    ports:
      - "${FRONTEND_PORT:-3000}:3000"
    depends_on:
      - backend
    networks:
      - careit_network

networks:
  careit_network:
    driver: bridge

volumes:
  careit_pgdata:
