# OpenCCB Docker Compose - Producción con SSL
# Servidor: AWS EC2 us-east-2
# Dominios: studio.norteamericano.com y learning.norteamericano.com
# Usa nginx-proxy + acme-companion para SSL automático con Let's Encrypt

services:
  # ========================================
  # NGINX Proxy + SSL (Let's Encrypt)
  # ========================================
  nginx-proxy:
    image: nginxproxy/nginx-proxy:1.4
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - certs:/etc/nginx/certs:ro
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - ./nginx/proxy.conf:/etc/nginx/conf.d/proxy.conf:ro
      - ./nginx/studio.conf:/etc/nginx/vhost.d/studio.norteamericano.com:ro
    restart: always
    networks:
      - openccb-network

  acme-companion:
    image: nginxproxy/acme-companion:2.2
    container_name: acme-companion
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - certs:/etc/nginx/certs:rw
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - ./nginx/certs-data:/etc/acme.sh:rw
    environment:
      - DEFAULT_EMAIL=admin@norteamericano.com
      - NGINX_PROXY_CONTAINER=nginx-proxy
      - LETSENCRYPT_STAGING=${LETSENCRYPT_STAGING:-true}
    depends_on:
      - nginx-proxy
    restart: always
    networks:
      - openccb-network

  # ========================================
  # Base de Datos
  # ========================================
  db:
    image: pgvector/pgvector:pg16
    container_name: openccb-db
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: ${DB_PASSWORD:-password}
      POSTGRES_DB: openccb
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - openccb-network
    restart: always
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ========================================
  # Studio + CMS (HTTPS)
  # ========================================
  studio:
    build:
      context: .
      dockerfile: web/studio/Dockerfile
      args:
        NEXT_PUBLIC_CMS_API_URL: ${NEXT_PUBLIC_CMS_API_URL}
        NEXT_PUBLIC_LMS_API_URL: ${NEXT_PUBLIC_LMS_API_URL}
    container_name: openccb-studio
    environment:
      - VIRTUAL_HOST=studio.norteamericano.com
      - VIRTUAL_PORT=3000
      - LETSENCRYPT_HOST=studio.norteamericano.com
      - HTTPS_METHOD=noredirect
      - HSTS=off
      - HOSTNAME=0.0.0.0
      - DATABASE_URL=${CMS_DATABASE_URL}
      - LMS_INTERNAL_URL=http://experience:3002
      - NEXT_PUBLIC_LMS_API_URL=${NEXT_PUBLIC_LMS_API_URL}
    volumes:
      - uploads_data:/app/uploads
    env_file: .env
    extra_hosts:
      - "host.docker.internal:host-gateway"
      - "t-800:192.168.0.5"
    depends_on:
      db:
        condition: service_healthy
    networks:
      - openccb-network
    restart: always

  # ========================================
  # Experience + LMS
  # ========================================
  experience:
    build:
      context: .
      dockerfile: web/experience/Dockerfile
      args:
        NEXT_PUBLIC_LMS_API_URL: ${NEXT_PUBLIC_LMS_API_URL}
        NEXT_PUBLIC_CMS_API_URL: ${NEXT_PUBLIC_CMS_API_URL}
    container_name: openccb-experience
    environment:
      - VIRTUAL_HOST=learning.norteamericano.com
      - VIRTUAL_PORT=3003
      - LETSENCRYPT_HOST=learning.norteamericano.com
      - HTTPS_METHOD=noredirect
      - HSTS=off
      - HOSTNAME=0.0.0.0
      - DATABASE_URL=${LMS_DATABASE_URL}
      - NEXT_PUBLIC_CMS_API_URL=${NEXT_PUBLIC_CMS_API_URL}
    env_file: .env
    extra_hosts:
      - "host.docker.internal:host-gateway"
      - "t-800:192.168.0.5"
    depends_on:
      db:
        condition: service_healthy
    networks:
      - openccb-network
    restart: always

# ========================================
# Volúmenes y Redes
# ========================================
volumes:
  postgres_data:
  uploads_data:
  certs:
  vhost:
  html:

networks:
  openccb-network:
    driver: bridge
