# Docker Compose - Student Enrollment System
# Optimizado para bajo consumo de RAM
# Usa archivo .env en la raíz del proyecto para configuración

services:
  db:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: student-db
    env_file:
      - ../../.env
    environment:
      - ACCEPT_EULA=Y
      - MSSQL_SA_PASSWORD=${DB_PASSWORD}
      - MSSQL_MEMORY_LIMIT_MB=${DB_MEMORY_LIMIT_MB:-512}
      - MSSQL_AGENT_ENABLED=false
      - MSSQL_PID=Express
    ports:
      - "${DB_PORT:-1433}:1433"
    volumes:
      - sqlserver-data:/var/opt/mssql:delegated
    deploy:
      resources:
        limits:
          cpus: "1"
          memory: ${DB_MEMORY_LIMIT:-1280M}
        reservations:
          cpus: "0.5"
          memory: 1024M
    healthcheck:
      test: /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "$${MSSQL_SA_PASSWORD}" -Q "SELECT 1" -C || exit 1
      interval: 15s
      timeout: 5s
      retries: 10
      start_period: 30s
    networks:
      - student-network
    restart: unless-stopped

  api:
    build:
      context: ../..
      dockerfile: deploy/docker/Dockerfile.api
      args:
        - DOTNET_CLI_TELEMETRY_OPTOUT=1
    container_name: student-api
    env_file:
      - ../../.env
    environment:
      - DB_HOST=db
      - DB_PORT=1433
      - ConnectionStrings__DefaultConnection=Server=db;Database=${DB_NAME:-StudentEnrollment};User Id=${DB_USER:-sa};Password=${DB_PASSWORD};TrustServerCertificate=True;Max Pool Size=50;Min Pool Size=2
      - ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT:-Development}
      - CORS_ORIGINS=${CORS_ORIGINS:-http://localhost:8080,http://localhost:4200}
      - DOTNET_gcServer=0
      - DOTNET_GCHeapCount=2
      - DOTNET_gcConcurrent=1
    ports:
      - "${API_PORT:-5000}:5000"
    deploy:
      resources:
        limits:
          cpus: "2"
          memory: 512M
        reservations:
          cpus: "0.5"
          memory: 256M
    depends_on:
      db:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:5000/health"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 30s
    networks:
      - student-network
    restart: unless-stopped

  frontend:
    build:
      context: ../..
      dockerfile: deploy/docker/Dockerfile.frontend
    container_name: student-frontend
    ports:
      - "${FRONTEND_PORT:-8080}:80"
    deploy:
      resources:
        limits:
          cpus: "1"
          memory: 128M
        reservations:
          cpus: "0.25"
          memory: 64M
    depends_on:
      api:
        condition: service_healthy
    networks:
      - student-network
    restart: unless-stopped

volumes:
  sqlserver-data:
    driver: local

networks:
  student-network:
    driver: bridge
