version: "3.7"

##########################################################
# STACK COMPLETA: Krayin CRM + MySQL + Redis
##########################################################

services:

  # ===== KRAYIN CRM =====
  crm:
    image: gitea.blyzer.com.br/blyzer/growup-crm:latest
    ports:
      - "8090:80"  # Ajuste a porta externa conforme necessário
    volumes:
      - growup_data:/var/www/html/storage
      - growup_public:/var/www/html/public/storage
    networks:
      - network_public
    environment:
      - APP_ENV=${APP_ENV:-production}
      - APP_DEBUG=${APP_DEBUG:-false}
      - APP_URL=${APP_URL}
      - DB_CONNECTION=${DB_CONNECTION:-mysql}
      - DB_HOST=${DB_HOST:-mysql}
      - DB_PORT=${DB_PORT:-3306}
      - DB_DATABASE=${DB_DATABASE:-growup}
      - DB_USERNAME=${DB_USERNAME:-growup}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=${REDIS_HOST:-redis}
      - REDIS_PORT=${REDIS_PORT:-6379}
    depends_on:
      - mysql
      - redis
    deploy:
      mode: replicated
      replicas: 1
      update_config:
        order: start-first
        failure_action: rollback
        delay: 10s
      placement:
        constraints:
          - node.role == manager

  # ===== MYSQL =====
  mysql:
    image: percona/percona-server:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
      - MYSQL_DATABASE=${DB_DATABASE:-growup}
      - MYSQL_USER=${DB_USERNAME:-growup}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - TZ=America/Sao_Paulo
    networks:
      - network_public
    volumes:
      - mysql_data:/var/lib/mysql
    deploy:
      placement:
        constraints:
          - node.role == manager
      resources:
        limits:
          cpus: '1'
          memory: 1024M
    command:
      [
        "--character-set-server=utf8mb4",
        "--collation-server=utf8mb4_general_ci",
        "--sql-mode=",
        "--default-authentication-plugin=mysql_native_password",
        "--max-allowed-packet=512MB",
        "--expire_logs_days=7",
        "--max_binlog_size=100M"
      ]

  # ===== REDIS =====
  redis:
    image: redis:latest
    command:
      [
        "redis-server",
        "--appendonly",
        "yes",
        "--port",
        "6379"
      ]
    volumes:
      - redis_data:/data
    networks:
      - network_public
    deploy:
      placement:
        constraints:
          - node.role == manager
      resources:
        limits:
          cpus: "0.5"
          memory: 1024M

networks:
  network_public:
    external: true
    name: network_public

volumes:
  growup_data:
    external: true
    name: growup_data
  growup_public:
    external: true
    name: growup_public
  mysql_data:
    external: true
    name: mysql_data
  redis_data:
    external: true
    name: redis_data
