// Package orchestration defines pure types for multi-bot coordination.
// Zero side effects — only data structures and helpers.
package orchestration

import "encoding/json"

// TaskEvent is sent by the orchestrator to a bot via the bus.
// It tells the bot: "answer this question in this room with this context."
type TaskEvent struct {
	TaskID            string           `json:"task_id"`
	TargetBotID       string           `json:"target_bot_id"`
	TargetRoomID      string           `json:"target_room_id"`
	OriginalSender    string           `json:"original_sender"`
	OriginalQuestion  string           `json:"original_question"`
	Iteration         int              `json:"iteration"`
	PreviousResponses []BotResponse    `json:"previous_responses,omitempty"`
	RoomContext       []ContextMessage `json:"room_context,omitempty"`
}

// BotResponse is a bot's reply to a TaskEvent.
type BotResponse struct {
	BotID string `json:"bot_id"`
	Text  string `json:"text"`
}

// TaskResult is sent by a bot back to the orchestrator via the bus.
type TaskResult struct {
	TaskID string      `json:"task_id"`
	BotID  string      `json:"bot_id"`
	Text   string      `json:"text"`
	Error  string      `json:"error,omitempty"`
}

// QualityScore is the LLM's evaluation of a bot's response.
type QualityScore struct {
	Score    float64 `json:"score"`    // 0.0–1.0
	Continue bool    `json:"continue"` // should the pipeline continue?
	Reason   string  `json:"reason"`
}

// RoutingDecision is the LLM's choice of which bot should respond.
type RoutingDecision struct {
	TargetBotID string  `json:"bot_id"`
	Confidence  float64 `json:"confidence"`
	Reason      string  `json:"reason"`
}

// ContextMessage is a single message from the room's recent history.
type ContextMessage struct {
	SenderID string `json:"sender_id"`
	Content  string `json:"content"`
}

// ParticipantInfo describes a bot available for routing.
type ParticipantInfo struct {
	ID           string   `json:"id"`
	MatrixUserID string   `json:"matrix_user_id"` // e.g. "@assistant-bot:server"
	Description  string   `json:"description"`
	Capabilities []string `json:"capabilities,omitempty"`
}

// MarshalTaskEvent serializes a TaskEvent to JSON for bus transport.
func MarshalTaskEvent(t TaskEvent) (string, error) {
	b, err := json.Marshal(t)
	if err != nil {
		return "", err
	}
	return string(b), nil
}

// UnmarshalTaskEvent deserializes a TaskEvent from JSON.
func UnmarshalTaskEvent(data string) (TaskEvent, error) {
	var t TaskEvent
	err := json.Unmarshal([]byte(data), &t)
	return t, err
}

// MarshalTaskResult serializes a TaskResult to JSON for bus transport.
func MarshalTaskResult(r TaskResult) (string, error) {
	b, err := json.Marshal(r)
	if err != nil {
		return "", err
	}
	return string(b), nil
}

// UnmarshalTaskResult deserializes a TaskResult from JSON.
func UnmarshalTaskResult(data string) (TaskResult, error) {
	var r TaskResult
	err := json.Unmarshal([]byte(data), &r)
	return r, err
}
