package server

import (
	"context"

	"github.com/tidbcloud/AgentTeam/internal/store/unread"
	unreadservice "github.com/tidbcloud/AgentTeam/internal/unread"
)

type unreadRuntimeStoreAdapter struct {
	store *unread.Store
}

func (adapter unreadRuntimeStoreAdapter) UpsertSnapshots(ctx context.Context, snapshots []unreadservice.Snapshot) error {
	return adapter.store.UpsertSnapshots(ctx, snapshots)
}

func (adapter unreadRuntimeStoreAdapter) UpsertTaskBarrierSnapshots(ctx context.Context, snapshots []unreadservice.Snapshot) error {
	return adapter.store.UpsertTaskBarrierSnapshots(ctx, snapshots)
}

func (adapter unreadRuntimeStoreAdapter) LoadSnapshotPage(ctx context.Context, workspaceID string, memberID string, scopeIDs []string, afterScopeID string, limit int) ([]unreadservice.Snapshot, error) {
	return adapter.store.LoadSnapshotPage(ctx, unread.SnapshotPageRequest{
		WorkspaceID:  workspaceID,
		MemberID:     memberID,
		ScopeIDs:     scopeIDs,
		AfterScopeID: afterScopeID,
		Limit:        limit,
	})
}

func (adapter unreadRuntimeStoreAdapter) LoadVisibleConversationScopeIDsPage(ctx context.Context, workspaceID string, memberID string, afterScopeID string, limit int) ([]string, error) {
	userID, err := adapter.store.LoadActiveHumanUserIDForMember(ctx, workspaceID, memberID)
	if err != nil {
		return nil, err
	}
	return adapter.store.LoadVisibleConversationScopeIDsPage(ctx, workspaceID, memberID, userID, afterScopeID, limit)
}

func (adapter unreadRuntimeStoreAdapter) LoadFrontierMetadataPage(ctx context.Context, workspaceID string, memberID string, scopeIDs []string, afterScopeID string, limit int) ([]unreadservice.FrontierMetadata, error) {
	return adapter.store.LoadFrontierMetadataPage(ctx, unread.MetadataPageRequest{
		WorkspaceID:  workspaceID,
		MemberID:     memberID,
		ScopeIDs:     scopeIDs,
		AfterScopeID: afterScopeID,
		Limit:        limit,
	})
}

func (adapter unreadRuntimeStoreAdapter) LoadScopeRosterPage(ctx context.Context, workspaceID string, scopeID string, afterMemberID string, limit int) ([]unreadservice.RosterMember, error) {
	return adapter.store.LoadScopeRosterPage(ctx, unread.RosterPageRequest{
		WorkspaceID:   workspaceID,
		ScopeID:       scopeID,
		AfterMemberID: afterMemberID,
		Limit:         limit,
	})
}

func (adapter unreadRuntimeStoreAdapter) LoadMessageTailPage(ctx context.Context, workspaceID string, scopeID string, afterSeq int64, limit int) ([]unreadservice.MessageStored, error) {
	return adapter.store.LoadMessageTailPage(ctx, unread.TailPageRequest{
		WorkspaceID: workspaceID,
		ScopeID:     scopeID,
		AfterSeq:    afterSeq,
		Limit:       limit,
	})
}

func (adapter unreadRuntimeStoreAdapter) LoadActivityTailPage(ctx context.Context, workspaceID string, scopeID string, afterSeq int64, limit int) ([]unreadservice.NonMessageActivityStored, error) {
	return adapter.store.LoadActivityTailPage(ctx, unread.TailPageRequest{
		WorkspaceID: workspaceID,
		ScopeID:     scopeID,
		AfterSeq:    afterSeq,
		Limit:       limit,
	})
}
