package agent

import (
	"github.com/tidbcloud/AgentTeam/internal/api"
	"github.com/tidbcloud/AgentTeam/internal/store/storeerr"
	"github.com/tidbcloud/AgentTeam/internal/workflow/publicerr"
)

// publicError constructs a canonical API-layer domain error that the shared
// Agent/Workflow classifiers project into the canonical public Workflow error
// contract. Status is authoritative; the caller-supplied problem is bounded
// before projection.
func publicError(status int, code string, problem string) error {
	return publicerr.WorkflowError{
		Kind: storeerr.ErrBadRequest, Status: status, Code: code, Problem: problem,
	}
}

// workflowErrorResponse maps a canonical public Workflow classification into the
// shared API error envelope, carrying every canonical contract field. details
// optionally overrides the classified details (for example after a domain
// project pass); pass nil to keep the classified details.
func workflowErrorResponse(classified publicerr.Classified, details any) api.ErrorResponse {
	if details == nil {
		details = classified.Details
	}
	response := api.ErrorResponse{
		Status: classified.Status, Message: classified.Error.Problem,
		Code: classified.Error.Code, Details: details,
		Stage: string(classified.Error.Stage), Path: classified.Error.Path,
		Hint: classified.Error.Hint, Retryable: classified.Error.Retryable,
		Diagnostics: classified.Error.Diagnostics,
	}
	if classified.Error.Truncation != nil {
		response.Truncation = classified.Error.Truncation
	}
	return response
}
