# Staging stack — builds images from local backend/frontend (needs full repo clone).
# For pull-only images + registry (no app source on server), use docker-compose.registry.yml
# and .gitea/workflows/registry-build-deploy.yml instead.
#
# From this directory:
#   docker compose -f docker-compose.staging.yml --env-file .env.staging up -d --build

name: dyolink-staging

services:
  postgres:
    image: postgres:15-alpine
    container_name: dyolink_postgres_staging
    env_file:
      - 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:
    build:
      context: ../backend
      dockerfile: Dockerfile
    container_name: dyolink_backend_staging
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - backend.staging.env
    environment:
      NODE_ENV: production
      TZ: UTC
      PORT: "3000"
      SENTRY_ENVIRONMENT: staging
    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:
    build:
      context: ../frontend
      dockerfile: Dockerfile
      args:
        NEXT_PUBLIC_API_URL: ${STAGING_NEXT_PUBLIC_API_URL:-http://wixur.ir/api}
        NEXT_PUBLIC_APP_URL: ${STAGING_NEXT_PUBLIC_APP_URL:-http://wixur.ir}
        NEXT_PUBLIC_APP_NAME: ${STAGING_NEXT_PUBLIC_APP_NAME:-Dyolink}
        NEXT_PUBLIC_SENTRY_DSN: ${NEXT_PUBLIC_SENTRY_DSN:-}
        NEXT_PUBLIC_SENTRY_ENVIRONMENT: staging
    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:
      # Root redirects to /en (next-intl) — accept 2xx/3xx as healthy.
      test: ["CMD", "node", "-e", "require('http').get('http://127.0.0.1:3000/', (r) => { process.exit(r.statusCode >= 200 && r.statusCode < 400 ? 0 : 1); }).on('error', () => process.exit(1))"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  nginx:
    image: nginx:alpine
    container_name: dyolink_nginx_staging
    depends_on:
      backend:
        condition: service_healthy
      frontend:
        condition: service_healthy
    ports:
      - "127.0.0.1:${STAGING_LOCAL_PORT:-18088}: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
