package oracle

import (
	"context"
	"time"

	"github.com/Armur-Ai/Pentest-Swarm-AI/internal/swarm/verify"
)

// timeoutOracle bounds how long a single Probe may run. An oracle that drives
// the network (a listen window, several timed requests) could otherwise stall a
// whole campaign on one slow target; the deadline turns that into an
// Inconclusive (via the probe's own ctx error) instead of a hang.
type timeoutOracle struct {
	inner   verify.Oracle
	timeout time.Duration
}

// WithTimeout wraps an oracle so each Probe is given at most d. d <= 0 returns
// the oracle unchanged.
func WithTimeout(o verify.Oracle, d time.Duration) verify.Oracle {
	if d <= 0 {
		return o
	}
	return timeoutOracle{inner: o, timeout: d}
}

func (t timeoutOracle) Name() string                   { return t.inner.Name() }
func (t timeoutOracle) Applies(c verify.Candidate) bool { return t.inner.Applies(c) }

func (t timeoutOracle) Probe(ctx context.Context, c verify.Candidate) (verify.OracleVerdict, float64, string, error) {
	ctx, cancel := context.WithTimeout(ctx, t.timeout)
	defer cancel()
	return t.inner.Probe(ctx, c)
}
