services:
  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    depends_on:
      backend:
        condition: service_healthy
    environment:
      - NUXT_PUBLIC_API_BASE=/api
    labels:
      - "traefik.enable=true"
      - "traefik.http.middlewares.hms-gzip.compress=true"
      - "traefik.http.middlewares.hms-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.hms-http.entryPoints=http"
      - "traefik.http.routers.hms-http.middlewares=hms-redirect"
      - "traefik.http.routers.hms-http.rule=Host(`hms.media-on.de`) && PathPrefix(`/`)"
      - "traefik.http.routers.hms-https.entryPoints=https"
      - "traefik.http.routers.hms-https.middlewares=hms-gzip"
      - "traefik.http.routers.hms-https.rule=Host(`hms.media-on.de`) && PathPrefix(`/`)"
      - "traefik.http.routers.hms-https.tls=true"
      - "traefik.http.routers.hms-https.tls.certresolver=letsencrypt"
      - "traefik.http.services.hms.loadbalancer.server.port=3000"
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://localhost:3000/').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 15s
    networks:
      - hms-network
      - coolify

  backend:
    build: ./backend
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      - DATABASE_URL=${DATABASE_URL:-postgresql+asyncpg://hms:hms@postgres:5432/hms}
      - REDIS_URL=${REDIS_URL:-redis://redis:6379/0}
      - RENTMAN_API_TOKEN=${RENTMAN_API_TOKEN}
      - JWT_SECRET=${JWT_SECRET}
      - SMTP_HOST=${SMTP_HOST}
      - SMTP_PORT=${SMTP_PORT:-587}
      - SMTP_USER=${SMTP_USER}
      - SMTP_PASSWORD=${SMTP_PASSWORD}
      - SMTP_FROM=${SMTP_FROM:-info@hms-licht-ton.de}
      - CORS_ORIGINS=${CORS_ORIGINS:-https://hms.media-on.de,http://localhost:3000}
      - ADMIN_USERNAME=${ADMIN_USERNAME:-admin}
      - ADMIN_PASSWORD=${ADMIN_PASSWORD}
    volumes:
      - ./images:/data/images
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/api/health')"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 10s
    networks:
      - hms-network

  postgres:
    image: postgres:16-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=hms
      - POSTGRES_PASSWORD=hms
      - POSTGRES_DB=hms
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U hms -d hms"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 5s
    networks:
      - hms-network

  redis:
    image: redis:7-alpine
    volumes:
      - redis_data:/data
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 5s
    networks:
      - hms-network

networks:
  hms-network:
    driver: bridge
  coolify:
    external: true

volumes:
  postgres_data:
  redis_data:
