/**
 * @aurora-catalyst-generated
 * @source cliter/iam/account.aurora.yaml
 */
import { IamGetAccountsService } from '@app/iam/account';
import { HandlerMeta } from '@aurorajs.dev/core-back';
import { Injectable } from '@nestjs/common';

@Injectable()
export class IamCheckUniqueUsernameAccountHandler {
  constructor(private readonly getService: IamGetAccountsService) {}

  async main(
    username: string,
    avoidUsernames: string[] = [],
    handlerMeta?: HandlerMeta,
  ): Promise<boolean> {
    const normalized = username.toLowerCase();
    const avoid = new Set(avoidUsernames.map((value) => value.toLowerCase()));
    const matches = await this.getService.main(
      { where: { username: normalized } },
      undefined,
      handlerMeta,
    );
    return matches.every((account) => avoid.has((account.username ?? '').toLowerCase()));
  }
}
