services:
  postgres:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-sig}
      POSTGRES_USER: ${POSTGRES_USER:-sig}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-sig} -d ${POSTGRES_DB:-sig}"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped

  fuzzy-service:
    build:
      context: ./fuzzy-service
      dockerfile: Dockerfile
    restart: unless-stopped

  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    environment:
      PORT: 4000
      JWT_SECRET: ${JWT_SECRET}
      FRONTEND_URL: https://${DOMAIN}
      DATABASE_URL: postgres://${POSTGRES_USER:-sig}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB:-sig}
      FUZZY_SERVICE_URL: http://fuzzy-service:5001
    depends_on:
      postgres:
        condition: service_healthy
    restart: unless-stopped

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile.frontend
      args:
        NEXT_PUBLIC_BACKEND_URL: https://${DOMAIN}
        BACKEND_URL: http://backend:4000
    environment:
      NODE_ENV: production
    depends_on:
      - backend
    restart: unless-stopped

  caddy:
    image: caddy:2-alpine
    environment:
      DOMAIN: ${DOMAIN}
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy_data:/data
      - caddy_config:/config
    depends_on:
      - frontend
    restart: unless-stopped

volumes:
  postgres_data:
  caddy_data:
  caddy_config:
