import { AppSource, LogUserType } from 'src/core/helpers/constant';
import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager';
import { EventTopics } from 'src/core/strings/constants/interface.constants';
import { LogUserLoginEvent } from 'src/modules/configuration/log/domain/entities/log-user-login.event';
import { UserModel } from 'src/modules/user-related/user/data/models/user.model';
import { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity';

export class LogoutAdminQueueManager extends BaseCustomManager<UserEntity> {
  protected userLogin;

  async validateProcess(): Promise<void> {
    return;
  }

  async beforeProcess(): Promise<void> {
    return;
  }

  async process(): Promise<void> {
    const id = this.data?.user_id ?? this.user.id;

    this.userLogin = await this.dataService.getOneByOptions({
      where: { id },
    });

    await this.dataService.removeUserLogin({
      user_id: id,
      source: AppSource.QUEUE_ADMIN,
    });

    await this.publishEvents();
    return;
  }

  async afterProcess(): Promise<void> {
    return;
  }

  getResult() {
    return `Success Logout User`;
  }

  get entityTarget(): any {
    return UserModel;
  }

  get eventTopics(): EventTopics[] {
    return [
      {
        topic: LogUserLoginEvent,
        data: {
          type: LogUserType.logout,
          role: this.userLogin.role,
          user_id: this.userLogin.id,
          username: this.userLogin.name,
          created_at: new Date().getTime(),
          source: AppSource.QUEUE_ADMIN,
        },
      },
    ];
  }
}
