package policy

import (
	"github.com/acp/interceptor/internal/auth"
	"github.com/acp/interceptor/internal/models"
)

// ToPolicyInput maps an API request into the OPA input document.
func ToPolicyInput(req models.ToolCallRequest, traceID string, principal *auth.Principal, reg *models.AgentRegistryInfo) models.PolicyInput {
	in := models.PolicyInput{
		AgentID:   req.AgentID,
		Tool:      req.Tool,
		Action:    req.Action,
		Amount:    req.Amount,
		RiskScore: req.RiskScore,
		TraceID:   traceID,
	}
	if principal != nil {
		in.Principal = &models.PrincipalClaims{
			Sub:     principal.Sub,
			AgentID: principal.AgentID,
			Roles:   principal.Roles,
			Scopes:  principal.Scopes,
		}
	}
	if reg != nil {
		in.Agent = reg
	}
	if len(req.Extra) > 0 {
		in.Extra = req.Extra
	}
	return in
}
