# docker-compose.yml — Teleton Rifa, self-hosted stack (app + postgres + caddy).
#
# Deploy:  docker compose --env-file .env.production up -d --build
#
# Everything is configured from .env.production (copy .env.production.example and
# fill it). See docs/deploy-vps.md for the full runbook. The `caddy` service terminates
# HTTPS (auto Let's Encrypt) and proxies to the app; the `db` service is a bundled
# Postgres you can swap for a managed one.

services:
  app:
    build:
      context: .
      args:
        # NEXT_PUBLIC_* are baked at build time (see Dockerfile).
        NEXT_PUBLIC_APP_URL: ${NEXT_PUBLIC_APP_URL:?set NEXT_PUBLIC_APP_URL}
        NEXT_PUBLIC_PAYMENT_PROVIDER: ${NEXT_PUBLIC_PAYMENT_PROVIDER:-niubiz}
        NEXT_PUBLIC_DEMO_MODE: ${NEXT_PUBLIC_DEMO_MODE:-}
        NEXT_PUBLIC_TURNSTILE_SITE_KEY: ${NEXT_PUBLIC_TURNSTILE_SITE_KEY:-}
    image: teleton-rifa:latest
    restart: unless-stopped
    env_file: .env.production
    expose:
      - "3000"
    depends_on:
      db:
        condition: service_healthy

  db:
    image: postgres:16
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-teleton}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env.production}
      POSTGRES_DB: ${POSTGRES_DB:-teleton}
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-teleton} -d ${POSTGRES_DB:-teleton}"]
      interval: 5s
      timeout: 5s
      retries: 12

  caddy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    environment:
      RIFA_DOMAIN: ${RIFA_DOMAIN:-rifa.teleton.pe}
      ACME_EMAIL: ${ACME_EMAIL:-sistemas@teleton.pe}
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - app

volumes:
  pgdata:
  caddy_data:
  caddy_config:
