services:
  caddy:
    env_file: "stack.env"
    image: caddy:alpine
    container_name: caddy
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile # Monta el archivo de configuración
      - caddy_data:/data # Almacena los certificados en este volumen
      - caddy_config:/config # Configuración de Caddy
      - shared_frontend:/www
    ports:
      - 80:80 # Puerto HTTP (Caddy lo redirige automáticamente a HTTPS)
      - 443:443 # Puerto HTTPS
      - 13001:13001
    networks:
      - reverse_proxy
    restart: on-failure

  mariadb:
    env_file: "stack.env"
    image: mariadb:latest
    container_name: mariadb
    ports:
      - 3306:3306
    volumes:
      - mariadb_data:/var/lib/mysql
    networks:
      - private
    restart: on-failure

  wait-for-mariadb:
    image: atkrad/wait4x
    depends_on:
      - mariadb
    command: tcp mariadb:3306 -t 30s -i 250ms
    networks:
      - private

  phpmyadmin:
    env_file: "stack.env"
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
      - 8080:80
    networks:
      - private
    depends_on:
      - mariadb

  backend:
    env_file: "stack.env"
    build:
      args:
        - NODE_ENV=production
      context: ./
      dockerfile: Dockerfile.server
    environment:
      - NODE_ENV=production
    volumes:
      - backend_logs:/var/log
    ports:
      - 3001:3001
    networks:
      - private
      - reverse_proxy
    depends_on:
      wait-for-mariadb:
        condition: service_completed_successfully
    restart: on-failure

  frontend:
    env_file: "stack.env"
    build:
      args:
        - NODE_ENV=production
      context: ./
      dockerfile: Dockerfile.client
    environment:
      - NODE_ENV=production
    volumes:
      - shared_frontend:/www
    networks:
      - reverse_proxy
    depends_on:
      - caddy
      - backend

volumes:
  shared_frontend:
  caddy_data:
  caddy_config:
  mariadb_data:
  backend_logs:

networks:
  private:
  reverse_proxy:
