services:
  db:
    image: postgres:17-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-posse_party}
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-postgres}"]
      interval: 5s
      timeout: 5s
      retries: 5
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - posse-party-network
    deploy:
      placement:
        constraints:
          - node.labels.type == nas

  migrate:
    image: ghcr.io/searlsco/posse_party:latest
    env_file:
      - .env
    environment:
      DATABASE_URL: postgres://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-posse_party}
      RAILS_ENV: production
    depends_on:
      db:
        condition: service_healthy
    command: ["./script/release"]
    restart: "no"
    networks:
      - posse-party-network
    deploy:
      placement:
        constraints:
          - node.labels.type == nas
      restart_policy:
        condition: none

  web:
    image: ghcr.io/searlsco/posse_party:latest
    restart: unless-stopped
    env_file:
      - .env
    environment:
      DATABASE_URL: postgres://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-posse_party}
      RAILS_ENV: production
      APP_HOST: ${APP_HOST:-posseparty.bendtstudio.com}
      SECRET_KEY_BASE: ${SECRET_KEY_BASE}
      FORCE_SSL: "false"
    command: ["./script/server"]
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS http://localhost:3000/up || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s
    depends_on:
      db:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    stdin_open: true
    tty: true
    networks:
      - posse-party-network
      - dokploy-network
    labels:
      - traefik.enable=true
      - traefik.http.routers.posseparty-web.rule=Host(`${APP_HOST:-posseparty.bendtstudio.com}`)
      - traefik.http.routers.posseparty-web.entrypoints=web
      - traefik.http.services.posseparty-web.loadbalancer.server.port=3000
      - traefik.http.routers.posseparty-web.service=posseparty-web
      - traefik.http.routers.posseparty-web.middlewares=redirect-to-https@file
      - traefik.http.routers.posseparty-websecure.rule=Host(`${APP_HOST:-posseparty.bendtstudio.com}`)
      - traefik.http.routers.posseparty-websecure.entrypoints=websecure
      - traefik.http.services.posseparty-websecure.loadbalancer.server.port=3000
      - traefik.http.routers.posseparty-websecure.service=posseparty-websecure
      - traefik.http.routers.posseparty-websecure.tls.certresolver=letsencrypt
    deploy:
      placement:
        constraints:
          - node.labels.type == nas

  worker:
    image: ghcr.io/searlsco/posse_party:latest
    restart: unless-stopped
    env_file:
      - .env
    environment:
      DATABASE_URL: postgres://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD:-postgres}@db:5432/${POSTGRES_DB:-posse_party}
      RAILS_ENV: production
      APP_HOST: ${APP_HOST:-posseparty.bendtstudio.com}
      SECRET_KEY_BASE: ${SECRET_KEY_BASE}
    depends_on:
      db:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    command: ["./script/worker"]
    healthcheck:
      test: [
        "CMD-SHELL",
        "./bin/rails runner 'exit(SolidQueue::Process.where(\"last_heartbeat_at > ?\", SolidQueue.process_alive_threshold.ago).exists? ? 0 : 1)'"
      ]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    networks:
      - posse-party-network
    deploy:
      placement:
        constraints:
          - node.labels.type == nas

volumes:
  db_data:

networks:
  posse-party-network:
    driver: bridge
  dokploy-network:
    external: true
