import { Session } from 'next-auth'

export type UserRole = 'technical' | 'business' | 'developer' | 'admin'

/**
 * Determines the primary role for a user based on their session roles
 * Priority: technical > business > developer > admin (default)
 */
export function getUserRole(session: Session | null): UserRole {
  if (!session?.roles || session.roles.length === 0) {
    return 'admin' // Default role
  }

  const roles = session.roles.map(r => r.toLowerCase())

  // Check for technical admin roles
  if (roles.some(r => r.includes('technical') || r.includes('admin') || r.includes('operator'))) {
    return 'technical'
  }

  // Check for business owner roles
  if (roles.some(r => r.includes('business') || r.includes('owner') || r.includes('manager'))) {
    return 'business'
  }

  // Check for developer roles
  if (roles.some(r => r.includes('developer') || r.includes('dev') || r.includes('engineer'))) {
    return 'developer'
  }

  // Default to admin/technical
  return 'technical'
}

/**
 * Gets the dashboard route for a user based on their role
 */
export function getDashboardRoute(session: Session | null): string {
  const role = getUserRole(session)
  return `/dashboard/${role}`
}

