import { Component, Inject } from '@angular/core';
import { DirUser } from '../../../models/user-management.api.models';
import { DirUserService } from '../../../services/api/dir-user.service';
import { BaseTableComponent } from '../base-table/base-table.component';
import { GuiGridModule, GuiColumn } from '@generic-ui/ngx-grid';
import { ColorModeService } from '../../../services/button/color-mode.service';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { env } from '../../../../environments/environment';
import { UserService } from '../../../services/api/user.service';
import { firstValueFrom } from 'rxjs/internal/firstValueFrom';

@Component({
  standalone: true,
  imports: [CommonModule, FormsModule, GuiGridModule],
  selector: 'app-dir-user-table',
  templateUrl: '../base-table/base-table.component.html',
  styleUrl: './dir-user-table.component.css'
})
export class DirUserTableComponent extends BaseTableComponent<DirUser, DirUserService>  {
  constructor(service: DirUserService, cModeService: ColorModeService, private uService: UserService) {
    super(service, env.columnNames.dirUser, cModeService)
  }

  fetchDataByGroupName(groupName: string): void {
    this.service.getAll(groupName).subscribe({
      next: async (response: DirUser[]) => {

        const usernames = (await firstValueFrom(this.uService.getAll())).map(u => u.username)
        this.source = response.filter(user => user.samaccountname?.length && !usernames.includes(user.samaccountname[0]));
        this.loading = false;
      },
      error: (error: any) => { }
    })
  }
}