import { useUIStore } from '../stores/ui'

const CORPORATE_LABELS = {
  'ult': 'Ultimate',
  'super operator': 'Super Operator',
  'operator': 'Operator',
  'coordinator': 'Coordinator',
  'supplier overseer': 'Supplier Overseer',
  'wholesale buyer': 'Wholesale Buyer',
  'supplier': 'Supplier',
  'store owner': 'Store Owner',
  'store manager': 'Store Manager',
  'user': 'User',
  'rider': 'Rider',
  'audit': 'Audit',
  'pos terminal': 'POS Terminal',
}

const COOPERATIVE_LABELS = {
  'ult': 'Chairperson',
  'super operator': 'General Manager',
  'operator': 'Officer',
  'coordinator': 'Chapter Coordinator',
  'supplier overseer': 'Supplier Liaison',
  'wholesale buyer': 'Bulk Buyer',
  'supplier': 'Member-Supplier',
  'store owner': 'Branch Owner',
  'store manager': 'Branch Manager',
  'user': 'Member',
  'rider': 'Rider',
  'audit': 'Auditor',
  'pos terminal': 'POS Terminal',
}

const TYPE_BADGE_CLASS = {
  'ult': 'bg-danger',
  'super operator': 'bg-warning text-dark',
  'operator': 'bg-primary',
  'coordinator': 'bg-info text-dark',
  'store owner': 'bg-success',
  'store manager': 'bg-info text-white',
  'user': 'bg-secondary',
}

export function getUserTypeLabel(type, mode) {
  if (!type) return 'Unknown'
  const key = String(type).toLowerCase()
  const map = mode === 'cooperative' ? COOPERATIVE_LABELS : CORPORATE_LABELS
  return map[key] || CORPORATE_LABELS[key] || (key.charAt(0).toUpperCase() + key.slice(1))
}

export function getUserTypeBadgeClass(type) {
  if (!type) return 'bg-secondary'
  return TYPE_BADGE_CLASS[String(type).toLowerCase()] || 'bg-secondary'
}

export function useUserTypeLabels() {
  const ui = useUIStore()
  return {
    label: (type) => getUserTypeLabel(type, ui.app_mode),
    badgeClass: (type) => getUserTypeBadgeClass(type),
  }
}
