import { Component } from '@angular/core';
import { UserService } from '../../../services/api/user.service';
import { ModuleOfUserService } from '../../../services/api/module-of-user.service';
import { GroupOfUserService } from '../../../services/api/group-of-user.service';
import { User } from '../../../models/user-management.api.models';
import { GuiGridModule } from '@generic-ui/ngx-grid';
import { BaseTableComponent } from '../base-table/base-table.component';
import { ColorModeService } from '../../../services/button/color-mode.service'
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { env } from '../../../../environments/environment';

@Component({
  standalone: true,
  imports: [CommonModule, FormsModule, GuiGridModule],
  selector: 'app-user-table',
  templateUrl: '../base-table/base-table.component.html',
  styleUrl: './user-table.component.css'
})
export class UserTableComponent extends BaseTableComponent<User, UserService> {

  mosService: ModuleOfUserService;
  gosService: GroupOfUserService;

  constructor(mosService: ModuleOfUserService,
    gosService: GroupOfUserService,
    service: UserService,
    cModeService: ColorModeService) {
    super(service, env.columnNames.user.basic, cModeService)
    this.mosService = mosService;
    this.gosService = gosService;
  }

  fetchDataByModuleId(moduleId: number, assigned: boolean = true): void {
    this.service.getByModuleId(moduleId, assigned).subscribe({
      next: (users) => {
        this.source = users;
      },
      error: (error) => {}
    });
  }

  fetchDataByGroupId(groupId: number, assigned: boolean = true): void {
    this.service.getByGroupId(groupId, assigned).subscribe({
      next: (users) => {
        this.source = users;
      },
      error: (error) => {}
    });
  }

  async createModuleOfUsers(moduleId: number, users: User[]): Promise<any[]> {
    const creationPromises = users
      .filter(user => user.id && user.id != null)
      .map(user => this.mosService.create({ moduleId: moduleId, userId: user.id ?? -1, addedWho: "DEFAULT" }).toPromise());

    return Promise.all(creationPromises);
  }

  async createGroupOfUsers(groupId: number, users: User[]): Promise<any[]> {
    const creationPromises = users
      .filter(user => user.id && user.id != null)
      .map(user => this.gosService.create({ groupId: groupId, userId: user.id ?? -1, addedWho: "DEFAULT" }).toPromise());

    return Promise.all(creationPromises);
  }

  async deleteModuleOfUsers(moduleId: number, users: User[]): Promise<void> {
    const deletionPromises = users
      .filter(user => user.id)
      .map(user => this.mosService.deleteByModuleGroupId(moduleId, user.id ?? -1).toPromise());

    try {
      const responses = await Promise.all(deletionPromises);
    } catch (error) {
    }
  }

  async deleteGroupOfUsers(groupId: number, users: User[]): Promise<void> {
    const deletionPromises = users
      .filter(user => user.id)
      .map(user => this.gosService.deleteByGroupUserId(groupId, user.id ?? -1).toPromise());

    try {
      const responses = await Promise.all(deletionPromises);
    } catch (error) {
    }
  }
}