services:
  postgres:
    image: postgres:16-alpine
    restart: unless-stopped
    networks:
      - internal
    environment:
      POSTGRES_DB: rentaldrivego
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-change-me}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d rentaldrivego"]
      interval: 5s
      timeout: 5s
      retries: 10
    volumes:
      - postgres_prod_data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    networks:
      - internal
    volumes:
      - redis_prod_data:/data

  api:
    build:
      context: .
      dockerfile: Dockerfile.production
    command:
      - bash
      - -c
      - |
        npm run db:deploy &&
        npm run start --workspace @rentaldrivego/api
    networks:
      - internal
      - traefik-proxy
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    env_file:
      - .env.docker.production
    restart: unless-stopped
    labels:
      - traefik.enable=true
      - traefik.http.routers.api.rule=Host(`${API_DOMAIN}`)
      - traefik.http.routers.api.entrypoints=websecure
      - traefik.http.routers.api.tls.certresolver=letsencrypt
      - traefik.http.services.api.loadbalancer.server.port=4000

  frontend:
    build:
      context: .
      dockerfile: Dockerfile.production
    command:
      - bash
      - -c
      - |
        npm run start --workspace @rentaldrivego/public-site &
        npm run start --workspace @rentaldrivego/marketplace &
        npm run start --workspace @rentaldrivego/admin &
        npm run start --workspace @rentaldrivego/dashboard &
        wait -n
    networks:
      - internal
      - traefik-proxy
    depends_on:
      - api
    env_file:
      - .env.docker.production
    restart: unless-stopped
    labels:
      - traefik.enable=true
      # public-site (port 3003)
      - traefik.http.routers.homepage.rule=Host(`rentaldrivego.ma`) || Host(`www.rentaldrivego.ma`)
      - traefik.http.routers.homepage.entrypoints=websecure
      - traefik.http.routers.homepage.tls.certresolver=letsencrypt
      - traefik.http.routers.homepage.service=public-site-svc
      - traefik.http.services.public-site-svc.loadbalancer.server.port=3003
      # marketplace (port 3000)
      - traefik.http.routers.marketplace.rule=Host(`${MARKETPLACE_DOMAIN}`)
      - traefik.http.routers.marketplace.entrypoints=websecure
      - traefik.http.routers.marketplace.tls.certresolver=letsencrypt
      - traefik.http.routers.marketplace.service=marketplace-svc
      - traefik.http.services.marketplace-svc.loadbalancer.server.port=3000
      # admin (port 3002)
      - traefik.http.routers.admin.rule=Host(`${ADMIN_DOMAIN}`)
      - traefik.http.routers.admin.entrypoints=websecure
      - traefik.http.routers.admin.tls.certresolver=letsencrypt
      - traefik.http.routers.admin.service=admin-svc
      - traefik.http.services.admin-svc.loadbalancer.server.port=3002
      # dashboard (port 3001)
      - traefik.http.routers.dashboard.rule=Host(`${DASHBOARD_DOMAIN}`)
      - traefik.http.routers.dashboard.entrypoints=websecure
      - traefik.http.routers.dashboard.tls.certresolver=letsencrypt
      - traefik.http.routers.dashboard.service=dashboard-svc
      - traefik.http.services.dashboard-svc.loadbalancer.server.port=3001

  pgmanage:
    image: cmdpromptinc/pgmanage-enterprise:latest
    restart: unless-stopped
    user: "0:0"
    networks:
      - internal
      - traefik-proxy
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      PGMANAGE_LISTEN_PORT: "8000"
    volumes:
      - pgmanage_prod_data:/appdata
    labels:
      - traefik.enable=true
      - traefik.http.routers.pgmanage.rule=Host(`${PGMANAGE_DOMAIN}`)
      - traefik.http.routers.pgmanage.entrypoints=websecure
      - traefik.http.routers.pgmanage.tls.certresolver=letsencrypt
      - traefik.http.services.pgmanage.loadbalancer.server.port=8000

networks:
  internal:
    driver: bridge
  traefik-proxy:
    external: true

volumes:
  postgres_prod_data:
  redis_prod_data:
  pgmanage_prod_data:
