package channel

import (
	"context"

	"github.com/tidbcloud/AgentTeam/internal/api"
	"github.com/tidbcloud/AgentTeam/internal/store/agent"
	storechannel "github.com/tidbcloud/AgentTeam/internal/store/channel"
	"github.com/tidbcloud/AgentTeam/internal/store/message"
	"github.com/tidbcloud/AgentTeam/internal/store/model"
)

var ErrNotFound = api.ErrNotFound

// ChannelAccessStore is the narrow store surface used by channel route access resolution.
type ChannelAccessStore interface {
	ResolveChannelAccessForUser(ctx context.Context, userID string, workspaceID string, channelID string) (storechannel.ChannelAccessResolution, error)
}

// ChannelDiscoveryStore is the narrow store surface used by channel discovery routes.
type ChannelDiscoveryStore interface {
	ListDiscoverableChannelsForUser(ctx context.Context, userID string, workspaceID string) ([]storechannel.DiscoverableChannel, error)
	DiscoverableChannelForUser(ctx context.Context, userID string, workspaceID string, channelID string) (storechannel.DiscoverableChannel, error)
}

// ThreadSubsystemChannelStore resolves hidden subsystem carrier channels for thread listing.
type ThreadSubsystemChannelStore interface {
	EnsureAgentThreadSessionChannelForUser(ctx context.Context, userID string, workspaceID string) (message.ChannelSummary, error)
}

// ThreadStore is the narrow storage port used by reply thread routes.
type ThreadStore interface {
	ListSubscribedThreadsForUserWithLimit(ctx context.Context, userID string, workspaceID string, limit int) ([]message.SubscribedThread, error)
	ListSubscribedThreadInboxPageForUser(ctx context.Context, userID string, workspaceID string, limit int, cursor message.ThreadInboxCursor) (message.ThreadInboxPage, error)
	CountSubscribedThreadsForUser(ctx context.Context, userID string, workspaceID string) (message.ThreadInboxCount, error)
	GetReplyThreadForUser(ctx context.Context, userID string, workspaceID string, threadChannelID string) (message.SubscribedThread, error)
	GetThreadMembershipForUser(ctx context.Context, userID string, workspaceID string, threadChannelID string) (message.ThreadMembership, error)
	LoadConversationForUser(ctx context.Context, userID string, workspaceID string, scopeID string, includeHidden bool) (message.ConversationItem, error)
	UnsubscribeThreadForUser(ctx context.Context, userID string, workspaceID string, threadChannelID string) error
	SubscribeThreadForUser(ctx context.Context, userID string, workspaceID string, threadChannelID string) error
	ListReplyThreadSummariesForUserWithLimit(ctx context.Context, userID string, workspaceID string, channelID string, limit int) (map[string]message.ReplyThreadSummary, error)
	OpenReplyThreadForUser(ctx context.Context, userID string, workspaceID string, channelID string, parentMessageID string) (message.ReplyThreadSummary, error)
	SetThreadSubscriptionStateForUser(ctx context.Context, userID string, workspaceID string, threadChannelID string, state string) error
}

// ThreadShareCardAppStore owns reply-thread share-card mutations and reads.
type ThreadShareCardAppStore interface {
	CreateThreadShareCardForUser(ctx context.Context, user model.User, workspaceID string, sourceThreadChannelID string, targetScopeID string) (message.AppMessage, message.MessageDelivery, error)
	LoadThreadShareCardForUser(ctx context.Context, userID string, workspaceID string, messageID string) (message.ThreadShareCardView, error)
}

// ThreadReviewFeedbackAppStore owns review feedback IM message mutations and reads.
type ThreadReviewFeedbackAppStore interface {
	CreateReviewFeedbackForUser(ctx context.Context, user model.User, workspaceID string, rc agent.ReviewContext, params message.ReviewFeedbackBuildRequest) (message.AppMessage, message.MessageDelivery, error)
	ListReviewFeedbackForUser(ctx context.Context, userID string, workspaceID string, rc agent.ReviewContext, limit int) ([]message.AppMessage, error)
	SetReviewFeedbackItemStatusForUser(ctx context.Context, user model.User, workspaceID string, rc agent.ReviewContext, messageID string, itemID string, status string) (message.AppMessage, error)
}

// ThreadAppStore is the route-level app store dependency for reply-thread and
// conversation-owned review routes.
type ThreadAppStore interface {
	ThreadShareCardAppStore
	ThreadReviewFeedbackAppStore
}

// ThreadReviewStore is the storage port used by thread-owned review routes.
type ThreadReviewStore interface {
	GetReviewContext(ctx context.Context, workspaceID string, id string) (*agent.ReviewContext, error)
	ListReviewContextsForThread(ctx context.Context, userID string, workspaceID string, threadID string) ([]agent.ReviewContext, error)
}
