services:
  # PostgreSQL Database
  db:
    image: postgres:16-alpine
    container_name: rswag-db
    restart: unless-stopped
    environment:
      POSTGRES_USER: rswag
      POSTGRES_PASSWORD: ${DB_PASSWORD:-devpassword}
      POSTGRES_DB: rswag
    volumes:
      - rswag-db-data:/var/lib/postgresql/data
    networks:
      - rswag-internal
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U rswag"]
      interval: 5s
      timeout: 5s
      retries: 5

  # Redis for sessions/cache
  redis:
    image: redis:7-alpine
    container_name: rswag-redis
    restart: unless-stopped
    volumes:
      - rswag-redis-data:/data
    networks:
      - rswag-internal

  # FastAPI Backend
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: rswag-backend
    restart: unless-stopped
    environment:
      - INFISICAL_CLIENT_ID=${INFISICAL_CLIENT_ID}
      - INFISICAL_CLIENT_SECRET=${INFISICAL_CLIENT_SECRET}
      - INFISICAL_PROJECT_SLUG=rswag
      - INFISICAL_ENV=prod
      - INFISICAL_URL=http://infisical:8080
      - DATABASE_URL=postgresql://rswag:${DB_PASSWORD:-devpassword}@db:5432/rswag
      - REDIS_URL=redis://redis:6379
      - DESIGNS_PATH=/app/designs
      - CONFIG_PATH=/app/config
      - SPACES_PATH=/app/spaces
    volumes:
      - ./designs:/app/designs
      - ./config:/app/config:ro
      - ./spaces:/app/spaces:ro
      - ./frontend/public/mockups:/app/mockups:ro
    depends_on:
      db:
        condition: service_healthy
    networks:
      - rswag-internal
      - traefik-public
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.rswag-api.rule=(Host(`rswag.online`) || Host(`fungiswag.jeffemmett.com`) || HostRegexp(`{subdomain:[a-z0-9-]+}.rswag.online`)) && PathPrefix(`/api`)"
      - "traefik.http.routers.rswag-api.entrypoints=web"
      - "traefik.http.services.rswag-api.loadbalancer.server.port=8000"
      - "traefik.docker.network=traefik-public"

  # Next.js Frontend
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
      args:
        - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL:-http://localhost:8000/api}
    container_name: rswag-frontend
    restart: unless-stopped
    environment:
      - NODE_ENV=production
    depends_on:
      - backend
    networks:
      - rswag-internal
      - traefik-public
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.rswag-web.rule=Host(`rswag.online`) || Host(`fungiswag.jeffemmett.com`) || HostRegexp(`{subdomain:[a-z0-9-]+}.rswag.online`)"
      - "traefik.http.routers.rswag-web.entrypoints=web"
      - "traefik.http.services.rswag-web.loadbalancer.server.port=3000"
      - "traefik.docker.network=traefik-public"

volumes:
  rswag-db-data:
  rswag-redis-data:

networks:
  rswag-internal:
    driver: bridge
  traefik-public:
    external: true
