import { Injectable, Provider } from '@angular/core';
import {
  DB_MODEL_COLUMNS,
  ID_COLUMN,
  PageColumns,
} from 'src/app/core/base/page.columns';
import { TableColumn } from 'src/app/modules/shared/components/table/table.model';
import { User } from 'src/app/model/auth/user';

@Injectable()
export class UsersColumns extends PageColumns<User> {
  get(): TableColumn<User>[] {
    return [
      ID_COLUMN,
      {
        name: 'keycloakId',
        translationKey: 'user.keycloak_id',
        type: 'text',
        filterable: true,
        sortable: true,
        value: (row: User) => row.keycloakId,
      },
      {
        name: 'username',
        translationKey: 'user.username',
        type: 'text',
        value: (row: User) => row.username,
        filterable: true,
      },
      {
        name: 'email',
        translationKey: 'user.email',
        type: 'text',
        value: (row: User) => row.email,
        filterable: true,
      },
      {
        name: 'roles',
        translationKey: 'common.roles',
        type: 'text',
        filterable: false,
        sortable: false,
        value: (row: User) =>
          (row?.roles ?? []).map(role => role.name).join(', '),
        width: '300px',
      },
      ...DB_MODEL_COLUMNS,
    ];
  }

  static provide(): Provider[] {
    return [
      {
        provide: PageColumns,
        useClass: UsersColumns,
      },
      UsersColumns,
    ];
  }
}
