import { useCompaniesStore } from 'stores/companies'
import type { User } from 'types/User'

export function useUserSection () {
  const companiesStore = useCompaniesStore()
  
  const userSection = (user: User) => {
    const tname = () => {
      return user.firstname
        ? user.lastname
          ? user.firstname + ' ' + user.lastname
          : user.firstname
        : user.lastname ?? ''
    }

    const section1 = user.fullname && user.fullname !== '' ? user.fullname : tname()
    const section2_1 = user.fullname ? tname() : ''
    const section2_2 = user.username ?? ''
    
    const section3 = (
      user.company_id && companiesStore.companyById(user.company_id)
        ? companiesStore.companyById(user.company_id)?.name + ((user.role || user.department) ? ', ' : '')
        : ''
    ) +
    (user.department ? user.department + (user.role ? ' - ' : '') : '') + 
    (user.role ?? '')

    return {
      section1,
      section2_1,
      section2_2,
      section3
    }
  }

  return { userSection }
}
