package message

import (
	"fmt"
	"time"

	"github.com/tidbcloud/AgentTeam/internal/core/routing"
)

const (
	OwnerQuoteSourceRefsSectionHeader  = routing.OwnerQuoteSourceRefsSectionHeader
	OwnerQuoteSourceRefLinePrefix      = routing.OwnerQuoteSourceRefLinePrefix
	OwnerQuoteInstructionSectionHeader = routing.OwnerQuoteInstructionSectionHeader
)

type OwnerQuoteSourceMessage struct {
	ID             string
	SourceScopeID  string
	AuthorMemberID string
	AuthorType     string
	MessageType    string
	Content        string
	CreatedAt      time.Time
	Seq            int64
	GlobalSeq      int64
	Attachments    []OwnerQuoteAttachmentSnapshot
}

type OwnerQuoteAttachmentSnapshot struct {
	ID          string
	Filename    string
	ContentType string
	SizeBytes   int64
}

func FormatOwnerQuoteSourceHeader(index int, author string, sourceMessageID string, createdAt time.Time) string {
	return fmt.Sprintf(
		"[%d] author=@%s message_id=%s created_at=%s",
		index,
		author,
		sourceMessageID,
		createdAt.UTC().Format(time.RFC3339),
	)
}

func ParseOwnerQuoteSourceMessageIDs(content string) []string {
	return routing.ParseOwnerQuoteSourceMessageIDs(content)
}

// ExtractOwnerQuoteMentionContent returns the instruction and quoted message
// bodies from an owner_quote raw draft, suitable for mention token parsing.
// It strips structural metadata lines (author=, message_id=, created_at=,
// source_message_id=, section headers) so that @mentions in the metadata
// are not misinterpreted as delivery targets.
func ExtractOwnerQuoteMentionContent(content string) string {
	return routing.ExtractOwnerQuoteMentionContent(content)
}

func ExtractOwnerQuoteInstruction(content string) string {
	return routing.ExtractOwnerQuoteInstruction(content)
}
