'use client'

import ScrollDown from './ScrollDown'
import MentionButton from './MentionButton'
import ViewerCard from './ViewerCard'
import { resolveViewerCardIsBanned } from './banChatHelpers'
import type { ChatMessage, ViewerCardState, ParticipantItem, UserAdminInfo } from '../types'

interface MessagesOverlayProps {
    mode: 'desktop' | 'mobile'
    // ScrollDown props
    isAtBottom: boolean
    latestMessage: ChatMessage | null
    onScrollToBottom: () => void
    streamerId: string
    // MentionButton props
    unreadMentionCount: number
    onScrollToMention: () => void
    // ViewerCard props (conditional)
    chatViewerCard: ViewerCardState | null
    chatViewerCardRef: React.RefObject<HTMLDivElement>
    viewerCardDepth: 'root' | 'gift' | 'admin'
    viewerCardBanStatus: { userId: string; isBanned: boolean } | null
    bannedUserIds: Set<string>
    userAdminInfo: UserAdminInfo | null
    // Handlers
    onCloseViewerCard: () => void
    onBackToRoot?: () => void
    onGift: (user: ParticipantItem) => void
    onBan: (user: ParticipantItem) => void
    onUnban: (user: ParticipantItem) => void
    onAddMod: (user: ParticipantItem) => void
    onRemoveMod: (user: ParticipantItem) => void
    // Permissions
    isModerator: boolean
    isStreamer: boolean
    isSuperAdmin: boolean
    currentUserId?: string | null
    modUserIds?: ReadonlySet<string>
    superAdminUserIds?: ReadonlySet<string>
    // i18n
    t: (key: string) => string
}

export default function MessagesOverlay({
    mode,
    isAtBottom,
    latestMessage,
    onScrollToBottom,
    streamerId,
    unreadMentionCount,
    onScrollToMention,
    chatViewerCard,
    chatViewerCardRef,
    viewerCardDepth,
    viewerCardBanStatus,
    bannedUserIds,
    userAdminInfo,
    onCloseViewerCard,
    onBackToRoot,
    onGift,
    onBan,
    onUnban,
    onAddMod,
    onRemoveMod,
    isModerator,
    isStreamer,
    isSuperAdmin,
    currentUserId,
    modUserIds,
    superAdminUserIds,
    t,
}: MessagesOverlayProps) {
    return (
        <>
            <ScrollDown
                isVisible={!isAtBottom}
                latestMessage={latestMessage}
                onScrollToBottom={onScrollToBottom}
                streamerId={streamerId}
                modUserIds={modUserIds}
                superAdminUserIds={superAdminUserIds}
                t={t}
            />
            <MentionButton
                unreadCount={unreadMentionCount}
                mode={mode}
                onScrollToMention={onScrollToMention}
                t={t}
            />
            {chatViewerCard?.mode === mode && (
                <ViewerCard
                    cardRef={chatViewerCardRef}
                    viewerCard={chatViewerCard}
                    viewerCardDepth={viewerCardDepth}
                    t={t}
                    onClose={onCloseViewerCard}
                    onBackToRoot={onBackToRoot || (() => { })}
                    onGift={onGift}
                    isModerator={isModerator}
                    isStreamer={isStreamer}
                    isSuperAdmin={isSuperAdmin}
                    onBan={onBan}
                    onUnban={onUnban}
                    isBanned={resolveViewerCardIsBanned(
                        chatViewerCard.user.id,
                        bannedUserIds,
                        viewerCardBanStatus
                    )}
                    currentUserId={currentUserId}
                    userAdminInfo={userAdminInfo}
                    onAddMod={onAddMod}
                    onRemoveMod={onRemoveMod}
                    streamerId={streamerId}
                />
            )}
        </>
    )
}

