'use client'

import { columns as columns_, type Order } from '@repo/ui/routes/orders/columns'

import { formatCNPJ, formatCPF } from '@brazilian-utils/brazilian-utils'
import { type ColumnDef } from '@tanstack/react-table'

import { Abbr } from '@repo/ui/components/abbr'
import { Avatar, AvatarFallback } from '@repo/ui/components/ui/avatar'
import { initials } from '@repo/ui/lib/utils'

export type { Order }

export const columns: ColumnDef<Order>[] = [
  {
    header: 'Comprador',
    cell: ({ row }) => {
      const { name, email } = row.original

      return (
        <div className="flex gap-2.5 items-center">
          <Avatar className="size-10 hidden lg:block">
            <AvatarFallback>{initials(name)}</AvatarFallback>
          </Avatar>

          <ul>
            <li className="font-bold">
              <Abbr>{name}</Abbr>
            </li>
            <li className="text-muted-foreground text-sm">
              <Abbr>{email}</Abbr>
            </li>
          </ul>
        </div>
      )
    }
  },
  {
    header: 'CNPJ/CPF',
    cell: ({ row }) => {
      const { cpf, cnpj } = row.original

      if (cpf) {
        return <>{formatCPF(cpf)}</>
      }

      if (cnpj) {
        return <>{formatCNPJ(cnpj)}</>
      }

      return <></>
    }
  },
  ...columns_
]
