package roles

import (
	"github.com/tidbcloud/AgentTeam/presets"
)

// presetTemplate is the meshx-side shape used by Service consumers. It is
// adapted from presets.RolePreset so this package keeps its own RuntimeHints
// and Attachment types (which carry transport-only fields like Metadata) at
// the boundary.
type presetTemplate struct {
	ID           string
	Version      int
	Locale       string
	Name         string
	Description  string
	Scenario     string
	Icon         string
	Tags         []string
	Definition   string
	RuntimeHints RuntimeHints
	Attachments  []Attachment
}

func selectPresetTemplates(locale string) ([]presetTemplate, error) {
	presetsList, err := presets.RolePresets(locale)
	if err != nil {
		return nil, err
	}
	out := make([]presetTemplate, 0, len(presetsList))
	for _, preset := range presetsList {
		out = append(out, adaptRolePreset(preset))
	}
	return out, nil
}

func selectPresetTemplate(id, locale string) (presetTemplate, bool, error) {
	preset, ok, err := presets.RolePresetByID(id, locale)
	if err != nil {
		return presetTemplate{}, false, err
	}
	if !ok {
		return presetTemplate{}, false, nil
	}
	return adaptRolePreset(preset), true, nil
}

func adaptRolePreset(preset presets.RolePreset) presetTemplate {
	attachments := make([]Attachment, 0, len(preset.Attachments))
	for _, attachment := range preset.Attachments {
		attachments = append(attachments, Attachment{
			Type:        attachment.Type,
			Name:        attachment.Name,
			Description: attachment.Description,
			Required:    attachment.Required,
			Realization: attachment.Realization,
			ResourceID:  attachment.ResourceID,
			AccessLevel: attachment.AccessLevel,
			Branch:      attachment.Branch,
			Content:     attachment.Content,
		})
	}
	return presetTemplate{
		ID:          preset.ID,
		Version:     preset.Version,
		Locale:      preset.Locale,
		Name:        preset.Name,
		Description: preset.Description,
		Scenario:    preset.Scenario,
		Icon:        preset.Icon,
		Tags:        preset.Tags,
		Definition:  preset.Definition,
		RuntimeHints: RuntimeHints{
			Runtime:         preset.RuntimeHints.Runtime,
			Model:           preset.RuntimeHints.Model,
			ReasoningEffort: preset.RuntimeHints.ReasoningEffort,
			SuggestedName:   preset.RuntimeHints.SuggestedName,
		},
		Attachments: attachments,
	}
}
