# Pull-only staging stack — uses images from a registry (Gitea Packages / Docker Hub / etc.).
# No backend/frontend source on the deployment host except this compose file + config + secrets.
#
# Required env (see deploy.registry.env.example):
#   REGISTRY_PREFIX   e.g. 178.131.50.201:3000/yourgiteauser  (no protocol, no trailing slash)
#   IMAGE_TAG         short sha or "latest" (CI sets this per deploy)
# Optional:
#   DEPLOY_SECRETS_DIR   absolute path on the server to database/backend *.env files (see below)
#
# Deploy:
#   docker compose -f docker-compose.registry.yml --env-file deploy.registry.env pull
#   docker compose -f docker-compose.registry.yml --env-file deploy.registry.env up -d

name: dyolink-registry

services:
  postgres:
    image: postgres:15-alpine
    container_name: dyolink_postgres_staging
    env_file:
      - ${DEPLOY_SECRETS_DIR:-.}/database.staging.env
    environment:
      TZ: UTC
    volumes:
      - postgres_data_staging:/var/lib/postgresql/data
      - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
      - ./database/backups:/backups
    networks:
      - dyolink_staging
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER"]
      interval: 15s
      timeout: 10s
      retries: 5
      start_period: 40s

  backend:
    image: ${REGISTRY_PREFIX}/dyolink-backend:${IMAGE_TAG:-latest}
    container_name: dyolink_backend_staging
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - ${DEPLOY_SECRETS_DIR:-.}/backend.staging.env
    environment:
      NODE_ENV: production
      TZ: UTC
      PORT: "3000"
    expose:
      - "3000"
    networks:
      - dyolink_staging
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://127.0.0.1:3000/api/health', (r) => {if(r.statusCode!==200)process.exit(1)})"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  frontend:
    image: ${REGISTRY_PREFIX}/dyolink-frontend:${IMAGE_TAG:-latest}
    container_name: dyolink_frontend_staging
    depends_on:
      - backend
    environment:
      NODE_ENV: production
      TZ: UTC
      PORT: "3000"
      HOSTNAME: "0.0.0.0"
    expose:
      - "3000"
    networks:
      - dyolink_staging
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "node", "-e", "require('http').get('http://127.0.0.1:3000/', (r) => {if(r.statusCode!==200)process.exit(1)})"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  nginx:
    image: nginx:alpine
    container_name: dyolink_nginx_staging
    depends_on:
      - backend
      - frontend
    ports:
      - "${STAGING_HTTP_PORT:-8088}:80"
    volumes:
      - ./nginx/http-only.conf:/etc/nginx/conf.d/default.conf:ro
      - ./logs/nginx-staging:/var/log/nginx
    networks:
      - dyolink_staging
    restart: unless-stopped

networks:
  dyolink_staging:
    name: dyolink_staging

volumes:
  postgres_data_staging:
    name: dyolink_postgres_data_staging
