package agent

import (
	"context"

	"github.com/tidbcloud/AgentTeam/internal/api"
	"github.com/tidbcloud/AgentTeam/internal/store/agent"
	"github.com/tidbcloud/AgentTeam/internal/store/message"
	"github.com/tidbcloud/AgentTeam/internal/store/rewind"
)

// MessageStore is the narrow storage surface needed by the message endpoints.
type MessageStore interface {
	ResolveWritableAgentTargetScopeID(ctx context.Context, agent agent.AuthenticatedAgent, target string) (string, error)
	ResolveReadableAgentTargetScopeID(ctx context.Context, agent agent.AuthenticatedAgent, target string) (string, error)
	ResolveAgentReviewSubjectScopeID(ctx context.Context, agent agent.AuthenticatedAgent, target string) (string, error)
	SendAgentMessage(ctx context.Context, sender agent.AuthenticatedAgent, params message.SendMessageParams) (message.MessageDelivery, error)
	SendAgentMessageWithFreshness(ctx context.Context, sender agent.AuthenticatedAgent, params message.SendMessageParams) (message.SendMessageResult, error)
	GetAgentWorkspaceSnapshot(ctx context.Context, agent agent.AuthenticatedAgent) (agent.WorkspaceSnapshot, error)
}

type WorkflowReplyService interface {
	ResolveAgentWorkflowReplyTargetScopeID(
		context.Context,
		agent.AuthenticatedAgent,
		string,
	) (string, error)
	SendAgentWorkflowReply(
		context.Context,
		agent.AuthenticatedAgent,
		string,
		string,
		string,
		message.SendMessageFreshness,
	) (message.SendMessageResult, error)
}

type WorkspaceSnapshotPager interface {
	GetAgentWorkspaceSnapshotPage(ctx context.Context, authenticated agent.AuthenticatedAgent, request agent.WorkspaceSnapshotRequest) (agent.WorkspaceSnapshot, error)
}

// RuntimeOverlay is the daemon-owned live state projected onto already-bounded
// agent workspace snapshots.
type RuntimeOverlay = api.AgentRuntimeOverlay

// ScopeAPIStore is implemented by the scope cutover API adapter for
// scope-aware history, search, read checks, and private-safe member display.
type ScopeAPIStore interface {
	ListScopeHistoryForAgent(ctx context.Context, agent agent.AuthenticatedAgent, request message.ScopeAPIHistoryRequest) (message.ScopeAPIHistoryPage, error)
	SearchScopeMessagesForAgent(ctx context.Context, agent agent.AuthenticatedAgent, request message.ScopeAPISearchRequest) (message.ScopeAPISearchPage, error)
	CanReadScopeMessageForAgent(ctx context.Context, agent agent.AuthenticatedAgent, request message.ScopeAPIReadCheckRequest) (message.ScopeAPIReadCheckResult, error)
	ListScopeMemberDisplayRowsForAgent(ctx context.Context, agent agent.AuthenticatedAgent, request message.ScopeMemberListRequest) (message.ScopeMemberListPage, error)
}

// RewindStore is the narrow recovery-point surface needed by message send.
type RewindStore interface {
	DeleteAgentRecoveryPointsOutsideThread(ctx context.Context, workspaceID string, agentID string, keepThreadID string) ([]rewind.MessageRecoveryPoint, error)
}

// MessageFanout emits a committed delivery through the canonical browser and
// daemon fanout path, including outbox delivery marking.
type MessageFanout interface {
	EmitCommittedMessage(ctx context.Context, workspaceID string, appMessage message.AppMessage, delivery message.MessageDelivery)
}

// AgentFanout is the shared fanout used by agent-authored message and reaction
// mutations.
type AgentFanout interface {
	MessageFanout
	ReactionFanout
}
