/**
 * Account list columns — overrides codegen scaffold to add HlmBadgeIconCell
 * for tenants/tags/scopes (icon + count badge + tooltip with joined names) and
 * user.name / user.surname columns via nested accessorFn.
 *
 * Overrides cliter/iam/account.aurora.yaml → see excludedFiles.
 */
import { BooleanCell, HlmBadgeIconCell, type DataTableColumnDef } from '@aurora';
import { TranslocoService } from '@jsverse/transloco';
import { type IamAccount, type IamTenant } from '@src/src/app/aurora.types';
import { flexRenderComponent } from '@tanstack/angular-table';

export const getAccountStandaloneColumns = (properties?: {
  translator?: TranslocoService;
}): DataTableColumnDef<IamAccount>[] => {
  const { translator = null } = properties ?? {};

  return [
    {
      accessorKey: 'tenants',
      id: 'tenants',
      size: 80,
      label: () => translator?.translate('iam.account.Tenants') ?? 'Tenants',
      enableSorting: false,
      filterable: true,
      cell: (info) =>
        flexRenderComponent(HlmBadgeIconCell, {
          inputs: {
            items: (info.getValue<IamTenant[]>() ?? []) as IamTenant[],
            label: (tenant: unknown) => (tenant as IamTenant).name,
            iconName: 'lucideBuilding2',
          },
        }),
    },
    {
      accessorKey: 'tags',
      id: 'tags',
      size: 80,
      label: () => translator?.translate('iam.account.Tags') ?? 'Tags',
      enableSorting: false,
      filterable: true,
      cell: (info) =>
        flexRenderComponent(HlmBadgeIconCell, {
          inputs: {
            items: (info.getValue<string[]>() ?? []) as string[],
            label: (tag: unknown) => String(tag),
            iconName: 'lucideTag',
          },
        }),
    },
    {
      accessorKey: 'scopes',
      id: 'scopes',
      size: 80,
      label: () => translator?.translate('iam.account.Scopes') ?? 'Scopes',
      enableSorting: false,
      filterable: true,
      cell: (info) =>
        flexRenderComponent(HlmBadgeIconCell, {
          inputs: {
            items: (info.getValue<string[]>() ?? []) as string[],
            label: (scope: unknown) => String(scope),
            iconName: 'lucideTarget',
          },
        }),
    },
    {
      accessorKey: 'email',
      id: 'email',
      size: 280,
      minSize: 160,
      label: () => translator?.translate('iam.account.Email') ?? 'Email',
      sortable: true,
      searchable: true,
      filterable: true,
      exportable: true,
      cell: (info) => `${info.getValue<string>() ?? ''}`,
    },
    {
      accessorFn: (row) => (row as IamAccount & { user?: { name?: string } }).user?.name ?? '',
      id: 'user.name',
      size: 180,
      minSize: 120,
      relation: { association: 'user' },
      label: () => translator?.translate('iam.account.Name') ?? 'Name',
      sortable: true,
      searchable: true,
      filterable: true,
      exportable: true,
      cell: (info) => `${info.getValue<string>() ?? ''}`,
    },
    {
      accessorFn: (row) =>
        (row as IamAccount & { user?: { surname?: string } }).user?.surname ?? '',
      id: 'user.surname',
      size: 180,
      minSize: 120,
      relation: { association: 'user' },
      label: () => translator?.translate('iam.account.Surname') ?? 'Surname',
      sortable: true,
      searchable: true,
      filterable: true,
      exportable: true,
      cell: (info) => `${info.getValue<string>() ?? ''}`,
    },
    {
      accessorKey: 'code',
      id: 'code',
      size: 140,
      minSize: 100,
      label: () => translator?.translate('iam.account.Code') ?? 'Code',
      sortable: true,
      searchable: true,
      filterable: true,
      exportable: true,
      cell: (info) => `${info.getValue<string>() ?? ''}`,
    },
    {
      accessorKey: 'username',
      id: 'username',
      size: 200,
      minSize: 140,
      label: () => translator?.translate('iam.account.Username') ?? 'Username',
      sortable: true,
      searchable: true,
      filterable: true,
      exportable: true,
      cell: (info) => `${info.getValue<string>() ?? ''}`,
    },
    {
      accessorKey: 'isActive',
      id: 'isActive',
      size: 80,
      label: () => translator?.translate('iam.account.IsActive') ?? 'IsActive',
      sortable: true,
      filterable: true,
      exportable: true,
      cell: () => flexRenderComponent(BooleanCell, { inputs: {} }),
    },
    {
      accessorKey: 'type',
      id: 'type',
      size: 100,
      label: () => translator?.translate('iam.account.Type') ?? 'Type',
      sortable: true,
      filterable: true,
      exportable: true,
      cell: (info) => `${info.getValue<string>() ?? ''}`,
    },
  ];
};
