package media

import (
	"context"

	"github.com/tidbcloud/AgentTeam/internal/store/agent"
	"github.com/tidbcloud/AgentTeam/internal/store/attachment"
)

// AttachmentStore is the narrow storage port used by attachment and avatar routes.
type AttachmentStore interface {
	UploadAttachmentsForUser(ctx context.Context, userID string, workspaceID string, scopeID string, attachments []attachment.CreateAttachmentParams) ([]attachment.AppAttachment, error)
	UploadUserAvatar(ctx context.Context, userID string, workspaceID string, params attachment.CreateAttachmentParams) (string, error)
	UploadAgentAvatar(ctx context.Context, workspaceID string, agentID string, params attachment.CreateAttachmentParams) (string, error)
	UploadResourceAvatar(ctx context.Context, workspaceID string, ownerKind string, ownerID string, params attachment.CreateAttachmentParams) (string, error)
	LoadUserAvatar(ctx context.Context, userID string) (attachment.AttachmentDownload, error)
	LoadAgentAvatar(ctx context.Context, workspaceID string, agentID string) (attachment.AttachmentDownload, error)
	LoadAvatar(ctx context.Context, avatarID string) (attachment.AttachmentDownload, error)
	ResolveUserAvatarRef(ctx context.Context, userID string) (attachment.AvatarVariantRef, error)
	ResolveAgentAvatarRef(ctx context.Context, workspaceID string, agentID string) (attachment.AvatarVariantRef, error)
	LoadAvatarThumbnail(ctx context.Context, avatarID string, variant string) (attachment.AttachmentVariant, error)
	SaveAvatarThumbnail(ctx context.Context, avatarID string, variant string, gen attachment.GeneratedAvatarThumbnail) (attachment.AttachmentVariant, error)
	LoadAttachmentForMember(ctx context.Context, memberID string, workspaceID string, attachmentID string) (attachment.AttachmentDownload, error)
	LoadAttachmentForUser(ctx context.Context, userID string, workspaceID string, attachmentID string) (attachment.AttachmentDownload, error)
	AuthorizeAttachmentRead(ctx context.Context, workspaceID string, attachmentID string, userID string, isAgentToken bool, agentMemberID string) error
	LoadAttachmentThumbnail(ctx context.Context, workspaceID string, attachmentID string) (attachment.AttachmentVariant, error)
	SaveAttachmentThumbnail(ctx context.Context, workspaceID string, attachmentID string, gen attachment.GeneratedThumbnail) (attachment.AttachmentVariant, error)
}

// AgentAvatarProfileStore authorizes browser Agent avatar changes and applies
// the versioned avatar URL to the managed Agent profile.
type AgentAvatarProfileStore interface {
	GetAgentForUser(ctx context.Context, userID string, workspaceID string, agentID string) (agent.AgentView, error)
	UpdateAgentForUser(ctx context.Context, userID string, workspaceID string, agentID string, params agent.UpdateAgentParams) (agent.AgentView, error)
}

// AgentTokenStore is the narrow storage port used to authorize agent attachment downloads.
type AgentTokenStore interface {
	MemberIDForAgentAuthToken(ctx context.Context, bearerToken string) (string, error)
}

// PersonalStickerStore is the narrow storage port used by personal sticker routes.
type PersonalStickerStore interface {
	ListPersonalStickersForUser(ctx context.Context, userID string, workspaceID string) ([]attachment.PersonalSticker, error)
	UploadPersonalStickersForUser(ctx context.Context, userID string, workspaceID string, stickers []attachment.CreateAttachmentParams) ([]attachment.PersonalSticker, error)
	DeletePersonalStickerForUser(ctx context.Context, userID string, workspaceID string, stickerID string) error
	LoadPersonalStickerForUser(ctx context.Context, userID string, workspaceID string, stickerID string) (attachment.AttachmentDownload, error)
}

func (h *AttachmentHandler) loadAttachmentForRequest(ctx context.Context, isAgentToken bool, agentMemberID string, userID string, workspaceID string, attachmentID string) (attachment.AttachmentDownload, error) {
	if isAgentToken {
		return h.store.LoadAttachmentForMember(ctx, agentMemberID, workspaceID, attachmentID)
	}
	return h.store.LoadAttachmentForUser(ctx, userID, workspaceID, attachmentID)
}
