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

export class LogoutManager extends BaseCustomManager<UserEntity> {
  async validateProcess(): Promise<void> {
    return;
  }

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

  async process(): Promise<void> {
    await this.dataService.removeUserLogin({
      user_id: this.user.id,
      login_token: this.userProvider.token,
      source: AppSource.POS_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.user.role,
          user_id: this.user.id,
          username: this.user.name,
          created_at: new Date().getTime(),
          source: AppSource.POS_ADMIN,
        },
      },
    ];
  }
}
