import { Component, Inject } from '@angular/core';
import { BaseTableComponent } from '../base-table/base-table.component';
import { DirGroupService } from '../../../services/api/dir-group.service';
import { DirGroup } from '../../../models/user-management.api.models';
import { GuiGridModule } 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 { GroupService } from '../../../services/api/group.service';
import { firstValueFrom } from 'rxjs';

@Component({
  standalone: true,
  imports: [CommonModule, FormsModule, GuiGridModule ],
  selector: 'app-dir-group-table',
  templateUrl: '../base-table/base-table.component.html',
  styleUrl: './dir-group-table.component.css'
})
export class DirGroupTableComponent extends BaseTableComponent<DirGroup, DirGroupService> {
  constructor(service: DirGroupService, cModeService: ColorModeService, private gService: GroupService) {
    super(service, env.columnNames.dirGroup, cModeService)
  }

  override fetchData(): void {
    this.service.getAll().subscribe({
      next: async (response) => {
        const group_names = (await firstValueFrom(this.gService.getAll())).map(g => g.name);
        this.source = response.filter(dGroup => dGroup.samaccountname?.length && !group_names.includes(dGroup.samaccountname[0]));
        this.loading = false;
      },
      error: (error) => { }
    });
  }
}