# Caddyfile for Jellyfin Xbox Codec Filter Proxy
# Routes Xbox PlaybackInfo to Python filter, everything else to Jellyfin directly

:8096 {
    # Debug: Log all requests
    log {
        output stdout
        format json
        level DEBUG
    }

    # Xbox PlaybackInfo requests with Xbox in User-Agent go to Python filter
    @xbox_playback {
        path_regexp ^/Users/.*/Items/.*/PlaybackInfo$
        header User-Agent *Xbox*
    }

    # Test: Route ALL PlaybackInfo requests to filter first (ignore User-Agent)
    @all_playback {
        path_regexp ^/Users/.*/Items/.*/PlaybackInfo$
    }

    handle @all_playback {
        respond "Matched PlaybackInfo - User-Agent: {http.request.header.User-Agent}" 200
        # Uncomment when working:
        # reverse_proxy xbox-filter:8096 {
        #     header_up Host {host}
        #     header_up X-Real-IP {remote_host}
        #     header_up X-Forwarded-For {remote_host}
        #     header_up X-Forwarded-Proto {scheme}
        # }
    }

    # All other requests go directly to Jellyfin
    handle {
        reverse_proxy jellyfin:8096 {
            header_up Host {host}
            header_up X-Real-IP {remote_host}
            header_up X-Forwarded-For {remote_host}
            header_up X-Forwarded-Proto {scheme}

            # Enable WebSocket support
            transport http {
                versions h2c 1.1
            }
        }
    }

    # Enable compression
    encode gzip
}
