name: englishworldcenter-chatbot

services:
  # PostgreSQL Database (shared by both profiles)
  postgres:
    image: postgres:15-alpine
    container_name: ai-learning-platform-ewc-postgres
    environment:
      POSTGRES_DB: ai_learning_platform
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: pos45345qtgrdflkgf986fgfsgaksdfadsfes123
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"  # Expose PostgreSQL port for health checks
    networks:
      - english-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 60s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  # Temporal Server
  temporal:
    image: temporalio/auto-setup:1.22.0
    container_name: ai-learning-platform-ewc-temporal
    environment:
      - DB=postgresql
      - DB_PORT=5432
      - POSTGRES_USER=postgres
      - POSTGRES_PWD=pos45345qtgrdflkgf986fgfsgaksdfadsfes123
      - POSTGRES_SEEDS=postgres
    ports:
      - "7233:7233"  # Temporal gRPC port
      - "8082:8080"  # Temporal Web UI
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - english-network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pgrep -f 'temporal.*server' > /dev/null || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 10
      start_period: 180s # Dar más tiempo para que Temporal se inicialice (puede tardar en crear/esquemas de BD)

  # Backend API (shared by both profiles)
  backend:
    build:
      context: .
      dockerfile: backend/Dockerfile
    image: ai-learning-platform-ewc-backend:latest
    container_name: ai-learning-platform-ewc-backend
    env_file:
      - .env
    volumes:
      - backend_uploads:/app/uploads
      - backend_logs:/app/logs
    environment:
      # Las variables de entorno para el backend son las mismas en ambos perfiles.
      NODE_ENV: production
      PORT: 3000
      DATABASE_URL: postgresql://postgres:pos45345qtgrdflkgf986fgfsgaksdfadsfes123@postgres:5432/ai_learning_platform
      JWT_SECRET: ${JWT_SECRET}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET}
      ELEVENLABS_API_KEY: ${ELEVENLABS_API_KEY}
      CORS_ORIGIN: ${CORS_ORIGIN}
      PRISMA_MIGRATE: ${PRISMA_MIGRATE}
      RUN_SEED: ${RUN_SEED}
      # Temporal configuration
      TEMPORAL_HOST: temporal
      TEMPORAL_PORT: 7233
    ports:
      - "3000:3000"  # Expone el puerto 3000 para el perfil de desarrollo
    depends_on:
      postgres:
        condition: service_healthy
      temporal:
        condition: service_healthy
    networks:
      - english-network
    restart: unless-stopped

  # Frontend Vue.js App - Development Mode
  frontend-dev:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    image: ai-learning-platform-ewc-frontend-dev:latest
    container_name: ai-learning-platform-ewc-frontend-dev
    env_file:
      - .env
    environment:
      VITE_API_URL: ${VITE_API_URL:-http://localhost:3000}
      VITE_APP_NAME: ${VITE_APP_NAME:-English World Center}
      VITE_APP_COMPANY_NAME: ${VITE_APP_COMPANY_NAME:-English World Center}
      VITE_APP_COMPANY_URL: ${VITE_APP_COMPANY_URL:-https://englishworldcenter.com/}
      VITE_APP_FOOTER_TEXT: ${VITE_APP_FOOTER_TEXT:-All rights reserved}
    ports:
      - "5173:5173"
    depends_on:
      - backend
    volumes:
      - ./frontend:/app
      - /app/node_modules
    networks:
      - english-network
    command: npm run dev -- --host 0.0.0.0
    restart: unless-stopped
    profiles:
      - dev

  # Frontend Vue.js App - Production Mode
  frontend-prod:
    build:
      context: ./frontend
      dockerfile: Dockerfile.prod
      args:
        VITE_API_URL: ${VITE_API_URL:-http://localhost:3000}
    image: ai-learning-platform-ewc-frontend-prod:latest
    container_name: ai-learning-platform-ewc-frontend-prod
    ports:
      - "8080:80"   # Puerto HTTP del host: Puerto HTTP del contenedor (nginx del sistema redirige aquí)
    depends_on:
      - backend
    networks:
      - english-network
    restart: unless-stopped
    profiles:
      - prod

volumes:
  postgres_data:
    name: ai-learning-platform-postgres-data
    driver: local
  backend_uploads:
    name: ai-learning-platform-backend-uploads
    driver: local
  backend_logs:
    name: ai-learning-platform-backend-logs
    driver: local

networks:
  english-network:
    name: english-network
    driver: bridge
    external: false
