import { resolveGroupRoleBadge } from '@/lib/groups/groupRoleBadge'

/** DB 儲存用角色字串 */
export const GROUP_ROLE_LEADER = 'admin'
export const GROUP_ROLE_DEPUTY = 'artist'
export const GROUP_ROLE_MEMBER = 'member'

export type GroupMemberAction =
    | 'transfer_leader'
    | 'demote_member'
    | 'promote_deputy'
    | 'kick'

export function isGroupLeaderRole(role: string | null | undefined): boolean {
    return resolveGroupRoleBadge(role) === 'leader'
}

export function isGroupDeputyRole(role: string | null | undefined): boolean {
    return resolveGroupRoleBadge(role) === 'deputy'
}

/** 成員列表 SQL：僅家族長置頂，副家族長與一般成員依加入時間排序 */
export const GROUP_MEMBERS_LIST_ORDER_SQL = `
    CASE LOWER(TRIM(CAST(ug.role AS TEXT)))
        WHEN 'admin' THEN 0
        WHEN 'leader' THEN 0
        WHEN 'head' THEN 0
        WHEN 'owner' THEN 0
        WHEN '1' THEN 0
        ELSE 1
    END,
    ug.join_at ASC,
    (SELECT COUNT(1) FROM user_actions f WHERE f.target_id = u.user_id AND f.action_type = 'follow') DESC
`
