'use client'


import { FaCrown, FaWrench } from 'react-icons/fa'
import { FaS } from 'react-icons/fa6'
import type { ChatMessage, ParticipantItem } from '../types'

/** 依訊息上的 userType 與即時 mod／超管集合解析角色（供圖示即時更新） */
export function resolveChatUserType(
    item: ChatMessage | ParticipantItem,
    streamerId: string | undefined,
    modUserIds?: ReadonlySet<string>,
    superAdminUserIds?: ReadonlySet<string>
): ChatMessage['userType'] | undefined {
    const userId = ('userId' in item ? item.userId : item.id) || null
    const uid = userId ? String(userId).trim() : ''
    if (item.userType === 'streamer' || (uid && uid === streamerId)) return 'streamer'
    if (item.userType === 'superAdmin' || (uid && superAdminUserIds?.has(uid))) return 'superAdmin'
    if (item.userType === 'manager' || (uid && modUserIds?.has(uid))) return 'manager'
    return item.userType
}

export const getUserRoleBadge = (
    item: ChatMessage | ParticipantItem,
    streamerId?: string,
    modUserIds?: ReadonlySet<string>,
    superAdminUserIds?: ReadonlySet<string>
) => {
    const userId = 'userId' in item ? (item.userId || null) : item.id
    const userType = resolveChatUserType(item, streamerId, modUserIds, superAdminUserIds)
    const isStreamer = userType === 'streamer' || userId === streamerId
    const isMod = userType === 'manager'
    const isSuperAdmin = userType === 'superAdmin'

    if (isStreamer) {
        return (
            <span className="inline-flex items-center justify-center w-4 h-4 rounded-sm bg-orange-500 border border-orange-600 dark:border-orange-400 flex-shrink-0 mr-1">
                <FaCrown className="w-2.5 h-2.5 text-white" />
            </span>
        )
    }
    if (isMod) {
        return (
            <span className="inline-flex items-center justify-center w-4 h-4 rounded-sm bg-blue-500 border border-blue-600 dark:border-blue-400 flex-shrink-0 mr-1">
                <FaWrench className="w-2.5 h-2.5 text-white" />
            </span>
        )
    }
    if (isSuperAdmin) {
        return (
            <span className="inline-flex items-center justify-center w-4 h-4 rounded-sm bg-purple-500 border border-purple-600 dark:border-purple-400 flex-shrink-0 mr-1">
                <FaS className="w-2.5 h-2.5 text-white" />
            </span>
        )
    }
    return null
}

export const getUserNameColor = (
    item: ChatMessage | ParticipantItem,
    streamerId?: string,
    modUserIds?: ReadonlySet<string>,
    superAdminUserIds?: ReadonlySet<string>
) => {
    const userId = 'userId' in item ? (item.userId || null) : item.id
    const userType = resolveChatUserType(item, streamerId, modUserIds, superAdminUserIds)
    const isStreamer = userType === 'streamer' || userId === streamerId
    const isMod = userType === 'manager'
    const isSuperAdmin = userType === 'superAdmin'

    if (isStreamer) {
        return 'text-orange-600 dark:text-orange-400'
    }
    if (isMod) {
        return 'text-blue-600 dark:text-blue-400'
    }
    if (isSuperAdmin) {
        return 'text-purple-600 dark:text-purple-400'
    }
    return 'text-gray-900 dark:text-gray-100'
}

/** Gift Super Chat：有身分時用聊天室名稱色，否則沿用 tier 預設樣式 */
export function getGiftCardUserNameClass(
    item: ChatMessage | ParticipantItem,
    streamerId: string | undefined,
    tierDefaultClass: string,
    variant: 'header' | 'body',
    modUserIds?: ReadonlySet<string>,
    superAdminUserIds?: ReadonlySet<string>
): string {
    const role = resolveChatUserType(item, streamerId, modUserIds, superAdminUserIds)
    if (role !== 'streamer' && role !== 'manager' && role !== 'superAdmin') {
        return tierDefaultClass
    }
    const layout = variant === 'header' ? 'truncate text-[1em]' : 'text-[1em]'
    return [layout, 'font-semibold', getUserNameColor(item, streamerId, modUserIds, superAdminUserIds)]
        .filter(Boolean)
        .join(' ')
}

/** OBS 疊層：訊息列角色修飾 class（供 Browser Source 自訂 CSS 選擇） */
/** 聊天／ViewerCard：未登入訪客（id 為 guest_* 或無 id） */
export function isGuestParticipant(user: Pick<ParticipantItem, 'id'>): boolean {
    const id = String(user.id ?? '').trim()
    return !id || id.startsWith('guest_')
}

export function getObsMessageRoleModifier(
    item: ChatMessage | ParticipantItem,
    streamerId?: string,
    modUserIds?: ReadonlySet<string>,
    superAdminUserIds?: ReadonlySet<string>
): 'obs-chat-message--streamer' | 'obs-chat-message--mod' | 'obs-chat-message--superadmin' | 'obs-chat-message--viewer' {
    const userType = resolveChatUserType(item, streamerId, modUserIds, superAdminUserIds)
    if (userType === 'streamer') return 'obs-chat-message--streamer'
    if (userType === 'manager') return 'obs-chat-message--mod'
    if (userType === 'superAdmin') return 'obs-chat-message--superadmin'
    return 'obs-chat-message--viewer'
}



