version: "3.7"

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

services:
  
  # ===== KRAYIN CRM =====
  krayin:
    image: growup:latest  # Sua imagem customizada
    ports:
      - "8090:80"  # Ajuste a porta externa conforme necessário
    volumes:
      - krayin_data:/var/www/html/storage
      - krayin_public:/var/www/html/public/storage
    networks:
      - network_public
    environment:
      - APP_ENV=production
      - APP_DEBUG=false
      - APP_URL=https://seudominio.com.br
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_DATABASE=krayin
      - DB_USERNAME=krayin
      - DB_PASSWORD=Mfcd62!!Mfcd62!!
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    depends_on:
      - mysql
      - redis
    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints:
          - node.role == manager

  # ===== MYSQL =====
  mysql:
    image: percona/percona-server:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=Mfcd62!!Mfcd62!!
      - MYSQL_DATABASE=krayin
      - MYSQL_USER=krayin
      - MYSQL_PASSWORD=Mfcd62!!Mfcd62!!
      - 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:
  krayin_data:
    external: true
    name: krayin_data
  krayin_public:
    external: true
    name: krayin_public
  mysql_data:
    external: true
    name: mysql_data
  redis_data:
    external: true
    name: redis_data
