package knowledgeqa

import (
	"errors"
	"time"

	"github.com/tidbcloud/AgentTeam/internal/knowledgeqa/protocol"
)

var (
	ErrConversationNotFound  = errors.New("knowledge Q&A conversation not found")
	ErrConversationForbidden = errors.New("knowledge Q&A conversation forbidden")
	ErrTurnBusy              = errors.New("knowledge Q&A conversation already has an active turn")
	ErrQueueFull             = errors.New("knowledge Q&A queue is full")
	ErrRateLimited           = errors.New("knowledge Q&A submission rate exceeded")
	ErrInvalidState          = errors.New("knowledge Q&A state transition is invalid")
	ErrFeedbackInvalid       = errors.New("knowledge Q&A feedback is invalid")
)

// Session is the API-owned conversation model. Persistence details stay behind
// ConversationService so handlers are not coupled to the store package.
type Session struct {
	ID                        string
	WorkspaceID               string
	OrganizationID            string
	OwnerUserID               string
	OwnerMemberID             string
	RootMessageID             string
	ThreadChannelID           string
	RequestedKnowledgeBaseIDs []string
	RequestedSourceIDs        []string
	RequestedModel            string
	RequestedReasonLevel      string
	WebSearchEnabled          bool
	ActiveTurnID              string
	Status                    string
}

type Organization struct {
	ID string
}

type Turn struct {
	ID                        string
	WorkspaceID               string
	OrganizationID            string
	SessionID                 string
	RequesterUserID           string
	UserMessageID             string
	AssistantMessageID        string
	Question                  string
	RequestedKnowledgeBaseIDs []string
	RequestedSourceIDs        []string
	AuthorizedRoots           []AuthorizedRoot
	RequestedModel            string
	RequestedReasonLevel      string
	EffectiveModel            string
	EffectiveReasonLevel      string
	OrganizationPolicyVersion int64
	RuntimeVersion            string
	PromptVersion             string
	CurrentAttemptID          string
	Status                    string
	ErrorCode                 string
	DebugModelOutput          string
	Activities                []protocol.RuntimeActivity

	WebSearchEnabled bool
}

type AuthorizedRoot struct {
	KnowledgeBaseID string
	SourceID        string
	RootKind        string
	RepoRole        string
	Revision        string
	FreshnessStatus string
}

type Citation struct {
	ID              string
	TurnID          string
	CitationKind    string
	KnowledgeBaseID string
	SourceID        string
	RootKind        string
	RepoRole        string
	Revision        string
	ResolvedRef     string
	ResolvedRefKind string
	Path            string
	LineStart       int64
	LineEnd         int64
	URL             string
	Title           string
	Snippet         string
	FetchedAt       *time.Time
}

type FeedbackSnapshot struct {
	Turn            Turn
	ThreadChannelID string
	Answer          string
	Activities      []protocol.RuntimeActivity
	Citations       []Citation
}

type Feedback struct {
	ID        string
	TurnID    string
	Rating    string
	Reasons   []string
	Comment   string
	UpdatedAt time.Time
}

type Share struct {
	ID               string
	OwnerDisplayName string
	Question         string
	Answer           string
	Token            string
	CreatedAt        time.Time
	LastOpenedAt     time.Time
}

// ShareCitation is an internal authorization record. Source authority is used
// only for the live MeshX permission check and is never projected in share
// payloads.
type ShareCitation struct {
	ShareID              string
	EvidenceID           string
	SourceWorkspaceID    string
	SourceOrganizationID string
	SourceTurnID         string
	Citation             Citation
}

type UpsertFeedbackParams struct {
	Snapshot       FeedbackSnapshot
	ReporterUserID string
	Rating         string
	Reasons        []string
	Comment        string
	Now            time.Time
}

type CreateSessionParams struct {
	ID                        string
	WorkspaceID               string
	OrganizationID            string
	OwnerUserID               string
	OwnerMemberID             string
	RootMessageID             string
	ThreadChannelID           string
	RequestedKnowledgeBaseIDs []string
	RequestedSourceIDs        []string
	RequestedModel            string
	RequestedReasonLevel      string
	WebSearchEnabled          bool
	Now                       time.Time
}

type CreateTurnParams struct {
	WorkspaceID               string
	OrganizationID            string
	SessionID                 string
	RequesterUserID           string
	UserMessageID             string
	Question                  string
	IdempotencyKey            string
	RequestedKnowledgeBaseIDs []string
	RequestedSourceIDs        []string
	Now                       time.Time
}

type RetryTurnParams struct {
	WorkspaceID     string
	SessionID       string
	TurnID          string
	RequesterUserID string
	IdempotencyKey  string
	Now             time.Time
}
