package app

import (
	"context"
	"time"

	"github.com/tidbcloud/AgentTeam/internal/coredb"
	"github.com/tidbcloud/AgentTeam/internal/store/message"
)

// ChannelManagementPort is the narrow channel-exec member/profile mutation port
// over the app Store. The canonical caller-tx primitives live as package-level
// functions (NOT *Store methods), so the app-ecosystem root Store method ceiling
// is not raised; this port re-exposes them as methods to satisfy the
// channelmanagement service's ChannelAppStore, and delegates the read/room
// methods to the underlying Store.
type ChannelManagementPort struct {
	store *Store
}

// NewChannelManagementPort wraps an app Store with the channel-exec mutation port.
func NewChannelManagementPort(store *Store) *ChannelManagementPort {
	return &ChannelManagementPort{store: store}
}

func (p *ChannelManagementPort) UpdateChannelProfileInTx(ctx context.Context, tx coredb.Tx, workspaceID string, channelID string, actorUserID string, policy ChannelUpdatePolicy, expectedVersion *int64, name *string, description *string, visibility *string) (message.ChannelSummary, int64, error) {
	return updateChannelProfileInTx(ctx, p.store, tx, workspaceID, channelID, actorUserID, policy, expectedVersion, name, description, visibility)
}

func (p *ChannelManagementPort) BatchAddChannelMembersInTx(ctx context.Context, tx coredb.Tx, workspaceID string, channelID string, authorityUserID string, actorMemberID string, members []message.ChannelMemberAddRequest, now time.Time, authorityPolicy ChannelMemberAuthorityPolicy, effects *ChannelMemberMutationEffects) ([]message.ChannelMemberEventResult, error) {
	return batchAddChannelMembersInTx(ctx, p.store, tx, workspaceID, channelID, authorityUserID, actorMemberID, members, now, authorityPolicy, effects)
}

func (p *ChannelManagementPort) BatchRemoveChannelMembersInTx(ctx context.Context, tx coredb.Tx, workspaceID string, channelID string, authorityUserID string, actorMemberID string, refs []ChannelMemberExecReference, now time.Time, effects *ChannelMemberMutationEffects) ([]message.ChannelMemberEventResult, error) {
	return batchRemoveChannelMembersInTx(ctx, p.store, tx, workspaceID, channelID, authorityUserID, actorMemberID, refs, now, effects)
}

func (p *ChannelManagementPort) ChannelMembersForUser(ctx context.Context, userID string, workspaceID string, channelID string) (message.ChannelMemberList, error) {
	return p.store.ChannelMembersForUser(ctx, userID, workspaceID, channelID)
}

func (p *ChannelManagementPort) ChannelMembersForUserPage(ctx context.Context, userID string, workspaceID string, channelID string, options message.ChannelMemberListOptions) (message.ChannelMemberList, error) {
	return p.store.ChannelMembersForUserPage(ctx, userID, workspaceID, channelID, options)
}

func (p *ChannelManagementPort) ListRealtimeRoomIDsForChannel(ctx context.Context, workspaceID string, channelID string) ([]string, error) {
	return p.store.ListRealtimeRoomIDsForChannel(ctx, workspaceID, channelID)
}
