services:
  traefik:
    image: traefik:2.11
    container_name: traefik
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./services/traefik/traefik.yml:/etc/traefik/traefik.yml:ro
      - ./services/traefik/dynamic.yml:/etc/traefik/dynamic.yml:ro
      - ./services/traefik/acme.json:/acme.json
    labels:
      - "traefik.enable=true"
      # Dashboard configuration (replace with your domain)
      - "traefik.http.routers.traefik.rule=Host(`traefik.example.com`)"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.routers.traefik.tls=true"
      - "traefik.http.routers.traefik.tls.certresolver=le"
      - "traefik.http.routers.traefik.service=api@internal"

  redis:
    image: redis:7
    container_name: redis
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  mysql:
    image: mysql:8.4
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${CONFIG_DB_PASSWORD:-example_root_password}
      MYSQL_DATABASE: ${CONFIG_DB_DATABASE:-example_db}
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "MYSQL_PWD=$$MYSQL_ROOT_PASSWORD mysqladmin -u root ping --silent"]
      interval: 10s
      timeout: 5s
      retries: 10

  edge-broker:
    build:
      context: .
      dockerfile: services/edge-broker/Dockerfile
    container_name: edge-broker
    environment:
      EDGE_AUTH_MODE: ${EDGE_AUTH_MODE:-strict}
      EDGE_MANAGER_URL: ${EDGE_MANAGER_URL:-http://caddy}
      EDGE_BROKER_SHARED_SECRET: ${EDGE_BROKER_SHARED_SECRET:?set EDGE_BROKER_SHARED_SECRET in .env}
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.edge-broker-api.rule=Host(`api.example.com`) && PathPrefix(`/edge-broker`)"
      - "traefik.http.routers.edge-broker-api.entrypoints=websecure"
      - "traefik.http.routers.edge-broker-api.tls=true"
      - "traefik.http.routers.edge-broker-api.tls.certresolver=le"
      - "traefik.http.routers.edge-broker-api.middlewares=edge-broker-strip"
      - "traefik.http.routers.edge-broker-api.service=edge-broker"
      - "traefik.http.routers.edge-broker-local.rule=Host(`localhost`) && PathPrefix(`/api/edge-broker`)"
      - "traefik.http.routers.edge-broker-local.entrypoints=web"
      - "traefik.http.routers.edge-broker-local.middlewares=edge-broker-strip-local"
      - "traefik.http.routers.edge-broker-local.service=edge-broker"
      - "traefik.http.middlewares.edge-broker-strip.stripPrefix.prefixes=/edge-broker"
      - "traefik.http.middlewares.edge-broker-strip-local.stripPrefix.prefixes=/api/edge-broker"
      - "traefik.http.services.edge-broker.loadbalancer.server.port=4300"


  caddy:
    image: caddy:2.7.6-alpine
    container_name: caddy
    depends_on:
      - php1
    volumes:
      - ./services/nginx/app:/var/www/html
      - ./services/caddy/Caddyfile:/etc/caddy/Caddyfile:ro
      - ./services/caddy/logs:/var/log/caddy
    labels:
      - "traefik.enable=true"
      # API routing (replace with your domain)
      - "traefik.http.routers.api.rule=Host(`api.example.com`)"
      - "traefik.http.routers.api.entrypoints=websecure"
      - "traefik.http.routers.api.tls=true"
      - "traefik.http.routers.api.tls.certresolver=le"
      - "traefik.http.routers.api.service=caddy"
      # HTTP to HTTPS redirect
      - "traefik.http.routers.api-http.rule=Host(`api.example.com`)"
      - "traefik.http.routers.api-http.entrypoints=web"
      - "traefik.http.routers.api-http.middlewares=redirect-to-https@file"
      - "traefik.http.routers.api-http.service=caddy"
      # Local development (HTTP only)
      - "traefik.http.routers.local.rule=Host(`localhost`)"
      - "traefik.http.routers.local.entrypoints=web"
      - "traefik.http.routers.local.service=caddy"

  php1:
    build:
      context: .
      dockerfile: services/php/Dockerfile
    container_name: php1
    depends_on:
      - redis
      - mysql
      - edge-broker
    command: ["php-fpm"]
    env_file:
      - .env.example
    environment:
      AUTO_COMPOSER_INSTALL: "true"
      EDGE_BROKER_URL: ${EDGE_BROKER_URL:-http://edge-broker:4300}
      EDGE_BROKER_SHARED_SECRET: ${EDGE_BROKER_SHARED_SECRET:?set EDGE_BROKER_SHARED_SECRET in .env}
    volumes:
      - ./services/nginx/app:/var/www/html
      - ./services/php/php.ini:/usr/local/etc/php/conf.d/zz-custom.ini:ro
      - ./services/php/logs:/var/log/php

  php-cron:
    build:
      context: .
      dockerfile: services/php/Dockerfile
    container_name: php-cron
    depends_on:
      - redis
      - mysql
      - edge-broker
    command: ["sh", "-c", "while true; do php index.php run cron; sleep 60; done"]
    env_file:
      - .env.example
    environment:
      AUTO_COMPOSER_INSTALL: "false"
      EDGE_BROKER_URL: ${EDGE_BROKER_URL:-http://edge-broker:4300}
      EDGE_BROKER_SHARED_SECRET: ${EDGE_BROKER_SHARED_SECRET:?set EDGE_BROKER_SHARED_SECRET in .env}
    volumes:
      - ./services/nginx/app:/var/www/html
      - ./services/php/php.ini:/usr/local/etc/php/conf.d/zz-custom.ini:ro

  n8n:
    image: n8nio/n8n:latest
    container_name: n8n
    restart: always
    environment:
      - N8N_HOST=n8n.example.com
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - WEBHOOK_URL=https://n8n.example.com/
      - GENERIC_TIMEZONE=${CONFIG_TIMEZONE:-Europe/Copenhagen}
    volumes:
      - n8n_data:/home/node/.n8n
    labels:
      - "traefik.enable=true"
      # n8n over HTTPS (replace with your domain and cert resolver)
      - "traefik.http.routers.n8n.rule=Host(`n8n.example.com`)"
      - "traefik.http.routers.n8n.entrypoints=websecure"
      - "traefik.http.routers.n8n.tls=true"
      - "traefik.http.routers.n8n.tls.certresolver=le"
      - "traefik.http.routers.n8n.service=n8n"
      # n8n HTTP to HTTPS redirect
      - "traefik.http.routers.n8n-http.rule=Host(`n8n.example.com`)"
      - "traefik.http.routers.n8n-http.entrypoints=web"
      - "traefik.http.routers.n8n-http.middlewares=redirect-to-https@file"
      - "traefik.http.routers.n8n-http.service=n8n"
      # n8n service port
      - "traefik.http.services.n8n.loadbalancer.server.port=5678"

volumes:
  mysql_data:
  redis_data:
  n8n_data:
