services:
  db:
    image: postgres:17-alpine
    restart: always
    environment:
      POSTGRES_USER: gmrelay
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}
      POSTGRES_DB: gmrelay_db
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - gmrelay
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U gmrelay -d gmrelay_db" ]
      interval: 3s
      timeout: 3s
      retries: 10

  db-backup:
    image: postgres:17-alpine
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    environment:
      POSTGRES_USER: gmrelay
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}
      POSTGRES_DB: gmrelay_db
      PGPASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}
      BACKUP_RETENTION_DAYS: ${BACKUP_RETENTION_DAYS:-7}
    volumes:
      - pgbackups:/backups
    networks:
      - gmrelay
    entrypoint: ["sh", "-c"]
    command:
      - |
        cat > /usr/local/bin/backup.sh << 'EOF'
        #!/bin/sh
        set -e
        TMPFILE="/tmp/backup_$$.sql"
        pg_dump -h db -U gmrelay -d gmrelay_db > "$TMPFILE"
        gzip "$TMPFILE"
        mv "$TMPFILE.gz" "/backups/gmrelay_db_$(date +%Y%m%d_%H%M%S).sql.gz"
        find /backups -name 'gmrelay_db_*.sql.gz' -type f -mtime +${BACKUP_RETENTION_DAYS} -delete
        EOF
        chmod +x /usr/local/bin/backup.sh
        echo "0 3 * * * /usr/local/bin/backup.sh" | crontab -
        crond -f

  bot:
    image: git.codeanddice.ru/toutsu/gmrelay-bot:3.9.8
    restart: always
    depends_on:
      db:
        condition: service_healthy
    environment:
      - "ConnectionStrings__gmrelaydb=Host=db;Port=5432;Database=gmrelay_db;Username=gmrelay;Password=${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}"
      - "Telegram__BotToken=${TELEGRAM_BOT_TOKEN:?Set TELEGRAM_BOT_TOKEN in .env}"
      - "Telegram__MiniAppUrl=${TELEGRAM_MINI_APP_URL:-}"
    networks:
      - gmrelay
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8081/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 3

  discord:
    image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:3.9.8
    restart: always
    depends_on:
      db:
        condition: service_healthy
      bot:
        condition: service_healthy
    environment:
      - "ConnectionStrings__gmrelaydb=Host=db;Port=5432;Database=gmrelay_db;Username=gmrelay;Password=${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}"
      - "Discord__Token=${DISCORD_BOT_TOKEN:?Set DISCORD_BOT_TOKEN in .env}"
    networks:
      - gmrelay
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8082/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 3

  web:
    image: git.codeanddice.ru/toutsu/gmrelay-web:3.9.8
    restart: always
    depends_on:
      db:
        condition: service_healthy
      bot:
        condition: service_healthy
    environment:
      - "ConnectionStrings__gmrelaydb=Host=db;Port=5432;Database=gmrelay_db;Username=gmrelay;Password=${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}"
      - "Telegram__BotToken=${TELEGRAM_BOT_TOKEN:?Set TELEGRAM_BOT_TOKEN in .env}"
      - "Telegram__BotUsername=${TELEGRAM_BOT_USERNAME:?Set TELEGRAM_BOT_USERNAME in .env}"
      - "Telegram__MiniAppUrl=${TELEGRAM_MINI_APP_URL:-}"
      - "Discord__ClientId=${DISCORD_CLIENT_ID:-}"
      - "Discord__ClientSecret=${DISCORD_CLIENT_SECRET:-}"
      - "Discord__RedirectUri=${DISCORD_REDIRECT_URI:-}"
      - "PortfolioCovers__StoragePath=/app/portfolio-covers"
    ports:
      - "${GMRELAY_WEB_PORT:-8080}:8080"
    volumes:
      - web_keys:/app/dataprotection-keys
      - portfolio_covers:/app/portfolio-covers
    networks:
      - gmrelay
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8080/health || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 3

volumes:
  pgdata:
    name: ${POSTGRES_VOLUME_NAME:-game_pgdata}
  web_keys:
    name: ${WEB_KEYS_VOLUME_NAME:-gmrelay_web_keys}
  pgbackups:
    name: ${BACKUP_VOLUME_NAME:-game_pgbackups}
  portfolio_covers:
    name: ${PORTFOLIO_COVERS_VOLUME_NAME:-gmrelay_portfolio_covers}

networks:
  gmrelay:
    driver: bridge
